Sha256: a49975f95026cc3186ecb63c725a8030f0cfb90407d7b45de1f8c5be4c2b1070

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

module ApiResource
  module Associations
    class HasManyRemoteObjectProxy < MultiObjectProxy

      # defines a method to get the foreign key
      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}
            @attributes_cache[:#{id_method_name}] ||= begin
              # check our attributes first, then go to the remote
              @attributes[:#{id_method_name}] || self.#{assoc_name}.collect(
                &:id
              )
            end
          end
        EOE
        super
      end

      protected
      # gets the foreign key name for a given association
      # e.g. service_ids
      def self.foreign_key_name(assoc_name)
        super(assoc_name).pluralize
      end

      public

      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

      protected

      # The method by which we get ids for the association
      # e.g. object_ids
      def association_id_method
        self.class.foreign_key_name(@options["name"])
      end


    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
api_resource-0.6.25 lib/api_resource/associations/has_many_remote_object_proxy.rb
api_resource-0.6.24 lib/api_resource/associations/has_many_remote_object_proxy.rb
api_resource-0.6.23 lib/api_resource/associations/has_many_remote_object_proxy.rb
api_resource-0.6.22 lib/api_resource/associations/has_many_remote_object_proxy.rb