Module: Decidim::Admin::FilterableHelper
- Defined in:
- app/helpers/decidim/admin/filterable_helper.rb
Overview
Helper that provides methods related to Decidim::Admin::Filterable concern.
Instance Method Summary collapse
-
#admin_filter_selector(i18n_ctx = nil) ⇒ Object
Renders the filters selector with tags in the admin panel.
- #applied_filter_tag(filter, value, i18n_scope) ⇒ Object
- #applied_filters_hidden_field_tags ⇒ Object
- #applied_filters_tags(i18n_ctx) ⇒ Object
-
#build_submenu_options_tree_from_array(filter, values, i18n_scope) ⇒ Object
Builds a tree of links from an array.
-
#build_submenu_options_tree_from_hash(filter, values, i18n_scope) ⇒ Object
Builds a tree of links from an Hash.
-
#dropdown_submenu(options) ⇒ Object
Produces the html for the dropdown submenu from the options tree.
-
#extra_dropdown_submenu_options_items(_filter, _i18n_scope) ⇒ Object
To be overriden.
- #filter_link_label(filter, i18n_scope) ⇒ Object
- #filter_link_value(filter, value, i18n_scope) ⇒ Object
- #filterable_i18n_scope_from_ctx(i18n_ctx) ⇒ Object
- #i18n_filter_label(filter, i18n_scope) ⇒ Object
- #i18n_filter_value(filter, value, i18n_scope) ⇒ Object
- #remove_filter_icon_link(filter) ⇒ Object
-
#submenu_options_tree(i18n_ctx = nil) ⇒ Object
Builds a tree of links from Decidim::Admin::Filterable::filters_with_values.
Instance Method Details
#admin_filter_selector(i18n_ctx = nil) ⇒ Object
Renders the filters selector with tags in the admin panel.
8 9 10 |
# File '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 'app/helpers/decidim/admin/filterable_helper.rb', line 106 def applied_filter_tag(filter, value, i18n_scope) content_tag(: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_tags ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 89 def 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 'app/helpers/decidim/admin/filterable_helper.rb', line 100 def (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 'app/helpers/decidim/admin/filterable_helper.rb', line 28 def (filter, values, i18n_scope) links = [] links += (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 'app/helpers/decidim/admin/filterable_helper.rb', line 42 def (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) (filter, value, i18n_scope) end end end |
#dropdown_submenu(options) ⇒ Object
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 'app/helpers/decidim/admin/filterable_helper.rb', line 55 def () content_tag(:ul, class: "vertical menu") do .map do |key, value| if value.nil? content_tag(:li, key) elsif value.is_a?(Hash) content_tag(:li, class: "is-dropdown-submenu-parent") do key + (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 'app/helpers/decidim/admin/filterable_helper.rb', line 37 def (_filter, _i18n_scope) [] end |
#filter_link_label(filter, i18n_scope) ⇒ Object
69 70 71 |
# File '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 |
#filter_link_value(filter, value, i18n_scope) ⇒ Object
73 74 75 |
# File '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 '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 '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 '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 |
#remove_filter_icon_link(filter) ⇒ Object
114 115 116 117 118 119 120 121 |
# File '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 |
#submenu_options_tree(i18n_ctx = nil) ⇒ Object
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 'app/helpers/decidim/admin/filterable_helper.rb', line 13 def (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 (filter, values, i18n_scope) when Hash (filter, values, i18n_scope) end end end |