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

Methods included from SanitizeHelper

#decidim_escape_translated, #decidim_html_escape, #decidim_rich_text, #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
31
32
33
34
35
36
37
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 10

def check_boxes_tree_options(value, label, **options)
  parent_id = options.delete(:parent_id) || ""
  object = options.delete(:object)
  checkbox_options = {
    value:,
    label:,
    multiple: true,
    include_hidden: false,
    label_options: {
      "data-children-checkbox": parent_id,
      value:,
      for: "#{options[:namespace]}_#{options[:id]}"
    }
  }
  options.merge!(checkbox_options)
  # as taxonomies work with a nested array of values, we need to manually check if the value is checked
  matches = options[:id]&.match(/^with_any_taxonomies_([0-9]+)__taxonomy-([0-9_]+)/)
  if matches.present? && object.respond_to?(:with_any_taxonomies) && object.with_any_taxonomies&.dig(matches[1]).is_a?(Array)
    options[:checked] = object.with_any_taxonomies[matches[1]].include?(value.to_s)
  end

  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

pending initiatives removal



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 86

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_global_scopes_valuesObject

pending initiatives removal



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

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)


59
60
61
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 59

def filter_origin_values
  raise StandardError, "Not implemented"
end

#filter_taxonomy_values_children(children) ⇒ Object



71
72
73
74
75
76
77
78
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 71

def filter_taxonomy_values_children(children)
  children.map do |id, hash|
    TreeNode.new(
      TreePoint.new(id, decidim_escape_translated(hash[:taxonomy].name).html_safe),
      filter_taxonomy_values_children(hash[:children])
    )
  end
end

#filter_taxonomy_values_for(taxonomy_filter) ⇒ Object



63
64
65
66
67
68
69
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 63

def filter_taxonomy_values_for(taxonomy_filter)
  taxonomies = filter_taxonomy_values_children(taxonomy_filter.taxonomies)
  TreeNode.new(
    TreePoint.new(taxonomy_filter.root_taxonomy_id, t("decidim.core.application_helper.filter_taxonomy_values.all"), true),
    taxonomies
  )
end

#filter_text_for(translation, id: nil) ⇒ Object



120
121
122
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 120

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

#filter_tree_from_array(array) ⇒ Object



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

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



113
114
115
116
117
118
# File 'decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb', line 113

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