Sha256: 45970dd6e0f98b9e45764cb0ac8560948fe4f18fcbf674cb042519fcad0c9a5d

Contents?: true

Size: 1.26 KB

Versions: 24

Compression:

Stored size: 1.26 KB

Contents

module JsonApiClient
  module Relationships
    class Relations
      include Helpers::DynamicAttributes
      include Helpers::Dirty
      include ActiveModel::Serialization

      attr_reader :record_class
      delegate :key_formatter, to: :record_class

      def initialize(record_class, relations)
        @record_class = record_class
        self.attributes = relations
      end

      def present?
        attributes.present?
      end

      def as_json_api
        Hash[attributes_for_serialization.map do |k, v|
               [k, v.slice("data")]  if v.has_key?("data")
             end.compact]
      end

      def as_json
        Hash[attributes.map do |k, v|
               [k, v.slice("data")]  if v.has_key?("data")
             end.compact]
      end

      def attributes_for_serialization
        attributes.slice(*changed)
      end

      protected

      def set_attribute(name, value)
        value = case value
        when JsonApiClient::Resource
          {data: value.as_relation}
        when Array
          {data: value.map(&:as_relation)}
        when NilClass
          {data: nil}
        else
          value
        end
        attribute_will_change!(name) if value != attributes[name]
        attributes[name] = value
      end

    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
json_api_client-1.23.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.22.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.21.1 lib/json_api_client/relationships/relations.rb
json_api_client-1.21.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.20.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.19.0 lib/json_api_client/relationships/relations.rb
carwow-json_api_client-1.19.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.18.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.17.1 lib/json_api_client/relationships/relations.rb
json_api_client-1.17.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.16.1 lib/json_api_client/relationships/relations.rb
json_api_client-1.16.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.15.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.14.1 lib/json_api_client/relationships/relations.rb
json_api_client-1.14.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.13.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.12.2 lib/json_api_client/relationships/relations.rb
json_api_client-1.12.1 lib/json_api_client/relationships/relations.rb
json_api_client-1.12.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.11.0 lib/json_api_client/relationships/relations.rb