Sha256: 38eb2cec985e2c01455c121f76044171d6489baab6c53d6fb578426edd5a6bfd

Contents?: true

Size: 842 Bytes

Versions: 4

Compression:

Stored size: 842 Bytes

Contents

module JsonApiClient
  module Associations
    module BelongsTo
      extend ActiveSupport::Concern

      module ClassMethods
        def belongs_to(attr_name, options = {})
          # self.associations = self.associations + [HasOne::Association.new(attr_name, self, options)]
          self.associations += [BelongsTo::Association.new(attr_name, self, options)]
        end
      end

      class Association < BaseAssociation
        include Helpers::URI
        def param
          :"#{attr_name}_id"
        end

        def to_prefix_path(formatter)
          "#{formatter.format(attr_name.to_s.pluralize)}/%{#{param}}"
        end

        def set_prefix_path(attrs, formatter)
          attrs[param] = encode_part(attrs[param]) if attrs.key?(param)
          to_prefix_path(formatter) % attrs
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
json_api_client-1.6.1 lib/json_api_client/associations/belongs_to.rb
json_api_client-1.6.0 lib/json_api_client/associations/belongs_to.rb
json_api_client-1.5.3 lib/json_api_client/associations/belongs_to.rb
json_api_client-1.5.2 lib/json_api_client/associations/belongs_to.rb