Module: Decidim::CheckBoxesTreeHelper

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
28
29
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 8

def check_boxes_tree_options(value, label, **options)
  parent_id = options.delete(:parent_id) || ""
  checkbox_options = {
    value:,
    label:,
    multiple: true,
    include_hidden: false,
    label_options: {
      "data-children-checkbox": parent_id,
      value:,
      class: "filter"
    }
  }
  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_areas_valuesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 99

def filter_areas_values
  areas_or_types = areas_for_select(current_organization)

  areas_values = if areas_or_types.first.is_a?(Decidim::Area)
                   filter_areas(areas_or_types)
                 else
                   filter_areas_and_types(areas_or_types)
                 end

  TreeNode.new(
    TreePoint.new("", t("decidim.core.application_helper.filter_area_values.all")),
    areas_values
  )
end

#filter_categories_valuesObject



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
76
77
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 51

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.core.application_helper.filter_category_values.all")),
    categories_values
  )
end

#filter_global_scopes_valuesObject



95
96
97
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 95

def filter_global_scopes_values
  filter_scopes_values_from(current_organization.scopes.top_level.includes(:scope_type, :children))
end

#filter_origin_valuesObject

Overwrite this method in your component helper to define origin values.

Raises:

  • (StandardError)


47
48
49
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 47

def filter_origin_values
  raise StandardError, "Not implemented"
end

#filter_scopes_valuesObject



87
88
89
90
91
92
93
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 87

def filter_scopes_values
  return filter_scopes_values_from_parent(current_component.scope) if current_component.scope.present?

  main_scopes = current_participatory_space.scopes.top_level
                                           .includes(:scope_type, :children)
  filter_scopes_values_from(main_scopes, current_participatory_space)
end

#filter_text_for(translation, id: nil) ⇒ Object



133
134
135
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 133

def filter_text_for(translation, id: nil)
  (:span, translation, id:).html_safe + (:span)
end

#filter_tree_from_array(array) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 114

def filter_tree_from_array(array)
  root_point = if array.first[0].blank?
                 TreePoint.new(*array.shift)
               else
                 TreePoint.new("", t("decidim.core.application_helper.filter_scope_values.all"))
               end
  TreeNode.new(
    root_point,
    array.map { |values| TreePoint.new(*values) }
  )
end

#flat_filter_values(*types, **options) ⇒ Object



126
127
128
129
130
131
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 126

def flat_filter_values(*types, **options)
  scope = options[:scope]
  types.map do |type|
    [type, t(type, scope:)]
  end
end

#resource_filter_scope_values(resource) ⇒ Object



79
80
81
82
83
84
85
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 79

def resource_filter_scope_values(resource)
  if resource.is_a?(Scope)
    filter_scopes_values_from([resource], current_participatory_space)
  else
    filter_scopes_values
  end
end