Sha256: 6eccb53c89f6b0cdbf490cf2cf34bf423f9dc838caab7e73b51eb3afcd3ce12c
Contents?: true
Size: 1.26 KB
Versions: 15
Compression:
Stored size: 1.26 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, graph_parent) 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) fk = parent.send(foreign_key) children = map[fk] return children if children keys = map.keys if fk.is_a?(String) && keys[0].is_a?(Integer) fk = fk.to_i elsif fk.is_a?(Integer) && keys[0].is_a?(String) fk = fk.to_s end map[fk] || [] end end
Version data entries
15 entries across 15 versions & 1 rubygems