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_valuesObject



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_valuesObject

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

Raises:

  • (StandardError)


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_valuesObject



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

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)
end

#filter_scopes_values_from(scopes) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 109

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?

  filter_tree_from(scopes_values)
end

#filter_scopes_values_from_parent(scope) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 93

def filter_scopes_values_from_parent(scope)
  scopes_values = []
  scope.children.each do |child|
    unless child.children
      scopes_values << TreePoint.new(child.id.to_s, translated_attribute(child.name, current_participatory_space.organization))
      next
    end
    scopes_values << TreeNode.new(
      TreePoint.new(child.id.to_s, translated_attribute(child.name, current_participatory_space.organization)),
      scope_children_to_tree(child)
    )
  end

  filter_tree_from(scopes_values)
end

#filter_tree_from(scopes_values) ⇒ Object



134
135
136
137
138
139
# File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 134

def filter_tree_from(scopes_values)
  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



122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/decidim/check_boxes_tree_helper.rb', line 122

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