Class: ApiResource::Associations::HasManyRemoteObjectProxy
- Inherits:
-
MultiObjectProxy
- Object
- AssociationProxy
- MultiObjectProxy
- ApiResource::Associations::HasManyRemoteObjectProxy
- Defined in:
- lib/api_resource/associations/has_many_remote_object_proxy.rb
Instance Attribute Summary
Attributes inherited from AssociationProxy
#finder_opts, #klass, #owner, #remote_path
Class Method Summary collapse
-
.define_association_as_attribute(klass, assoc_name, opts = {}) ⇒ Object
defines a method to get the foreign key.
-
.foreign_key_name(assoc_name) ⇒ Object
gets the foreign key name for a given association e.g.
Instance Method Summary collapse
Methods inherited from MultiObjectProxy
#==, #all, #collection?, #each, #internal_object=, #serializable_hash
Methods inherited from AssociationProxy
#==, #expires_in, #includes, #initialize, #load_resource_definition, #loaded?, #reload, #ttl
Constructor Details
This class inherits a constructor from ApiResource::Associations::AssociationProxy
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ApiResource::Associations::AssociationProxy
Class Method Details
.define_association_as_attribute(klass, assoc_name, opts = {}) ⇒ Object
defines a method to get the foreign key
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/api_resource/associations/has_many_remote_object_proxy.rb', line 6 def self.define_association_as_attribute(klass, assoc_name, opts = {}) id_method_name = self.foreign_key_name(assoc_name) klass.api_resource_generated_methods.module_eval "\n def \#{id_method_name}\n @attributes_cache[:\#{id_method_name}] ||= begin\n # check our attributes first, then go to the remote\n @attributes[:\#{id_method_name}] || self.\#{assoc_name}.collect(\n &:id\n )\n end\n end\n EOE\n super\nend\n", __FILE__, __LINE__ + 1 |
.foreign_key_name(assoc_name) ⇒ Object
gets the foreign key name for a given association e.g. service_ids
26 27 28 |
# File 'lib/api_resource/associations/has_many_remote_object_proxy.rb', line 26 def self.foreign_key_name(assoc_name) super(assoc_name).pluralize end |
Instance Method Details
#internal_object ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/api_resource/associations/has_many_remote_object_proxy.rb', line 32 def internal_object # if we don't have a remote path and we do have and id, # we set it before we call the internal object # this lets us dynamically generate the correct path if self.remote_path.blank? # first try for a set of ids e.g. /objects.json?ids[]=1 associated_ids = self.owner.read_attribute( self.association_id_method ) if associated_ids.is_a?(Array) && associated_ids.present? self.remote_path = self.klass.collection_path( :ids => associated_ids ) # next try for a foreign key e.g. /objects.json?owner_id=1 elsif self.owner.try(:id).to_i > 0 self.remote_path = self.klass.collection_path( self.owner.class.to_s.foreign_key => self.owner.id ) end end super end |