Sha256: 11824c364ff680cbe64a0fead801916f288f639c57317010dc599c4658d224a5

Contents?: true

Size: 1.99 KB

Versions: 17

Compression:

Stored size: 1.99 KB

Contents

module Graphiti
  module Util
    # Helper methods for traversing the 'relationships' JSONAPI payloads
    # @api private
    class RelationshipPayload
      attr_reader :resource, :payload

      def self.iterate(resource:, relationships: {}, only: [], except: [])
        instance = new(resource, relationships, only: only, except: except)
        instance.iterate do |sideload, relationship_data, sub_relationships|
          yield sideload, relationship_data, sub_relationships
        end
      end

      def initialize(resource, payload, only: [], except: [])
        @resource = resource
        @payload  = payload
        @only     = only
        @except   = except
      end

      def iterate
        payload.each_pair do |relationship_name, relationship_payload|
          if sl = resource.class.sideload(relationship_name.to_sym)
            if should_yield?(sl.type)
              if relationship_payload.is_a?(Array)
                relationship_payload.each do |rp|
                  yield payload_for(sl, rp)
                end
              else
                yield payload_for(sl, relationship_payload)
              end
            end
          end
        end
      end

      private

      def should_yield?(type)
        (@only.length > 0 && @only.include?(type)) ||
          (@except.length > 0 && !@except.include?(type))
      end

      def payload_for(sideload, relationship_payload)
        if sideload.polymorphic_parent?
          type     = relationship_payload[:meta][:jsonapi_type]
          sideload = sideload.child_for_type(type.to_sym)
        end
        relationship_payload[:meta][:method] ||= :update

        {
          sideload: sideload,
          is_polymorphic: sideload.polymorphic_child?,
          primary_key: sideload.primary_key,
          foreign_key: sideload.foreign_key,
          attributes: relationship_payload[:attributes],
          meta: relationship_payload[:meta],
          relationships: relationship_payload[:relationships]
        }
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
graphiti-1.0.alpha.19 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.18 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.17 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.16 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.15 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.14 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.12 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.11 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.10 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.9 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.8 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.7 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.6 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.5 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.4 lib/graphiti/util/relationship_payload.rb
graphiti-1.0.alpha.1 lib/graphiti/util/relationship_payload.rb
graphiti-rb-1.0.alpha.1 lib/graphiti/util/relationship_payload.rb