Module: ActiveRemote::Association::ClassMethods
- Defined in:
- lib/active_remote/association.rb
Instance Method Summary collapse
-
#belongs_to(belongs_to_klass, options = {}) ⇒ Object
Create a
belongs_toassociation for a given remote resource. -
#has_many(has_many_class, options = {}) ⇒ Object
Create a
has_manyassociation for a given remote resource. -
#has_one(has_one_klass, options = {}) ⇒ Object
Create a
has_oneassociation for a given remote resource.
Instance Method Details
#belongs_to(belongs_to_klass, options = {}) ⇒ Object
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.
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
reference to the user.
====Examples
class User
belongs_to :client
end
An equivalent code snippet without a belongs_to declaration would be:
====Examples
class User
def client
Client.search(:guid => self.client_guid).first
end
end
40 41 42 43 44 45 46 |
# File 'lib/active_remote/association.rb', line 40 def belongs_to(belongs_to_klass, ={}) perform_association(belongs_to_klass, ) do |klass, object| foreign_key = .fetch(:foreign_key) { :"#{belongs_to_klass}_guid" } association_guid = object.read_attribute(foreign_key) klass.search(:guid => association_guid).first if association_guid end end |
#has_many(has_many_class, options = {}) ⇒ Object
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)).
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
return multiple user records. The client would not
have a field indicating which users are associated.
====Examples
class Client
has_many :users
end
An equivalent code snippet without a has_many declaration would be:
====Examples
class Client
def users
User.search(:client_guid => self.guid)
end
end
78 79 80 81 82 83 |
# File 'lib/active_remote/association.rb', line 78 def has_many(has_many_class, ={}) perform_association( has_many_class, ) do |klass, object| foreign_key = .fetch(:foreign_key) { :"#{object.class.name.demodulize.underscore}_guid" } object.guid ? klass.search(foreign_key => object.guid) : [] end end |
#has_one(has_one_klass, options = {}) ⇒ Object
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.
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.
====Examples
class User
has_one :client
end
An equivalent code snippet without a has_one declaration would be:
====Examples
class User
def client
Client.search(:user_guid => self.guid).first
end
end
114 115 116 117 118 119 |
# File 'lib/active_remote/association.rb', line 114 def has_one(has_one_klass, ={}) perform_association(has_one_klass, ) do |klass, object| foreign_key = .fetch(:foreign_key) { :"#{object.class.name.demodulize.underscore}_guid" } klass.search(foreign_key => object.guid).first if object.guid end end |