Skip to content

Commit

Permalink
Move the load_target implementation to SingularAssociation
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyj committed Apr 2, 2012
1 parent 60ccd6d commit 1fcd7c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
20 changes: 1 addition & 19 deletions activerecord/lib/active_record/associations/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,8 @@ def target_scope
end

# Loads the \target if needed and returns it.
#
# This method is abstract in the sense that it relies on +find_target+,
# which is expected to be provided by descendants.
#
# If the \target is already \loaded it is just returned. Thus, you can call
# +load_target+ unconditionally to get the \target.
#
# ActiveRecord::RecordNotFound is rescued within the method, and it is
# not reraised. The proxy is \reset and +nil+ is the return value.
def load_target
@target ||= find_target if find_target?
loaded! unless loaded?
target
rescue ActiveRecord::RecordNotFound
reset
raise NotImplementedError, "Subclasses must implement a load_target method"
end

def interpolate(sql, record = nil)
Expand All @@ -153,11 +140,6 @@ def find_target?
!loaded? && (!owner.new_record? || foreign_key_present?) && klass
end

# Implemented by subclasses
def find_target
raise NotImplementedError, "Subclasses must implement a find_target method"
end

def creation_attributes
attributes = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ def build(attributes = {}, options = {})
record
end

# Loads the \target if needed and returns it.
#
# This method is abstract in the sense that it relies on +find_target+,
# which is expected to be provided by descendants.
#
# If the \target is already \loaded it is just returned. Thus, you can call
# +load_target+ unconditionally to get the \target.
#
# ActiveRecord::RecordNotFound is rescued within the method, and it is
# not reraised. The proxy is \reset and +nil+ is the return value.
def load_target
@target ||= find_target if find_target?
loaded! unless loaded?
target
rescue ActiveRecord::RecordNotFound
reset
end

private

def create_scope
Expand Down

0 comments on commit 1fcd7c9

Please sign in to comment.