Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/active_resource/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Builder
# === Options
# [:class_name]
# Specify the class name of the association. This class name would
# be used for resolving the association class.
# be used for resolving the association class.
#
# ==== Example for [:class_name] - option
# GET /posts/123.json delivers following response body:
Expand Down Expand Up @@ -48,7 +48,7 @@ def has_many(name, options = {})
# === Options
# [:class_name]
# Specify the class name of the association. This class name would
# be used for resolving the association class.
# be used for resolving the association class.
#
# ==== Example for [:class_name] - option
# GET /posts/1.json delivers following response body:
Expand All @@ -74,7 +74,7 @@ def has_one(name, options = {})
end

# Specifies a one-to-one association with another class. This class should only be used
# if this class contains the foreign key.
# if this class contains the foreign key.
#
# Methods will be added for retrieval and query for a single associated object, for which
# this object holds an id:
Expand All @@ -83,7 +83,7 @@ def has_one(name, options = {})
# Returns the associated object. +nil+ is returned if the foreign key is +nil+.
# Throws a ActiveResource::ResourceNotFound exception if the foreign key is not +nil+
# and the resource is not found.
#
#
# (+association+ is replaced with the symbol passed as the first argument, so
# <tt>belongs_to :post</tt> would add among others <tt>post.nil?</tt>.
#
Expand Down Expand Up @@ -130,8 +130,8 @@ def defines_belongs_to_finder_method(method_name, association_model, finder_key)
instance_variable_get(ivar_name)
elsif attributes.include?(method_name)
attributes[method_name]
else
instance_variable_set(ivar_name, association_model.find(send(finder_key)))
elsif association_id = send(finder_key)
instance_variable_set(ivar_name, association_model.find(association_id))
end
end
end
Expand All @@ -149,7 +149,7 @@ def defines_has_many_finder_method(method_name, association_model)
end
end
end

# Defines the has_one association
def defines_has_one_finder_method(method_name, association_model)
ivar_name = :"@#{method_name}"
Expand Down
18 changes: 18 additions & 0 deletions test/cases/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,22 @@ def test_defines_belongs_to_finder_method_with_instance_variable_cache
2.times{person.customer}
assert person.instance_variable_defined?(:@customer)
end

def test_belongs_to_with_finder_key
Person.defines_belongs_to_finder_method(:customer, Customer, 'customer_id')

person = Person.new
person.stubs(:customer_id).returns(1)
Customer.expects(:find).with(1).once()
person.customer
end

def test_belongs_to_with_nil_finder_key
Person.defines_belongs_to_finder_method(:customer, Customer, 'customer_id')

person = Person.new
person.stubs(:customer_id).returns(nil)
Customer.expects(:find).with(nil).never()
person.customer
end
end