Module: NestedSelect::Preloader

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Defined in:
lib/nested_select/preloader.rb,
lib/nested_select/preloader/branch.rb,
lib/nested_select/preloader/association.rb,
lib/nested_select/preloader/through_association.rb

Defined Under Namespace

Modules: Association, Branch, ThroughAssociation

Instance Method Summary collapse

Instance Method Details

#apply_nested_select_values(nested_select_values) ⇒ Object

first one will start from the roots [[{], included_2: [{}]}]



19
20
21
# File 'lib/nested_select/preloader.rb', line 19

def apply_nested_select_values(nested_select_values)
  distribute_nested_select_over_loading_tree(@tree, nested_select_values)
end

#distribute_nested_select_over_loading_tree(sub_tree, nested_select_values) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nested_select/preloader.rb', line 23

def distribute_nested_select_over_loading_tree(sub_tree, nested_select_values)
  # Ex: nested_select_values = [:id, :title, comments: [:id, :body], cover: [:id, img: [:url]]]
  return if nested_select_values.blank?

  sub_tree.nested_select_values = [*nested_select_values.grep_v(Hash)]
  # sub_nested_select_values = { comments: [:id, :body], cover: [:id, img: [:url]] }
  sub_nested_select_values = nested_select_values.grep(Hash).inject({}, &:merge)&.symbolize_keys

  # it could be a case when selection tree is not that deep than Branch tree.
  return if sub_nested_select_values.blank?

  # its possible to subselect in reverse direction for through relation's
  # in that case includes are implicit, but we need to add that reverse tree into nested select
  reverse_nested_selections = sub_nested_select_values.except(*sub_tree.children.map(&:association))
  # this is for reverse selection of through models
  sub_tree.nested_select_values << reverse_nested_selections if reverse_nested_selections.present?

  sub_tree.children.each do |chld_brnch|
    distribute_nested_select_over_loading_tree(chld_brnch, sub_nested_select_values[chld_brnch.association])
  end
end