aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorEloy Duran <eloy.de.enige@gmail.com>2009-12-31 14:07:56 +0100
committerEloy Duran <eloy.de.enige@gmail.com>2010-01-07 13:19:48 +0100
commit2171e0a1d13a176db1e9e9dd55242fdb319b526c (patch)
treedf7594c11e84aeee8541603219a88d2a13e372e5 /activerecord
parentf82adc7c5addb4f9bf6b9cb3f1fcf3fb47505e53 (diff)
downloadrails-2171e0a1d13a176db1e9e9dd55242fdb319b526c.tar.gz
rails-2171e0a1d13a176db1e9e9dd55242fdb319b526c.tar.bz2
rails-2171e0a1d13a176db1e9e9dd55242fdb319b526c.zip
Cleanup some code in nested_attributes.rb, autosave_association.rb, and associations.rb with AssociationReflection#collection_association?
Also cache the result value.
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb7
-rw-r--r--activerecord/lib/active_record/autosave_association.rb3
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb8
-rw-r--r--activerecord/lib/active_record/reflection.rb5
4 files changed, 9 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index b2ae447d5e..1320f5b624 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1770,7 +1770,7 @@ module ActiveRecord
end
def using_limitable_reflections?(reflections)
- reflections.reject { |r| [ :belongs_to, :has_one ].include?(r.macro) }.length.zero?
+ reflections.collect(&:collection_association?).length.zero?
end
def column_aliases(join_dependency)
@@ -1843,7 +1843,7 @@ module ActiveRecord
case associations
when Symbol, String
reflection = base.reflections[associations]
- if reflection && [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
+ if reflection && reflection.collection_association?
records.each { |record| record.send(reflection.name).target.uniq! }
end
when Array
@@ -1853,12 +1853,11 @@ module ActiveRecord
when Hash
associations.keys.each do |name|
reflection = base.reflections[name]
- is_collection = [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
parent_records = []
records.each do |record|
if descendant = record.send(reflection.name)
- if is_collection
+ if reflection.collection_association?
parent_records.concat descendant.target.uniq
else
parent_records << descendant
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index 98ab64537e..62cb996b5d 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -167,8 +167,7 @@ module ActiveRecord
validation_method = "validate_associated_records_for_#{reflection.name}"
force_validation = (reflection.options[:validate] == true || reflection.options[:autosave] == true)
- case reflection.macro
- when :has_many, :has_and_belongs_to_many
+ if reflection.collection_association?
unless method_defined?(save_method)
before_save :before_save_collection_association
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index 3e6f84f31d..e05e46916c 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -235,16 +235,10 @@ module ActiveRecord
attr_names.each do |association_name|
if reflection = reflect_on_association(association_name)
- type = case reflection.macro
- when :has_one, :belongs_to
- :one_to_one
- when :has_many, :has_and_belongs_to_many
- :collection
- end
-
reflection.options[:autosave] = true
add_autosave_association_callbacks(reflection)
nested_attributes_options[association_name.to_sym] = options
+ type = (reflection.collection_association? ? :collection : :one_to_one)
# def pirate_attributes=(attributes)
# assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 69772bf9bb..96aac96cda 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -256,7 +256,10 @@ module ActiveRecord
# association. Returns +true+ if the +macro+ is one of +has_many+ or
# +has_and_belongs_to_many+, +false+ otherwise.
def collection_association?
- [:has_many, :has_and_belongs_to_many].include?(macro)
+ if @collection_association.nil?
+ @collection_association = [:has_many, :has_and_belongs_to_many].include?(macro)
+ end
+ @collection_association
end
private