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
-
#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) ⇒ 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) ⇒ Filtered
Returns a new instance of Filtered.
17 18 19 20 21 22 23 |
# File 'lib/models/filtered.rb', line 17 def initialize(model_class, items, queries, sort_name, sort_reversed) @model_class = model_class @items = items @queries = queries @sort_name = sort_name @sort_reversed = sort_reversed end |
Instance Attribute Details
#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
27 28 29 |
# File 'lib/models/filtered.rb', line 27 def active_filters? @queries["filter"].present? end |
#add_filter_url(url, key, value) ⇒ String
Adds a filter to the URL.
49 50 51 52 53 |
# File 'lib/models/filtered.rb', line 49 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.
98 99 100 |
# File 'lib/models/filtered.rb', line 98 def clear_all_url(url) clear_url(url, true, true) end |
#clear_filter_url(url) ⇒ String
Clears all filters from the URL.
82 83 84 |
# File 'lib/models/filtered.rb', line 82 def clear_filter_url(url) clear_url(url, true, false) end |
#clear_sort_url(url) ⇒ String
Clears sorting from the URL.
90 91 92 |
# File 'lib/models/filtered.rb', line 90 def clear_sort_url(url) clear_url(url, false, true) end |
#remove_filter_url(url, key) ⇒ String
Removes a filter from the URL.
60 61 62 63 64 |
# File 'lib/models/filtered.rb', line 60 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.
72 73 74 75 76 |
# File 'lib/models/filtered.rb', line 72 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.
37 38 39 40 41 |
# File 'lib/models/filtered.rb', line 37 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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/models/filtered.rb', line 115 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 |