lib/active_remote/association.rb in active_remote-1.3.3 vs lib/active_remote/association.rb in active_remote-1.4.0
- old
+ new
@@ -36,14 +36,14 @@
# Client.search(:guid => self.client_guid).first
# end
# end
#
def belongs_to(belongs_to_klass, options={})
- perform_association( belongs_to_klass, options ) do |klass, obj|
+ perform_association(belongs_to_klass, options) do |klass, object|
foreign_key = options.fetch(:foreign_key) { :"#{belongs_to_klass}_guid" }
- association_guid = obj.read_attribute(foreign_key)
- klass.search(:guid => association_guid).first
+ association_guid = object.read_attribute(foreign_key)
+ klass.search(:guid => association_guid).first if association_guid
end
end
# Create a `has_many` association for a given remote resource.
# Specify one or more associations to define. The constantized
@@ -74,13 +74,13 @@
# User.search(:client_guid => self.guid)
# end
# end
#
def has_many(has_many_class, options={})
- perform_association( has_many_class, options ) do |klass, obj|
- foreign_key = options.fetch(:foreign_key) { :"#{obj.class.name.demodulize.underscore}_guid" }
- klass.search(foreign_key => obj.guid)
+ perform_association( has_many_class, options ) do |klass, object|
+ foreign_key = options.fetch(:foreign_key) { :"#{object.class.name.demodulize.underscore}_guid" }
+ object.guid ? klass.search(foreign_key => object.guid) : []
end
end
# Create a `has_one` association for a given remote resource.
# Specify one or more associations to define. The constantized
@@ -110,12 +110,12 @@
# Client.search(:user_guid => self.guid).first
# end
# end
#
def has_one(has_one_klass, options={})
- perform_association( has_one_klass, options ) do |klass, obj|
- foreign_key = options.fetch(:foreign_key) { :"#{obj.class.name.demodulize.underscore}_guid" }
- klass.search(foreign_key => obj.guid).first
+ perform_association(has_one_klass, options) do |klass, object|
+ foreign_key = options.fetch(:foreign_key) { :"#{object.class.name.demodulize.underscore}_guid" }
+ klass.search(foreign_key => object.guid).first if object.guid
end
end
private