Sha256: 69eeb9244494bb0ad5ed9c248259170727584a0884d707b0c7bd40bd56bccf80

Contents?: true

Size: 1.04 KB

Versions: 33

Compression:

Stored size: 1.04 KB

Contents

class Graphiti::Sideload::ManyToMany < Graphiti::Sideload::HasMany
  def type
    :many_to_many
  end

  def through
    foreign_key.keys.first
  end

  def true_foreign_key
    foreign_key.values.first
  end

  def base_filter(parents)
    {true_foreign_key => ids_for_parents(parents).join(",")}
  end

  def infer_foreign_key
    raise "You must explicitly pass :foreign_key for many-to-many relationships, or override in subclass to return a hash."
  end

  def performant_assign?
    false
  end

  # Override in subclass
  def polymorphic?
    false
  end

  def apply_belongs_to_many_filter
    self_ref = self
    fk_type = parent_resource_class.attributes[:id][:type]
    fk_type = :hash if polymorphic?
    resource_class.filter true_foreign_key, fk_type do
      eq do |scope, value|
        self_ref.belongs_to_many_filter(scope, value)
      end
    end
  end

  def assign_each(parent, children)
    children.select do |c|
      match = ->(ct) { ct.send(true_foreign_key) == parent.send(primary_key) }
      c.send(through).any?(&match)
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
graphiti-1.2.19 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.18 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.17 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.16 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.15 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.14 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.13 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.12 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.11 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.10 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.9 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.8 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.7 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.6 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.5 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.4 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.3 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.2 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.1 lib/graphiti/sideload/many_to_many.rb
graphiti-1.2.0 lib/graphiti/sideload/many_to_many.rb