Skip to content

Commit

Permalink
Cleanup some code in nested_attributes.rb, autosave_association.rb, a…
Browse files Browse the repository at this point in the history
…nd associations.rb with AssociationReflection#collection_association?

Also cache the result value.
  • Loading branch information
alloy committed Jan 7, 2010
1 parent f12dd62 commit c48a71c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
7 changes: 3 additions & 4 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -1783,7 +1783,7 @@ def references_eager_loaded_tables?(options)
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)
Expand Down Expand Up @@ -1860,7 +1860,7 @@ def remove_duplicate_results!(base, records, associations)
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
Expand All @@ -1870,12 +1870,11 @@ def remove_duplicate_results!(base, records, associations)
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.map do |record|
descendant = record.send(reflection.name)
next unless descendant
descendant.target.uniq! if is_collection
descendant.target.uniq! if reflection.collection_association?
descendant
end.flatten.compact

Expand Down
3 changes: 1 addition & 2 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -168,8 +168,7 @@ def add_autosave_association_callbacks(reflection)
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

Expand Down
8 changes: 1 addition & 7 deletions activerecord/lib/active_record/nested_attributes.rb
Expand Up @@ -231,16 +231,10 @@ def accepts_nested_attributes_for(*attr_names)

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)
Expand Down
5 changes: 4 additions & 1 deletion activerecord/lib/active_record/reflection.rb
Expand Up @@ -258,7 +258,10 @@ def polymorphic_inverse_of(associated_class)
# 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
Expand Down

0 comments on commit c48a71c

Please sign in to comment.