Sha256: 53a1d6ab04016711287a638ba5976f9b0c47252fb07926ce46626776d02037e4

Contents?: true

Size: 1.02 KB

Versions: 49

Compression:

Stored size: 1.02 KB

Contents

class Graphiti::Sideload::HasMany < Graphiti::Sideload
  def initialize(name, opts)
    @inverse_filter = opts[:inverse_filter]

    super(name, opts)
  end

  def type
    :has_many
  end

  def inverse_filter
    @inverse_filter || foreign_key
  end

  def load_params(parents, query)
    query.hash.tap do |hash|
      hash[:filter] ||= {}
      hash[:filter].merge!(base_filter(parents))
    end
  end

  def base_filter(parents)
    {foreign_key => parent_filter(parents)}
  end

  def link_filter(parents)
    {inverse_filter => parent_filter(parents)}
  end

  private

  def parent_filter(parents)
    ids_for_parents(parents).join(",")
  end

  def child_map(children)
    children.group_by(&foreign_key)
  end

  def children_for(parent, map)
    pk = parent.send(primary_key)
    children = map[pk]
    return children if children

    keys = map.keys
    if pk.is_a?(String) && keys[0].is_a?(Integer)
      pk = pk.to_i
    elsif pk.is_a?(Integer) && keys[0].is_a?(String)
      pk = pk.to_s
    end
    map[pk] || []
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
graphiti-1.7.6 lib/graphiti/sideload/has_many.rb
graphiti-1.7.5 lib/graphiti/sideload/has_many.rb
graphiti-1.7.4 lib/graphiti/sideload/has_many.rb
graphiti-1.7.3 lib/graphiti/sideload/has_many.rb
graphiti-1.7.2 lib/graphiti/sideload/has_many.rb
graphiti-1.7.1 lib/graphiti/sideload/has_many.rb
graphiti-1.7.0 lib/graphiti/sideload/has_many.rb
graphiti-1.6.4 lib/graphiti/sideload/has_many.rb
graphiti-1.6.3 lib/graphiti/sideload/has_many.rb
graphiti-1.6.2 lib/graphiti/sideload/has_many.rb
graphiti-1.6.1 lib/graphiti/sideload/has_many.rb
graphiti-1.6.0 lib/graphiti/sideload/has_many.rb
graphiti-1.5.3 lib/graphiti/sideload/has_many.rb
graphiti-1.5.2 lib/graphiti/sideload/has_many.rb
graphiti-1.5.1 lib/graphiti/sideload/has_many.rb
graphiti-1.5.0 lib/graphiti/sideload/has_many.rb
graphiti-1.4.0 lib/graphiti/sideload/has_many.rb
graphiti-1.3.9 lib/graphiti/sideload/has_many.rb
graphiti-1.3.8 lib/graphiti/sideload/has_many.rb
graphiti-1.3.7 lib/graphiti/sideload/has_many.rb