Module: Decidim::Initiatives::ApplicationHelper

Includes:
CheckBoxesTreeHelper
Defined in:
decidim-initiatives/app/helpers/decidim/initiatives/application_helper.rb

Overview

Custom helpers, scoped to the initiatives engine.

Instance Method Summary collapse

Methods included from CheckBoxesTreeHelper

#check_boxes_tree_options, #filter_categories_values, #filter_origin_values, #filter_scopes_values_from, #filter_scopes_values_from_parent, #filter_tree_from, #resource_filter_scope_values

Instance Method Details

#filter_areas(areas) ⇒ Object


84
85
86
87
88
89
90
# File 'decidim-initiatives/app/helpers/decidim/initiatives/application_helper.rb', line 84

def filter_areas(areas)
  areas.map do |area|
    TreeNode.new(
      TreePoint.new(area.id.to_s, area.name[I18n.locale.to_s])
    )
  end
end

#filter_areas_and_types(area_types) ⇒ Object


92
93
94
95
96
97
98
99
100
101
# File 'decidim-initiatives/app/helpers/decidim/initiatives/application_helper.rb', line 92

def filter_areas_and_types(area_types)
  area_types.map do |area_type|
    TreeNode.new(
      TreePoint.new(area_type.area_ids.join("_"), area_type.name[I18n.locale.to_s]),
      area_type.areas.map do |area|
        TreePoint.new(area.id.to_s, area.name[I18n.locale.to_s])
      end
    )
  end
end

#filter_areas_valuesObject


69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'decidim-initiatives/app/helpers/decidim/initiatives/application_helper.rb', line 69

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.initiatives.application_helper.filter_area_values.all")),
    areas_values
  )
end

#filter_scopes_valuesObject


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'decidim-initiatives/app/helpers/decidim/initiatives/application_helper.rb', line 27

def filter_scopes_values
  main_scopes = current_organization.scopes.top_level

  scopes_values = main_scopes.includes(:scope_type, :children).flat_map do |scope|
    TreeNode.new(
      TreePoint.new(scope.id.to_s, translated_attribute(scope.name, current_organization)),
      scope_children_to_tree(scope)
    )
  end

  scopes_values.prepend(TreePoint.new("global", t("decidim.scopes.global")))

  TreeNode.new(
    TreePoint.new("", t("decidim.initiatives.application_helper.filter_scope_values.all")),
    scopes_values
  )
end

#filter_states_valuesObject


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'decidim-initiatives/app/helpers/decidim/initiatives/application_helper.rb', line 10

def filter_states_values
  TreeNode.new(
    TreePoint.new("", t("decidim.initiatives.application_helper.filter_state_values.all")),
    [
      TreePoint.new("open", t("decidim.initiatives.application_helper.filter_state_values.open")),
      TreeNode.new(
        TreePoint.new("closed", t("decidim.initiatives.application_helper.filter_state_values.closed")),
        [
          TreePoint.new("accepted", t("decidim.initiatives.application_helper.filter_state_values.accepted")),
          TreePoint.new("rejected", t("decidim.initiatives.application_helper.filter_state_values.rejected"))
        ]
      ),
      TreePoint.new("answered", t("decidim.initiatives.application_helper.filter_state_values.answered"))
    ]
  )
end

#filter_types_valuesObject


56
57
58
59
60
61
62
63
64
65
66
67
# File 'decidim-initiatives/app/helpers/decidim/initiatives/application_helper.rb', line 56

def filter_types_values
  types_values = Decidim::InitiativesType.where(organization: current_organization).map do |type|
    TreeNode.new(
      TreePoint.new(type.id.to_s, type.title[I18n.locale.to_s])
    )
  end

  TreeNode.new(
    TreePoint.new("", t("decidim.initiatives.application_helper.filter_type_values.all")),
    types_values
  )
end

#scope_children_to_tree(scope) ⇒ Object


45
46
47
48
49
50
51
52
53
54
# File 'decidim-initiatives/app/helpers/decidim/initiatives/application_helper.rb', line 45

def scope_children_to_tree(scope)
  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_organization)),
      scope_children_to_tree(child)
    )
  end
end