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_link(key, menu_id) ⇒ Object
-
#dropdown_submenu(options, menu_id) ⇒ Object
Produces the html for the dropdown submenu from the options tree.
-
#extra_dropdown_submenu_options_items(_filter, _i18n_scope) ⇒ Object
To be overridden.
- #extract_html_value(html_string) ⇒ Object
- #filter_link_label(filter, i18n_scope) ⇒ Object
- #filter_link_value(filter, value, i18n_scope) ⇒ Object
- #filterable_i18n_scope_from_ctx(i18n_ctx) ⇒ Object
- #filtered_adjacent_paths(item, path_method) ⇒ Object
- #i18n_filter_label(filter, i18n_scope) ⇒ Object
- #i18n_filter_value(filter, value, i18n_scope) ⇒ Object
- #remove_all_filters_tag ⇒ 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: } end |
#applied_filter_tag(filter, value, i18n_scope) ⇒ Object
131 132 133 134 135 136 137 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 131 def applied_filter_tag(filter, value, i18n_scope) content_tag(:span, class: "label reverse") 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
109 110 111 112 113 114 115 116 117 118 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 109 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
120 121 122 123 124 125 126 127 128 129 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 120 def (i18n_ctx) = ransack_params.slice(*filters).map do |filter, value| applied_filter_tag(filter, value, filterable_i18n_scope_from_ctx(i18n_ctx)) end return if .blank? << remove_all_filters_tag if .count > 1 .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_link(key, menu_id) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 75 def dropdown_link(key, ) link_to("#", class: "dropdown__button", data: { controller: "dropdown", target: "dropdown-filters-#{}" }) do safe_join([ content_tag(:span) { extract_html_value(key) }, icon("arrow-right-s-line", class: "fill-secondary absolute right-2"), icon("arrow-right-s-line", class: "fill-current text-white absolute right-2") ]) end end |
#dropdown_submenu(options, menu_id) ⇒ Object
Produces the html for the dropdown submenu from the options tree. Returns a ActiveSupport::SafeBuffer.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 56 def (, ) css_classes = .starts_with?("top-") ? "dropdown" : "dropdown dropdown__right z-#{.length * 10}" content_tag(:ul, id: "dropdown-filters-#{}", class: css_classes, "aria-hidden": true) do .map do |key, value| if value.nil? content_tag(:li, class: "dropdown__item") do concat content_tag(:span, class: "dropdown__button") { key } end elsif value.is_a?(Hash) child_id = SecureRandom.uuid content_tag(:li, class: "dropdown__item") do dropdown_link(key, child_id) + (value, child_id) end end end.join.html_safe end end |
#extra_dropdown_submenu_options_items(_filter, _i18n_scope) ⇒ Object
To be overridden. 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 |
#extract_html_value(html_string) ⇒ Object
85 86 87 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 85 def extract_html_value(html_string) Nokogiri::HTML.fragment(html_string).at("a").text end |
#filter_link_label(filter, i18n_scope) ⇒ Object
89 90 91 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 89 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
93 94 95 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 93 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
154 155 156 157 158 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 154 def filterable_i18n_scope_from_ctx(i18n_ctx) i18n_scope = "decidim.admin.filters" i18n_scope += ".#{i18n_ctx}" if i18n_ctx i18n_scope end |
#filtered_adjacent_paths(item, path_method) ⇒ Object
160 161 162 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 160 def filtered_adjacent_paths(item, path_method) adjacent_items(item).transform_values(&method(path_method)) end |
#i18n_filter_label(filter, i18n_scope) ⇒ Object
97 98 99 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 97 def i18n_filter_label(filter, i18n_scope) t("#{i18n_scope}.#{filter}.label") end |
#i18n_filter_value(filter, value, i18n_scope) ⇒ Object
101 102 103 104 105 106 107 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 101 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_all_filters_tag ⇒ Object
139 140 141 142 143 144 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 139 def remove_all_filters_tag link_to(url_for(blank_query_params), class: "label bg-transparent") do concat t("decidim.admin.filters.remove_all") concat icon("delete-bin-line", aria_label: t("decidim.admin.filters.remove_all"), role: "img") end end |
#remove_filter_icon_link(filter) ⇒ Object
146 147 148 149 150 151 152 |
# File 'app/helpers/decidim/admin/filterable_helper.rb', line 146 def remove_filter_icon_link(filter) icon_link_to( "delete-bin-line", url_for(query_params_without(filter)), t("decidim.admin.actions.cancel") ) 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 |