Sha256: f52c4727083814ee75d080acdf209dca95ce6c45761abc545774ff1197a7f46e

Contents?: true

Size: 1.77 KB

Versions: 30

Compression:

Stored size: 1.77 KB

Contents

# query methods should delegate to Relation
module Her
  module Model
    module Associations
      class Association
        # @private
        attr_accessor :params
        delegate :all, :order_by, :sort_by, :order, :sort, :limit, :skip, :where, to: :blank_relation
        
        # Required by Relation methods
        def build_request_path(params = {})
          build_association_path lambda { "#{@parent.request_path(@params.merge(params))}#{@opts[:path]}" }
        end
        
        # Required by Relation methods
        def method_for(meth)
          @parent.class.method_for(meth)
        end
        
        # Required by Relation methods
        def request(*args,&block)
          @parent.class.request(*args,&block)
        end
        
        # Required by Relation methods
        def new_collection(parsed_data)
          Her::Model::Attributes.initialize_collection(@klass, parsed_data)
        end
        
        # Properly format the attributes to post/put 
        def to_params(attributes)
          @parent.class.to_params(attributes,attributes)
        end
        
        # Reload the association from remote service
        def reload
          @klass.get(build_request_path, @params).tap do |result|
            @parent.attributes[@name] = result
          end
        end
        
        # Reset params when directly called on association class
        # def where(*args)
        #   blank_relation.params = {}
        #   blank_relation.where(*args)
        # end
        
        def blank_relation
          @blank_relation ||= Relation.new(self)
          @blank_relation.params = {}
          @blank_relation
        end
        
        def method_missing(name, *args, &block)
          blank_relation.send(name, *args, &block)
        end

      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
mno-enterprise-core-3.4.0 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.3.3 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.3.2 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.2.1 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.3.1 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.3.0 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.2.0 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.1.4 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.0.7 lib/her_extension/model/associations/association.rb
mno-enterprise-core-2.0.9 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.1.3 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.0.6 lib/her_extension/model/associations/association.rb
mno-enterprise-core-2.0.8 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.1.2 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.0.5 lib/her_extension/model/associations/association.rb
mno-enterprise-core-2.0.7 lib/her_extension/model/associations/association.rb
mno-enterprise-core-2.0.6 lib/her_extension/model/associations/association.rb
mno-enterprise-core-2.0.5 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.1.1 lib/her_extension/model/associations/association.rb
mno-enterprise-core-3.0.4 lib/her_extension/model/associations/association.rb