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, #link_hides_primary_key?, #links_by_public_id?, #load_params, #register_public_id_source

Methods inherited from Graphiti::Sideload

after_save, assign, #assign, assign_each, assigned?, assigning_node, #associate, #associate_all, #association_name, #base_scope, #build_resource_proxy, #clear_resources, #collect_foreign_keys, #create_remote_resource, current_assigning_query, #customized_base_scope?, #default_link_mode, #default_render_resource_ids?, #description, #disassociate, #errors, #fire_hooks!, #foreign_key, #future_resolve, #guarded?, hooks, #ids_for_parents, #initialize, link, #link?, #link_extra_fields, #link_filter, #link_hides_primary_key?, #link_mode, #load, #load_params, #non_default_options, params, #parent_resource, #polymorphic_child?, #polymorphic_has_many?, #polymorphic_has_one?, #polymorphic_parent?, pre_load, #primary_key, #readable?, #readable_guard_name, #readable_guarded?, #register_public_id_source, #remote?, #render_resource_ids?, #rendered_id_for, #requested_link_mode, #resolve, #resource, #resource_class, #resource_class_loaded?, #resource_ids_blocker, #resource_ids_from_foreign_key?, scope, #scope, #shared_remote?, #single?, #sync_load, #sync_resolve, #writable?

Constructor Details

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

Instance Method Details

#apply_belongs_to_many_filter ⇒ Object



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, primary_keys:|
        self_ref.belongs_to_many_filter(scope, primary_keys)
      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 => internal_parent_filter(parents, true_foreign_key)}
end

#infer_foreign_key ⇒ Object



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_filter ⇒ Object



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

#through ⇒ Object



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

def through
  foreign_key.keys.first
end

#true_foreign_key ⇒ Object



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

def true_foreign_key
  foreign_key.values.first
end

#type ⇒ Object



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

def type
  :many_to_many
end