Permalink
Browse files

Rename AssociationProxy#loaded to loaded! as it mutates the association

  • Loading branch information...
1 parent de05e2f commit aa86420be24d7df9c07379bcf6f33904d0d41adc @jonleighton jonleighton committed Jan 24, 2011
@@ -125,7 +125,7 @@ def preload_one_association(records, association, preload_options={})
def add_preloaded_records_to_collection(parent_records, reflection_name, associated_record)
parent_records.each do |parent_record|
association_proxy = parent_record.send(reflection_name)
- association_proxy.loaded
+ association_proxy.loaded!
association_proxy.target.concat(Array.wrap(associated_record))
association_proxy.send(:set_inverse_instance, associated_record)
end
@@ -187,7 +187,7 @@ def preload_has_and_belongs_to_many_association(records, reflection, preload_opt
id_to_record_map = construct_id_map(records)
- records.each {|record| record.send(reflection.name).loaded}
+ records.each { |record| record.send(reflection.name).loaded! }
options = reflection.options
right = Arel::Table.new(options[:join_table]).alias('t0')
@@ -268,7 +268,7 @@ def preload_has_many_association(records, reflection, preload_options={})
foreign_key = reflection.through_reflection_foreign_key
id_to_record_map = construct_id_map(records, foreign_key || reflection.options[:primary_key])
- records.each {|record| record.send(reflection.name).loaded}
+ records.each { |record| record.send(reflection.name).loaded! }
if options[:through]
through_records = preload_through_records(records, reflection, options[:through])
@@ -371,7 +371,7 @@ def load_target
@target = merge_target_lists(targets, @target)
end
- loaded
+ loaded!
target
end
@@ -128,7 +128,7 @@ def loaded?
end
# Asserts the \target has been loaded setting the \loaded flag to +true+.
- def loaded
+ def loaded!
@loaded = true
@stale_state = stale_state
end
@@ -152,7 +152,7 @@ def stale_target?
# Sets the target of this proxy to <tt>\target</tt>, and the \loaded flag to +true+.
def target=(target)
@target = target
- loaded
+ loaded!
end
# Forwards the call to the target. Loads the \target if needed.
@@ -227,7 +227,7 @@ def target_scope
# not reraised. The proxy is \reset and +nil+ is the return value.
def load_target
@target = find_target if find_target?
- loaded
+ loaded!
target
rescue ActiveRecord::RecordNotFound
reset
@@ -210,7 +210,7 @@ def construct_association(record, join_part, row)
case macro
when :has_many, :has_and_belongs_to_many
collection = record.send(join_part.reflection.name)
- collection.loaded
+ collection.loaded!
collection.target.push(association)
collection.send(:set_inverse_instance, association)
when :belongs_to
@@ -40,7 +40,7 @@ def count_records
# If there's nothing in the database and @target has no new records
# we are certain the current target is an empty array. This is a
# documented side-effect of the method that may avoid an extra SELECT.
- @target ||= [] and loaded if count == 0
+ @target ||= [] and loaded! if count == 0
[@reflection.options[:limit], count].compact.min
end
@@ -368,7 +368,7 @@ def save_belongs_to_association(reflection)
if association.updated?
association_id = association.send(reflection.options[:primary_key] || :id)
self[reflection.foreign_key] = association_id
- association.loaded
+ association.loaded!
end
saved if autosave

0 comments on commit aa86420

Please sign in to comment.