class Graphiti::Adapters::ActiveRecord::ManyToManySideload

Public Instance Methods

belongs_to_many_filter(scope, value) click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 23
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
ids_for_parents(parents) click to toggle source
Calls superclass method Graphiti::Sideload#ids_for_parents
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 37
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
inverse_filter() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 11
def inverse_filter
  return @inverse_filter if @inverse_filter

  inferred_name = infer_inverse_association

  if inferred_name
    "#{inferred_name.to_s.singularize}_id"
  else
    super
  end
end
through_relationship_name() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 7
def through_relationship_name
  foreign_key.keys.first
end
through_table_name() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 2
def through_table_name
  @through_table_name ||= parent_resource_class.model
    .reflections[through.to_s].klass.table_name
end

Private Instance Methods

belongs_to_many_clause(value, type) click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 55
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
filter_for(scope, value, type = nil) click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 49
def filter_for(scope, value, type = nil)
  scope
    .includes(through_relationship_name)
    .where(belongs_to_many_clause(value, type))
end
foreign_type_column() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 63
def foreign_type_column
  through_reflection.type
end
foreign_type_value() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 67
def foreign_type_value
  through_reflection.active_record.name
end
infer_foreign_key() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 85
def infer_foreign_key
  key = parent_reflection.options[:through]
  value = through_reflection.foreign_key.to_sym
  {key => value}
end
infer_inverse_association() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 91
def infer_inverse_association
  through_class = through_reflection.klass

  foreign_reflection = through_class.reflections[name.to_s.singularize]
  foreign_reflection && foreign_reflection.options[:inverse_of]
end
parent_reflection() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 80
def parent_reflection
  parent_model = parent_resource_class.model
  parent_model.reflections[association_name.to_s]
end
polymorphic?() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 71
def polymorphic?
  !!foreign_type_column
end
through_reflection() click to toggle source
# File lib/graphiti/adapters/active_record/many_to_many_sideload.rb, line 75
def through_reflection
  through = parent_reflection.options[:through]
  parent_resource_class.model.reflections[through.to_s]
end