aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_association.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-12-26 19:37:35 +0000
committerJon Leighton <j@jonathanleighton.com>2010-12-26 19:38:04 +0000
commitb0498372a10bda006350af42708a5588ab28ffcb (patch)
tree3dbbd2186cf61fa253d9302dd751c2f88b2636dc /activerecord/lib/active_record/associations/has_association.rb
parent0c272471fe815c121a83d959468b2f1b6f8aaba8 (diff)
downloadrails-b0498372a10bda006350af42708a5588ab28ffcb.tar.gz
rails-b0498372a10bda006350af42708a5588ab28ffcb.tar.bz2
rails-b0498372a10bda006350af42708a5588ab28ffcb.zip
Add a HasAssociation module for common code for has_* associations
Diffstat (limited to 'activerecord/lib/active_record/associations/has_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/has_association.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/has_association.rb b/activerecord/lib/active_record/associations/has_association.rb
new file mode 100644
index 0000000000..4407e2ea9a
--- /dev/null
+++ b/activerecord/lib/active_record/associations/has_association.rb
@@ -0,0 +1,54 @@
+module ActiveRecord
+ module Associations
+ # Included in all has_* associations (i.e. everything except belongs_to)
+ module HasAssociation #:nodoc:
+ protected
+ # Sets the owner attributes on the given record
+ def set_owner_attributes(record)
+ if @owner.persisted?
+ construct_owner_attributes.each { |key, value| record[key] = value }
+ end
+ end
+
+ # Returns a hash linking the owner to the association represented by the reflection
+ def construct_owner_attributes(reflection = @reflection)
+ attributes = {}
+ if reflection.macro == :belongs_to
+ attributes[reflection.association_primary_key] = @owner.send(reflection.primary_key_name)
+ else
+ attributes[reflection.primary_key_name] = @owner.send(reflection.active_record_primary_key)
+
+ if reflection.options[:as]
+ attributes["#{reflection.options[:as]}_type"] = @owner.class.base_class.name
+ end
+ end
+ attributes
+ end
+
+ # Builds an array of arel nodes from the owner attributes hash
+ def construct_owner_conditions(table = aliased_table, reflection = @reflection)
+ construct_owner_attributes(reflection).map do |attr, value|
+ table[attr].eq(value)
+ end
+ end
+
+ def construct_conditions
+ conditions = construct_owner_conditions
+ conditions << Arel.sql(sql_conditions) if sql_conditions
+ aliased_table.create_and(conditions)
+ end
+
+ if RUBY_VERSION < '1.9.2'
+ # Array#flatten has problems with recursive arrays before Ruby 1.9.2.
+ # Going one level deeper solves the majority of the problems.
+ def flatten_deeper(array)
+ array.collect { |element| (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element }.flatten
+ end
+ else
+ def flatten_deeper(array)
+ array.flatten
+ end
+ end
+ end
+ end
+end