Class: Graphiti::Sideload::ManyToMany

Inherits:
HasMany show all
Defined in:
lib/graphiti/sideload/many_to_many.rb

Constant Summary

Constants inherited from Graphiti::Sideload

HOOK_ACTIONS, TYPES

Instance Attribute Summary

Attributes inherited from Graphiti::Sideload

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

Instance Method Summary collapse

Methods inherited from HasMany

#load_params

Methods inherited from Graphiti::Sideload

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

Constructor Details

This class inherits a constructor from Graphiti::Sideload

Instance Method Details

#apply_belongs_to_many_filterObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/graphiti/sideload/many_to_many.rb', line 31

def apply_belongs_to_many_filter
  self_ref = self
  fk_type = parent_resource_class.attributes[:id][:type]
  fk_type = :hash if polymorphic?
  resource_class.filter true_foreign_key, fk_type do
    eq do |scope, value|
      self_ref.belongs_to_many_filter(scope, value)
    end
  end
end

#assign_each(parent, children) ⇒ Object



42
43
44
45
46
47
# File 'lib/graphiti/sideload/many_to_many.rb', line 42

def assign_each(parent, children)
  children.select do |c|
    match = ->(ct) { ct.send(true_foreign_key) == parent.send(primary_key) }
    c.send(through).any?(&match)
  end
end

#base_filter(parents) ⇒ Object



14
15
16
# File 'lib/graphiti/sideload/many_to_many.rb', line 14

def base_filter(parents)
  {true_foreign_key => ids_for_parents(parents).join(",")}
end

#infer_foreign_keyObject



18
19
20
# File 'lib/graphiti/sideload/many_to_many.rb', line 18

def infer_foreign_key
  raise "You must explicitly pass :foreign_key for many-to-many relationships, or override in subclass to return a hash."
end

#performant_assign?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/graphiti/sideload/many_to_many.rb', line 22

def performant_assign?
  false
end

#polymorphic?Boolean

Override in subclass

Returns:

  • (Boolean)


27
28
29
# File 'lib/graphiti/sideload/many_to_many.rb', line 27

def polymorphic?
  false
end

#throughObject



6
7
8
# File 'lib/graphiti/sideload/many_to_many.rb', line 6

def through
  foreign_key.keys.first
end

#true_foreign_keyObject



10
11
12
# File 'lib/graphiti/sideload/many_to_many.rb', line 10

def true_foreign_key
  foreign_key.values.first
end

#typeObject



2
3
4
# File 'lib/graphiti/sideload/many_to_many.rb', line 2

def type
  :many_to_many
end