Module: SnFilterable::FilteredHelper
- Included in:
- ChipsComponent, FilterCategoryComponent
- Defined in:
- lib/sn_filterable.rb
Overview
View helper for [Filtered].
Defaults the URL of [Filtered] calls to use ‘url_for`.
Instance Method Summary collapse
-
#add_filter_url(filtered_instance, filter_key, value, url: url_for) ⇒ String
Helper function of [Filtered#add_filter_url].
-
#clear_all_url(filtered_instance, url: url_for) ⇒ String
Helper function of [Filtered#clear_all_url].
-
#clear_filter_url(filtered_instance, url: url_for) ⇒ String
Helper function of [Filtered#clear_filter_url].
-
#clear_sort_url(filtered_instance, url: url_for) ⇒ String
Helper function of [Filtered#clear_sort_url].
-
#filtered_form_inputs(filtered_instance) ⇒ Object
Creates hidden inputs elements representing the current filter / sort values.
-
#filtered_form_with(filtered_instance, options = {}, &block) ⇒ Object
Creates a form tag for filtered instances.
-
#filtered_paginate(filtered_instance) ⇒ Object
Renders a Tailwind paginated pagination section for a filtered instance.
-
#remove_filter_url(filtered_instance, filter_key, url: url_for) ⇒ String
Helper function of [Filtered#remove_filter_url].
-
#remove_sub_filter_url(filtered_instance, filter_key, filter_value, url: url_for) ⇒ String
Helper function of [Filtered#remove_sub_filter_url].
-
#set_filter_url(filtered_instance, filter_key, value, url: url_for) ⇒ String
Helper function of [Filtered#set_filter_url].
-
#sort_url(filtered_instance, sort_key, order: nil, url: url_for, &block) ⇒ String
Helper function of [Filtered#sort_url].
-
#table_header_sort_link(filtered_instance, sort_key, scope: nil, html_options: nil, url: url_for, in_filterable_component: true, &block) ⇒ Object
Creates an anchor element using ‘sort_url` and the block.
Instance Method Details
#add_filter_url(filtered_instance, filter_key, value, url: url_for) ⇒ String
Helper function of [Filtered#add_filter_url]
40 41 42 |
# File 'lib/sn_filterable.rb', line 40 def add_filter_url(filtered_instance, filter_key, value, url: url_for) filtered_instance.add_filter_url(url, filter_key, value) end |
#clear_all_url(filtered_instance, url: url_for) ⇒ String
Helper function of [Filtered#clear_all_url]
83 84 85 |
# File 'lib/sn_filterable.rb', line 83 def clear_all_url(filtered_instance, url: url_for) filtered_instance.clear_all_url(url) end |
#clear_filter_url(filtered_instance, url: url_for) ⇒ String
Helper function of [Filtered#clear_filter_url]
67 68 69 |
# File 'lib/sn_filterable.rb', line 67 def clear_filter_url(filtered_instance, url: url_for) filtered_instance.clear_filter_url(url) end |
#clear_sort_url(filtered_instance, url: url_for) ⇒ String
Helper function of [Filtered#clear_sort_url]
75 76 77 |
# File 'lib/sn_filterable.rb', line 75 def clear_sort_url(filtered_instance, url: url_for) filtered_instance.clear_sort_url(url) end |
#filtered_form_inputs(filtered_instance) ⇒ Object
Creates hidden inputs elements representing the current filter / sort values. Useful for forms when we want to retain the user’s current filtering parameters.
When used on a form, this should be rendered first to avoid conflicts with other form inputs defined. Duplicate input name’s may exist but can be ignored as the last duplicate parameter occurrence takes precedence.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/sn_filterable.rb', line 131 def filtered_form_inputs(filtered_instance) inputs = sanitize("") filtered_instance.queries.each do |key, value| case value when String inputs << content_tag(:input, "", type: :hidden, name: key, value: value) when Hash value.each do |subitem| inputs << content_tag(:input, "", type: :hidden, name: "#{key}[#{subitem[0]}]", value: subitem[1]) end end end inputs end |
#filtered_form_with(filtered_instance, options = {}, &block) ⇒ Object
Creates a form tag for filtered instances. See [FilteredFormBuilder] for the helper methods
153 154 155 156 157 158 159 |
# File 'lib/sn_filterable.rb', line 153 def filtered_form_with(filtered_instance, = {}, &block) [:method] = :get [:builder] = FilteredFormBuilder [:filtered] = filtered_instance form_with , &block end |
#filtered_paginate(filtered_instance) ⇒ Object
Renders a Tailwind paginated pagination section for a filtered instance. Uses kaminari’s paginate
165 166 167 |
# File 'lib/sn_filterable.rb', line 165 def filtered_paginate(filtered_instance) paginate filtered_instance.items, filtered: filtered_instance end |
#remove_filter_url(filtered_instance, filter_key, url: url_for) ⇒ String
Helper function of [Filtered#remove_filter_url]
49 50 51 |
# File 'lib/sn_filterable.rb', line 49 def remove_filter_url(filtered_instance, filter_key, url: url_for) filtered_instance.remove_filter_url(url, filter_key) end |
#remove_sub_filter_url(filtered_instance, filter_key, filter_value, url: url_for) ⇒ String
Helper function of [Filtered#remove_sub_filter_url]
59 60 61 |
# File 'lib/sn_filterable.rb', line 59 def remove_sub_filter_url(filtered_instance, filter_key, filter_value, url: url_for) filtered_instance.remove_sub_filter_url(url, filter_key, filter_value) end |
#set_filter_url(filtered_instance, filter_key, value, url: url_for) ⇒ String
Helper function of [Filtered#set_filter_url]
30 31 32 |
# File 'lib/sn_filterable.rb', line 30 def set_filter_url(filtered_instance, filter_key, value, url: url_for) filtered_instance.set_filter_url(url, filter_key, value) end |
#sort_url(filtered_instance, sort_key, order: nil, url: url_for, &block) ⇒ String
Helper function of [Filtered#sort_url]
93 94 95 |
# File 'lib/sn_filterable.rb', line 93 def sort_url(filtered_instance, sort_key, order: nil, url: url_for, &block) filtered_instance.sort_url(url, sort_key, order, &block) end |
#table_header_sort_link(filtered_instance, sort_key, scope: nil, html_options: nil, url: url_for, in_filterable_component: true, &block) ⇒ Object
Creates an anchor element using ‘sort_url` and the block. Additionally appends a suffix containing a triangle symbol representing the direction of the sort if we are currently sorting the items of [Filtered] using the `sort_key`.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sn_filterable.rb', line 105 def table_header_sort_link(filtered_instance, sort_key, scope: nil, html_options: nil, url: url_for, in_filterable_component: true, &block) url, state = filtered_instance.sort_url(url, sort_key, scope: scope) = in_filterable_component ? { "@click": "filtersLoading = true", "class": "whitespace-nowrap" } : {} = .merge() if .present? suffix = sanitize case state when :asc " ▲" when :desc " ▼" else "" end link_to(capture(&block) + suffix, url, ) end |