Class: ApiResource::Associations::HasOneRemoteObjectProxy

Inherits:
SingleObjectProxy show all
Defined in:
lib/api_resource/associations/has_one_remote_object_proxy.rb

Instance Attribute Summary

Attributes inherited from AssociationProxy

#finder_opts, #klass, #owner, #remote_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SingleObjectProxy

#==, #collection?, #eql?, #hash, #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 id of the associated objecgt



6
7
8
9
10
11
12
13
14
15
# File 'lib/api_resource/associations/has_one_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 <<-EOE, __FILE__, __LINE__ + 1
    def #{id_method_name}
      self.#{assoc_name}? ? self.#{assoc_name}.id : nil
    end
  EOE
  super
end

Instance Method Details

#internal_objectObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/api_resource/associations/has_one_remote_object_proxy.rb', line 17

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? && self.owner.try(:id).present?
    self.remote_path = self.klass.collection_path(
      self.owner.class.to_s.foreign_key => self.owner.id
    )
  end
  super
end