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

#initialize, #link_filter, #load_params

Methods inherited from Graphiti::Sideload

after_save, #always_include_resource_ids?, assign, #assign, 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, #ids_for_parents, #initialize, link, #link?, #link_extra_fields, #link_filter, #load, #load_params, params, #parent_resource, #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?, #writable?

Constructor Details

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

Instance Method Details

#apply_belongs_to_many_filterObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/graphiti/sideload/many_to_many.rb', line 35

def apply_belongs_to_many_filter
  self_ref = self
  fk_type = parent_resource_class.attributes[:id][:type]
  fk_type = :hash if polymorphic?
  # Do not recreate if filter already exists
  unless resource_class.config[:filters].has_key?(inverse_filter.to_sym)
    resource_class.filter inverse_filter, fk_type do
      eq do |scope, value|
        self_ref.belongs_to_many_filter(scope, value)
      end
    end
  end
end

#assign_each(parent, children) ⇒ Object



49
50
51
52
53
54
# File 'lib/graphiti/sideload/many_to_many.rb', line 49

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



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

def base_filter(parents)
  {true_foreign_key => parent_filter(parents)}
end

#infer_foreign_keyObject



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

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

#inverse_filterObject



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

def inverse_filter
  @inverse_filter || true_foreign_key
end

#performant_assign?Boolean

Returns:

  • (Boolean)


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

def performant_assign?
  false
end

#polymorphic?Boolean

Override in subclass

Returns:

  • (Boolean)


31
32
33
# File 'lib/graphiti/sideload/many_to_many.rb', line 31

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