Sha256: d15d54c2167a1d1b99518e59314abfd210009fe0609da13611c1164316c7de32
Contents?: true
Size: 1.82 KB
Versions: 2
Compression:
Stored size: 1.82 KB
Contents
class Graphiti::Adapters::ActiveRecord::ManyToManySideload < Graphiti::Sideload::ManyToMany def through_table_name @through_table_name ||= parent_resource_class.model .reflections[through.to_s].klass.table_name end def through_relationship_name foreign_key.keys.first end def belongs_to_many_filter(scope, value) if polymorphic? clauses = value.group_by { |v| v["type"] }.map { |group| ids = group[1].map { |g| g["id"] } filter_for(scope, ids, group[0]) } scope = clauses.shift clauses.each { |c| scope = scope.or(c) } scope else filter_for(scope, value) end end def ids_for_parents(parents) if polymorphic? parents.group_by(&:class).map do |group| {id: super(group[1]), type: group[0].name}.to_json end else super end end private def filter_for(scope, value, type = nil) scope .preload(through_relationship_name) .joins(through_relationship_name) .where(belongs_to_many_clause(value, type)) end def belongs_to_many_clause(value, type) where = {true_foreign_key => value} if polymorphic? && type where[foreign_type_column] = type end {through_table_name => where} end def foreign_type_column through_reflection.type end def foreign_type_value through_reflection.active_record.name end def polymorphic? !!foreign_type_column end def through_reflection through = parent_reflection.options[:through] parent_resource_class.model.reflections[through.to_s] end def parent_reflection parent_model = parent_resource_class.model parent_model.reflections[association_name.to_s] end def infer_foreign_key key = parent_reflection.options[:through] value = through_reflection.foreign_key.to_sym {key => value} end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
graphiti-1.2.21 | lib/graphiti/adapters/active_record/many_to_many_sideload.rb |
graphiti-1.2.20 | lib/graphiti/adapters/active_record/many_to_many_sideload.rb |