Sha256: 97a800931b01e30a2aec505c1c9dfb46ed8359a8bf1886c6b558fb4f8e522842

Contents?: true

Size: 1.23 KB

Versions: 21

Compression:

Stored size: 1.23 KB

Contents

module ApiResource
  module Associations
    class HasOneRemoteObjectProxy < SingleObjectProxy
      
      # defines a method to get the id of the associated objecgt
      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

      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

      protected

      # because of how this works we use a multi object proxy and return the first element
      def to_condition
        ApiResource::Conditions::SingleObjectAssociationCondition.new(
          self.klass, self.remote_path
        )
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
api_resource-0.6.25 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.24 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.23 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.22 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.21 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.20 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.19 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.18 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.17 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.16 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.15 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.14 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.13 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.12 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.11 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.9 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.10 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.8 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.7 lib/api_resource/associations/has_one_remote_object_proxy.rb
api_resource-0.6.6 lib/api_resource/associations/has_one_remote_object_proxy.rb