Sha256: f53a69655dd22ccf7fba8a52a4ca709f240e84d2316c5be99195c09804b723b2

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module JsonApiClient
  module Helpers
    module Linkable
      extend ActiveSupport::Concern

      included do
        class_attribute :linker
        self.linker = Linking::Links

        # the links for this resource
        attr_accessor :links

        # reference to all of the preloaded data
        attr_accessor :linked_data

        initializer do |obj, params|
          links = params && params.delete("links")
          links ||= {}
          obj.links = obj.linker.new(links)
        end
      end

      def as_link
        {
          :type => self.class.table_name,
          primary_key => self[primary_key]
        }
      end

      def attributes
        super.tap do |attrs|
          attrs.merge!(links: links.attributes) if links.present?
        end
      end

      def method_missing(method, *args)
        return super unless links && links.has_attribute?(method)
        linked_data.data_for(method, links[method])
      end

      def respond_to_missing?(symbol, include_all = false)
        return true if links && links.has_attribute?(symbol)
        super
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_api_client-1.0.0.beta lib/json_api_client/helpers/linkable.rb