Module: Decidim::Admin::FilterableHelper

Defined in:
decidim-admin/app/helpers/decidim/admin/filterable_helper.rb

Overview

Helper that provides methods related to Decidim::Admin::Filterable concern.

Instance Method Summary collapse

Instance Method Details

#admin_filter_selector(i18n_ctx = nil) ⇒ Object

Renders the filters selector with tags in the admin panel.


8
9
10
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 8

def admin_filter_selector(i18n_ctx = nil)
  render partial: "decidim/admin/shared/filters", locals: { i18n_ctx: i18n_ctx }
end

#applied_filter_tag(filter, value, i18n_scope) ⇒ Object


106
107
108
109
110
111
112
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 106

def applied_filter_tag(filter, value, i18n_scope)
  (:span, class: "label secondary") do
    concat "#{i18n_filter_label(filter, i18n_scope)}: "
    concat i18n_filter_value(filter, value, i18n_scope)
    concat remove_filter_icon_link(filter)
  end
end

#applied_filters_hidden_field_tagsObject


89
90
91
92
93
94
95
96
97
98
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 89

def applied_filters_hidden_field_tags
  html = []
  html += ransack_params.slice(*filters, *extra_filters).map do |filter, value|
    hidden_field_tag("q[#{filter}]", value)
  end
  html += query_params.slice(*extra_allowed_params).map do |filter, value|
    hidden_field_tag(filter, value)
  end
  html.join.html_safe
end

#applied_filters_tags(i18n_ctx) ⇒ Object


100
101
102
103
104
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 100

def applied_filters_tags(i18n_ctx)
  ransack_params.slice(*filters).map do |filter, value|
    applied_filter_tag(filter, value, filterable_i18n_scope_from_ctx(i18n_ctx))
  end.join.html_safe
end

#build_submenu_options_tree_from_array(filter, values, i18n_scope) ⇒ Object

Builds a tree of links from an array. The tree will have only one level.


28
29
30
31
32
33
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 28

def build_submenu_options_tree_from_array(filter, values, i18n_scope)
  links = []
  links += extra_dropdown_submenu_options_items(filter, i18n_scope)
  links += values.map { |value| filter_link_value(filter, value, i18n_scope) }
  links.index_with { nil }
end

#build_submenu_options_tree_from_hash(filter, values, i18n_scope) ⇒ Object

Builds a tree of links from an Hash. The tree can have many levels.


42
43
44
45
46
47
48
49
50
51
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 42

def build_submenu_options_tree_from_hash(filter, values, i18n_scope)
  values.each_with_object({}) do |(key, value), hash|
    link = filter_link_value(filter, key, i18n_scope)
    hash[link] = if value.nil?
                   nil
                 elsif value.is_a?(Hash)
                   build_submenu_options_tree_from_hash(filter, value, i18n_scope)
                 end
  end
end

Produces the html for the dropdown submenu from the options tree. Returns a ActiveSupport::SafeBuffer.


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

def dropdown_submenu(options)
  (:ul, class: "vertical menu") do
    options.map do |key, value|
      if value.nil?
        (:li, key)
      elsif value.is_a?(Hash)
        (:li, class: "is-dropdown-submenu-parent") do
          key + dropdown_submenu(value)
        end
      end
    end.join.html_safe
  end
end

#extra_dropdown_submenu_options_items(_filter, _i18n_scope) ⇒ Object

To be overriden. Useful for adding links that do not match with the filter. Must return an Array.


37
38
39
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 37

def extra_dropdown_submenu_options_items(_filter, _i18n_scope)
  []
end

69
70
71
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 69

def filter_link_label(filter, i18n_scope)
  link_to(i18n_filter_label(filter, i18n_scope), href: "#")
end

73
74
75
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 73

def filter_link_value(filter, value, i18n_scope)
  link_to(i18n_filter_value(filter, value, i18n_scope), query_params_with(filter => value))
end

#filterable_i18n_scope_from_ctx(i18n_ctx) ⇒ Object


123
124
125
126
127
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 123

def filterable_i18n_scope_from_ctx(i18n_ctx)
  i18n_scope = "decidim.admin.filters"
  i18n_scope += ".#{i18n_ctx}" if i18n_ctx
  i18n_scope
end

#i18n_filter_label(filter, i18n_scope) ⇒ Object


77
78
79
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 77

def i18n_filter_label(filter, i18n_scope)
  t("#{i18n_scope}.#{filter}.label")
end

#i18n_filter_value(filter, value, i18n_scope) ⇒ Object


81
82
83
84
85
86
87
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 81

def i18n_filter_value(filter, value, i18n_scope)
  if I18n.exists?("#{i18n_scope}.#{filter}.values.#{value}")
    t(value, scope: "#{i18n_scope}.#{filter}.values")
  else
    find_dynamic_translation(filter, value)
  end
end

114
115
116
117
118
119
120
121
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 114

def remove_filter_icon_link(filter)
  icon_link_to(
    "circle-x",
    url_for(query_params_without(filter)),
    t("decidim.admin.actions.cancel"),
    class: "action-icon--remove"
  )
end

Builds a tree of links from Decidim::Admin::Filterable::filters_with_values


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'decidim-admin/app/helpers/decidim/admin/filterable_helper.rb', line 13

def submenu_options_tree(i18n_ctx = nil)
  i18n_scope = filterable_i18n_scope_from_ctx(i18n_ctx)

  filters_with_values.each_with_object({}) do |(filter, values), hash|
    link = filter_link_label(filter, i18n_scope)
    hash[link] = case values
                 when Array
                   build_submenu_options_tree_from_array(filter, values, i18n_scope)
                 when Hash
                   build_submenu_options_tree_from_hash(filter, values, i18n_scope)
                 end
  end
end