Module: Remotely::Associations::ClassMethods
- Defined in:
- lib/remotely/associations.rb
Overview
A set class methods for defining associations that are retreived from a remote API. They’re available to all classes which inherit from ActiveRecord::Base orRemotely::Model.
class Show < ActiveRecord::Base
has_many_remote :members
has_one_remote :set
belongs_to_remote :station
end
Warning
Just like with ActiveRecord, associations will overwrite any instance method with the same name as the association. So don’t do that.
Cardinality and Defining Associations
Remotely can be used to specify one-to-one and one-to-many associations. Many-to-many is not supported.
Unlike ActiveRecord, remote associations are only defined on the client side. has_many
relations have no accompanying belongs_to
.
One-to-many
Use has_many_remote
to define a one-to-many relationship where the model you are defining it in is the parent.
URI Assumptions
Remotely assumes all has_many_remote
associations can be found at:
/model_name(plural)/id/association_name(plural)
Example
class User < ActiveRecord::Base
has_many_remote :friends
end
user = User.new(:id => 1)
user.friends # => /users/1/friends
One-to-one
Use has_one_remote
to define a one-to-one relationship.
URI Assumptions
Remotely assumes all has_one_remote
associations can be found at:
/model_name(plural)/id/association_name(singular)
Example
class Car < ActiveRecord::Base
has_one_remote :engine
end
car = Car.new(:id => 1)
car.engine # => /cars/1/engine
Many-to-one
Use belongs_to_remote
to define a many-to-one relationship. That is, if the model you’re defining this on has a foreign key to the remote model.
URI Assumptions
Remotely assumes all belongs_to_remote
associations can be found at:
/association_name(plural)/{association_name}_id
Example
class Car < ActiveRecord::Base
belongs_to_remote :brand
end
car = Car.new(:brand_id => 2)
car.brand # => /brands/2
Options
:path
The full URI that should be used to fetch this resource. (supported by all methods)
:foreign_key
The attribute that should be used, instead of id
when generating URIs. (supported by belongs_to_remote
only)
Path Variables
The path
option will replace any symbol(ish) looking string with the value of that attribute, of the model.
class User < ActiveRecord::Base
belongs_to_remote :family, :path => "/families/:family_key"
end
user = User.new(:family_key => "noble")
user.family # => /families/noble
Instance Attribute Summary collapse
-
#remote_associations ⇒ Object
Remote associations defined and their options.
Instance Method Summary collapse
-
#belongs_to_remote(name, options = {}) ⇒ Object
Specifies a many-to-one relationship.
-
#has_many_remote(name, options = {}) ⇒ Object
Specifies a one-to-many relationship.
-
#has_one_remote(name, options = {}) ⇒ Object
Specifies a one-to-one relationship.
Instance Attribute Details
#remote_associations ⇒ Object
Remote associations defined and their options.
111 112 113 |
# File 'lib/remotely/associations.rb', line 111 def remote_associations @remote_associations end |
Instance Method Details
#belongs_to_remote(name, options = {}) ⇒ Object
Specifies a many-to-one relationship.
141 142 143 |
# File 'lib/remotely/associations.rb', line 141 def belongs_to_remote(name, ={}) define_association_method(:belongs_to, name, ) end |
#has_many_remote(name, options = {}) ⇒ Object
Specifies a one-to-many relationship.
119 120 121 |
# File 'lib/remotely/associations.rb', line 119 def has_many_remote(name, ={}) define_association_method(:has_many, name, ) end |
#has_one_remote(name, options = {}) ⇒ Object
Specifies a one-to-one relationship.
129 130 131 |
# File 'lib/remotely/associations.rb', line 129 def has_one_remote(name, ={}) define_association_method(:has_one, name, ) end |