lib/active_remote/association.rb in active_remote-5.1.1 vs lib/active_remote/association.rb in active_remote-5.2.0.alpha
- old
+ new
@@ -6,12 +6,11 @@
# Create a `belongs_to` association for a given remote resource.
# Specify one or more associations to define. The constantized
# class must be loaded into memory already. A method will be defined
# with the same name as the association. When invoked, the associated
# remote model will issue a `search` for the :guid with the associated
- # guid's attribute (e.g. read_attribute(:client_guid)) and return the first
- # remote object from the result, or nil.
+ # guid attribute and return the first remote object from the result, or nil.
#
# A `belongs_to` association should be used when the associating remote
# contains the guid to the associated model. For example, if a User model
# `belongs_to` Client, the User model would have a client_guid field that is
# used to search the Client service. The Client model would have no
@@ -35,23 +34,23 @@
#
def belongs_to(belongs_to_klass, options = {})
perform_association(belongs_to_klass, options) do |klass, object|
foreign_key = options.fetch(:foreign_key) { :"#{klass.name.demodulize.underscore}_guid" }
search_hash = {}
- search_hash[:guid] = object.read_attribute(foreign_key)
- search_hash[options[:scope]] = object.read_attribute(options[:scope]) if options.key?(:scope)
+ search_hash[:guid] = object.send(foreign_key)
+ search_hash[options[:scope]] = object.send(options[:scope]) if options.key?(:scope)
search_hash.values.any?(&:nil?) ? nil : klass.search(search_hash).first
end
end
# Create a `has_many` association for a given remote resource.
# Specify one or more associations to define. The constantized
# class must be loaded into memory already. A method will be defined
# with the same plural name as the association. When invoked, the associated
# remote model will issue a `search` for the :guid with the associated
- # guid's attribute (e.g. read_attribute(:client_guid)).
+ # guid attribute.
#
# A `has_many` association should be used when the associated model has
# a field to identify the associating model, and there can be multiple
# remotes associated. For example, if a Client has many Users, the User remote
# would have a client_guid field that is searchable. That search would likely
@@ -77,11 +76,11 @@
def has_many(has_many_class, options = {})
perform_association(has_many_class, options) do |klass, object|
foreign_key = options.fetch(:foreign_key) { :"#{object.class.name.demodulize.underscore}_guid" }
search_hash = {}
search_hash[foreign_key] = object.guid
- search_hash[options[:scope]] = object.read_attribute(options[:scope]) if options.key?(:scope)
+ search_hash[options[:scope]] = object.send(options[:scope]) if options.key?(:scope)
search_hash.values.any?(&:nil?) ? [] : klass.search(search_hash)
end
options[:has_many] = true
@@ -91,12 +90,11 @@
# Create a `has_one` association for a given remote resource.
# Specify one or more associations to define. The constantized
# class must be loaded into memory already. A method will be defined
# with the same name as the association. When invoked, the associated
# remote model will issue a `search` for the :guid with the associated
- # guid's attribute (e.g. read_attribute(:client_guid)) and return the first
- # remote object in the result, or nil.
+ # guid attribute and return the first remote object in the result, or nil.
#
# A `has_one` association should be used when the associated remote
# contains the guid from the associating model. For example, if a User model
# `has_one` Client, the Client remote would have a user_guid field that is
# searchable. The User model would have no reference to the client.
@@ -118,10 +116,10 @@
def has_one(has_one_klass, options = {})
perform_association(has_one_klass, options) do |klass, object|
foreign_key = options.fetch(:foreign_key) { :"#{object.class.name.demodulize.underscore}_guid" }
search_hash = {}
search_hash[foreign_key] = object.guid
- search_hash[options[:scope]] = object.read_attribute(options[:scope]) if options.key?(:scope)
+ search_hash[options[:scope]] = object.send(options[:scope]) if options.key?(:scope)
search_hash.values.any?(&:nil?) ? nil : klass.search(search_hash).first
end
end