Class: Graphiti::Adapters::ActiveRecord::ManyToManySideload

Inherits:
Sideload::ManyToMany show all
Defined in:
lib/graphiti/adapters/active_record/many_to_many_sideload.rb

Constant Summary

Constants inherited from Sideload

Sideload::HOOK_ACTIONS, Sideload::TYPES

Instance Attribute Summary

Attributes inherited from Sideload

#group_name, #link, #name, #parent, #parent_resource_class, #polymorphic_as

Instance Method Summary collapse

Methods inherited from Sideload::ManyToMany

#apply_belongs_to_many_filter, #assign_each, #base_filter, #performant_assign?, #through, #true_foreign_key, #type

Methods inherited from Sideload::HasMany

#base_filter, #initialize, #link_filter, #load_params, #type

Methods inherited from Sideload

after_save, #always_include_resource_ids?, assign, #assign, assign_each, #assign_each, #associate, #associate_all, #association_name, #base_scope, #build_resource_proxy, #clear_resources, #create_remote_resource, #description, #disassociate, #errors, #fire_hooks!, #foreign_key, hooks, #initialize, link, #link?, #link_extra_fields, #link_filter, #load, #load_params, params, #parent_resource, #performant_assign?, #polymorphic_child?, #polymorphic_has_many?, #polymorphic_has_one?, #polymorphic_parent?, pre_load, #primary_key, #readable?, #remote?, #resolve, #resource, #resource_class, #resource_class_loaded?, #scope, scope, #shared_remote?, #single?, #type, #writable?

Constructor Details

This class inherits a constructor from Graphiti::Sideload::HasMany

Instance Method Details

#belongs_to_many_filter(scope, value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
# 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_filterObject



11
12
13
14
15
16
17
18
19
20
21
# 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_nameObject



7
8
9
# File 'lib/graphiti/adapters/active_record/many_to_many_sideload.rb', line 7

def through_relationship_name
  foreign_key.keys.first
end

#through_table_nameObject



2
3
4
5
# 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