Sha256: 88fa5a95302d8a238b96578c0e884a3efb4ca09afd4b43cf8d4bc2a342605161

Contents?: true

Size: 1.01 KB

Versions: 17

Compression:

Stored size: 1.01 KB

Contents

class Graphiti::Sideload::BelongsTo < Graphiti::Sideload
  def type
    :belongs_to
  end

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

  def load(parents, query)
    if ids_for_parents(parents).empty?
      []
    else
      super
    end
  end

  def base_filter(parents)
    parent_ids = ids_for_parents(parents)
    { primary_key => parent_ids.join(',') }
  end

  def ids_for_parents(parents)
    parent_ids = parents.map(&foreign_key)
    parent_ids.compact!
    parent_ids.uniq!
    parent_ids
  end

  def infer_foreign_key
    if polymorphic_child?
      parent.foreign_key
    else
      model = resource.model
      namespace = namespace_for(model)
      model_name = model.name.gsub("#{namespace}::", '')
      :"#{model_name.underscore}_id"
    end
  end

  private

  def child_map(children)
    children.index_by(&primary_key)
  end

  def children_for(parent, map)
    map[parent.send(foreign_key)]
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
graphiti-1.0.beta.5 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.beta.4 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.beta.3 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.beta.2 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.26 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.25 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.24 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.23 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.22 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.21 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.20 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.19 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.18 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.17 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.16 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.15 lib/graphiti/sideload/belongs_to.rb
graphiti-1.0.alpha.14 lib/graphiti/sideload/belongs_to.rb