Sha256: a510cee81ffbbfaddd9a189e2e6ec95945ac9bf80e478a731cb3658abdbee6fc

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

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

      private

      def add_join_table_magic(proxy)
        if defined?(ActiveRecord) && proxy.resource.model.ancestors.include?(ActiveRecord::Base)
          thru = @sideload.foreign_key.keys.first
          thru_model = proxy.resource.model.reflect_on_association(thru).klass
          names = thru_model.column_names.map do |n|
            "#{thru_model.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

2 entries across 2 versions & 1 rubygems

Version Path
graphiti_gql-0.2.2 lib/graphiti_gql/loaders/many_to_many.rb
graphiti_gql-0.2.1 lib/graphiti_gql/loaders/many_to_many.rb