Module: Decidim::CheckBoxesTreeHelper
  
  
  
  
  
  
  
  
  
  
  
  
    - Defined in:
- app/helpers/decidim/check_boxes_tree_helper.rb
 
Overview
  
    
This helper include some methods for rendering a checkboxes tree input.
   
 
  
Defined Under Namespace
  
    
  
    
      Classes: TreeNode, TreePoint
    
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
    Instance Method Details
    
      
  
  
    #check_boxes_tree_options(value, label, **options)  ⇒ Object 
  
  
  
  
    
This method returns a hash with the options for the checkbox and its label used in filters that uses checkboxes trees
   
 
  
  
    | 
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | # File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 8
def check_boxes_tree_options(value, label, **options)
  checkbox_options = {
    value: value,
    label: label,
    multiple: true,
    include_hidden: false,
    label_options: {
      "data-children-checkbox": "",
      value: value
    }
  }
  options.merge!(checkbox_options)
  if options.delete(:is_root_check_box) == true
    options[:label_options].merge!("data-global-checkbox": "")
    options[:label_options].delete(:"data-children-checkbox")
  end
  options
end | 
 
    
      
  
  
    #filter_categories_values  ⇒ Object 
  
  
  
  
    | 
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 | # File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 49
def filter_categories_values
  organization = current_participatory_space.organization
  sorted_main_categories = current_participatory_space.categories.first_class.includes(:subcategories).sort_by do |category|
    [category.weight, translated_attribute(category.name, organization)]
  end
  categories_values = sorted_main_categories.flat_map do |category|
    sorted_descendant_categories = category.descendants.includes(:subcategories).sort_by do |subcategory|
      [subcategory.weight, translated_attribute(subcategory.name, organization)]
    end
    subcategories = sorted_descendant_categories.flat_map do |subcategory|
      TreePoint.new(subcategory.id.to_s, translated_attribute(subcategory.name, organization))
    end
    TreeNode.new(
      TreePoint.new(category.id.to_s, translated_attribute(category.name, organization)),
      subcategories
    )
  end
  TreeNode.new(
    TreePoint.new("", t("decidim.proposals.application_helper.filter_category_values.all")),
    categories_values
  )
end | 
 
    
      
  
  
    #filter_origin_values  ⇒ Object 
  
  
  
  
    
Overwrite this method in your component helper to define origin values.
   
 
  
    | 
45
46
47 | # File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 45
def filter_origin_values
  raise StandardError, "Not implemented"
end | 
 
    
      
  
  
    #filter_scopes_values  ⇒ Object 
  
  
  
  
    | 
85
86
87
88
89
90
91
92
93 | # File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 85
def filter_scopes_values
  main_scopes = if current_component.scope.present?
                  [current_component.scope]
                else
                  current_participatory_space.scopes.top_level
                                             .includes(:scope_type, :children)
                end
  filter_scopes_values_from(main_scopes)
end | 
 
    
      
  
  
    #filter_scopes_values_from(scopes)  ⇒ Object 
  
  
  
  
    | 
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 | # File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 95
def filter_scopes_values_from(scopes)
  scopes_values = scopes.compact.flat_map do |scope|
    TreeNode.new(
      TreePoint.new(scope.id.to_s, translated_attribute(scope.name, current_participatory_space.organization)),
      scope_children_to_tree(scope)
    )
  end
  scopes_values.prepend(TreePoint.new("global", t("decidim.scopes.global"))) if current_participatory_space.scope.blank?
  TreeNode.new(
    TreePoint.new("", t("decidim.proposals.application_helper.filter_scope_values.all")),
    scopes_values
  )
end | 
 
    
      
  
  
    #resource_filter_scope_values(resource)  ⇒ Object 
  
  
  
  
    | 
77
78
79
80
81
82
83 | # File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 77
def resource_filter_scope_values(resource)
  if resource.is_a?(Scope)
    filter_scopes_values_from([resource])
  else
    filter_scopes_values
  end
end | 
 
    
      
  
  
    #scope_children_to_tree(scope)  ⇒ Object 
  
  
  
  
    | 
111
112
113
114
115
116
117
118
119
120
121 | # File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 111
def scope_children_to_tree(scope)
  return if scope.scope_type && scope.scope_type == current_participatory_space.try(:scope_type_max_depth)
  return unless scope.children.any?
  scope.children.includes(:scope_type, :children).flat_map do |child|
    TreeNode.new(
      TreePoint.new(child.id.to_s, translated_attribute(child.name, current_participatory_space.organization)),
      scope_children_to_tree(child)
    )
  end
end |