Module: Decidim::CheckBoxesTreeHelper

Includes:
SanitizeHelper
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

Methods included from SanitizeHelper

#decidim_escape_translated, #decidim_html_escape, #decidim_sanitize, #decidim_sanitize_admin, #decidim_sanitize_editor, #decidim_sanitize_editor_admin, #decidim_sanitize_newsletter, #decidim_sanitize_translated, #decidim_url_escape, included

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 10

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:
    }
  }
  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



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

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



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

def filter_categories_values
  sorted_main_categories = current_participatory_space.categories.first_class.includes(:subcategories).sort_by do |category|
    [category.weight, decidim_escape_translated(category.name)]
  end

  categories_values = sorted_main_categories.flat_map do |category|
    sorted_descendant_categories = category.descendants.includes(:subcategories).sort_by do |subcategory|
      [subcategory.weight, decidim_escape_translated(subcategory.name)]
    end

    subcategories = sorted_descendant_categories.flat_map do |subcategory|
      TreePoint.new(subcategory.id.to_s, decidim_escape_translated(subcategory.name))
    end

    TreeNode.new(
      TreePoint.new(category.id.to_s, decidim_escape_translated(category.name)),
      subcategories
    )
  end

  TreeNode.new(
    TreePoint.new("", t("decidim.core.application_helper.filter_category_values.all")),
    categories_values
  )
end

#filter_global_scopes_valuesObject



94
95
96
# File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 94

def filter_global_scopes_values
  filter_scopes_values_from(current_organization.scopes.top_level)
end

#filter_origin_valuesObject

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

Raises:

  • (StandardError)


48
49
50
# File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 48

def filter_origin_values
  raise StandardError, "Not implemented"
end

#filter_scopes_valuesObject



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

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



132
133
134
# File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 132

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

#filter_tree_from_array(array) ⇒ Object



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

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



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

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



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

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