Sha256: cf4375a209916ae71bad2d725282deffa2635a5c97ed1c7db9b703db1c8115f0

Contents?: true

Size: 1.28 KB

Versions: 16

Compression:

Stored size: 1.28 KB

Contents

module GraphitiGql
  module Loaders
    class ManyToMany < Many
      def assign(parent_records, proxy)
        thru = @sideload.foreign_key.keys.first
        fk = @sideload.foreign_key[thru]
        add_join_table_magic(proxy)
        records = proxy.data
        parent_records.each do |pr|
          corresponding = records.select do |record|
            record.send(:"_edge_#{fk}") == pr.send(@sideload.primary_key)
          end
          fulfill(pr, [corresponding, proxy])
        end
      end

      private

      def thru_model
        thru = @sideload.foreign_key.keys.first
        reflection = @sideload.parent_resource.model.reflect_on_association(thru)
        reflection.klass
      end

      def add_join_table_magic(proxy)
        return unless @sideload.edge_magic
        if defined?(ActiveRecord) && proxy.resource.model.ancestors.include?(ActiveRecord::Base)
          thru_table_name = @sideload.join_table_alias || thru_model.table_name
          names = thru_model.column_names.map do |n|
            next if n == :id
            "#{thru_table_name}.#{n} as _edge_#{n}"
          end
          scope = proxy.scope.object
          scope = scope.select(["#{proxy.resource.model.table_name}.*"] + names)
          proxy.scope.object = scope
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
graphiti_gql-0.2.38 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.37 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.36 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.35 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.34 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.33 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.32 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.31 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.30 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.29 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.28 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.27 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.26 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.25 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.24 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.23 lib/graphiti_gql/loaders/many_to_many.rb