Class: Filtered
- Inherits:
-
Object
- Object
- Filtered
- Defined in:
- lib/models/filtered.rb
Overview
Helper class that provides access to the filtered items and generation of sort/filter URLs for the items.
Should not be initialized directly, models should implement [Filterable] instead.
Instance Attribute Summary collapse
-
#extra_params ⇒ Object
Returns the value of attribute extra_params.
-
#items ⇒ Object
Returns the value of attribute items.
-
#queries ⇒ Object
Returns the value of attribute queries.
Instance Method Summary collapse
-
#active_filters? ⇒ Boolean
Returns if any filters are active.
-
#add_filter_url(url, key, value) ⇒ String
Adds a filter to the URL.
-
#clear_all_url(url) ⇒ String
Clears all filters and sorting from the URL.
-
#clear_filter_url(url) ⇒ String
Clears all filters from the URL.
-
#clear_sort_url(url) ⇒ String
Clears sorting from the URL.
-
#initialize(model_class, items, queries, sort_name, sort_reversed, extra_params = {}) ⇒ Filtered
constructor
A new instance of Filtered.
-
#remove_filter_url(url, key) ⇒ String
Removes a filter from the URL.
-
#remove_sub_filter_url(url, key, value) ⇒ String
Removes a sub filter from the URL.
-
#set_filter_url(url, key, value) ⇒ String
Sets one filter to the URL.
-
#sort_url(url, key, order = nil, scope: nil) ⇒ Object
Generates a URL used in table headers for column sorting.
Constructor Details
#initialize(model_class, items, queries, sort_name, sort_reversed, extra_params = {}) ⇒ Filtered
Returns a new instance of Filtered.
18 19 20 21 22 23 24 25 |
# File 'lib/models/filtered.rb', line 18 def initialize(model_class, items, queries, sort_name, sort_reversed, extra_params = {}) @model_class = model_class @items = items @queries = queries @sort_name = sort_name @sort_reversed = sort_reversed @extra_params = extra_params || {} end |
Instance Attribute Details
#extra_params ⇒ Object
Returns the value of attribute extra_params.
10 11 12 |
# File 'lib/models/filtered.rb', line 10 def extra_params @extra_params end |
#items ⇒ Object
Returns the value of attribute items.
10 11 12 |
# File 'lib/models/filtered.rb', line 10 def items @items end |
#queries ⇒ Object
Returns the value of attribute queries.
10 11 12 |
# File 'lib/models/filtered.rb', line 10 def queries @queries end |
Instance Method Details
#active_filters? ⇒ Boolean
Returns if any filters are active
29 30 31 |
# File 'lib/models/filtered.rb', line 29 def active_filters? @queries["filter"].present? end |
#add_filter_url(url, key, value) ⇒ String
Adds a filter to the URL.
51 52 53 54 55 |
# File 'lib/models/filtered.rb', line 51 def add_filter_url(url, key, value) modify_url_queries(url) do |queries| queries["filter"][key] = value end end |
#clear_all_url(url) ⇒ String
Clears all filters and sorting from the URL.
100 101 102 |
# File 'lib/models/filtered.rb', line 100 def clear_all_url(url) clear_url(url, true, true) end |
#clear_filter_url(url) ⇒ String
Clears all filters from the URL.
84 85 86 |
# File 'lib/models/filtered.rb', line 84 def clear_filter_url(url) clear_url(url, true, false) end |
#clear_sort_url(url) ⇒ String
Clears sorting from the URL.
92 93 94 |
# File 'lib/models/filtered.rb', line 92 def clear_sort_url(url) clear_url(url, false, true) end |
#remove_filter_url(url, key) ⇒ String
Removes a filter from the URL.
62 63 64 65 66 |
# File 'lib/models/filtered.rb', line 62 def remove_filter_url(url, key) modify_url_queries(url) do |queries| queries["filter"].delete(key) end.chomp("?") end |
#remove_sub_filter_url(url, key, value) ⇒ String
Removes a sub filter from the URL.
74 75 76 77 78 |
# File 'lib/models/filtered.rb', line 74 def remove_sub_filter_url(url, key, value) modify_url_queries(url) do |queries| queries["filter"][key].delete(value.to_s) if queries["filter"][key].is_a?(Array) end.chomp("?") end |
#set_filter_url(url, key, value) ⇒ String
Sets one filter to the URL.
39 40 41 42 43 |
# File 'lib/models/filtered.rb', line 39 def set_filter_url(url, key, value) modify_url_queries(url) do |queries| queries["filter"] = { key => value } end end |
#sort_url(url, key, order = nil, scope: nil) ⇒ Object
Generates a URL used in table headers for column sorting.
Calls and returns ‘block`, providing the following parameters in the following order:
- url: [String] The URL for the column sorting which links to the *next* sorting state
- state: [nil, Symbol] The *current* sorting state of the provided key. Provides the key's sorting order as a symbol, either `:asc`, or `:desc` when active. Returns `nil`, this sorting key is not active.
When the sorting key provided is active for this [Filtered] instance, this method will return a URL with the order reversed.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/models/filtered.rb', line 117 def sort_url(url, key, order = nil, scope: nil) state = nil url = modify_url_queries(url) do |queries| queries["sort"] = key if @sort_name == key state = @sort_reversed ? :desc : :asc queries["order"] = @sort_reversed ? "asc" : "desc" else queries.delete("order") end queries["order"] = order.to_s unless order.nil? queries["scope"] = scope.to_s unless scope.nil? end return yield(url, state) if block_given? [url, state] end |