Class: Filtered

Inherits:
Object
  • Object
show all
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.

See Also:

  • Filterable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, items, queries, sort_name, sort_reversed, extra_params = {}) ⇒ Filtered

Returns a new instance of Filtered.

Parameters:

  • model_class (Class)

    The class of the ActiveRecord::Base subclass

  • items (ActiveRecord::Relation)

    The items sorted and filtered by [Filterable]

  • queries (Hash)

    A hash of the sorting / filtering parameters

  • sort_name (Symbol)

    The current sorting name

  • sort_reversed (Boolean)

    True when the current sorting order is reversed

  • extra_params (Hash) (defaults to: {})

    Optional hash of additional parameters to include in URLs



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_paramsObject

Returns the value of attribute extra_params.



10
11
12
# File 'lib/models/filtered.rb', line 10

def extra_params
  @extra_params
end

#itemsObject

Returns the value of attribute items.



10
11
12
# File 'lib/models/filtered.rb', line 10

def items
  @items
end

#queriesObject

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

Returns:

  • (Boolean)

    True if at least one filter is 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.

Parameters:

  • url (String)

    The URL to add the filter to

  • key (String)

    The filter’s key

  • value (String)

    The value to apply using the filter

Returns:

  • (String)

    the modified URL with the filter added



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.

Parameters:

  • url (String)

    The url to remove the sorting and filtering from

Returns:

  • (String)

    the modified URL with all filters and sorting removed



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.

Parameters:

  • url (String)

    The url to remove the filter from

Returns:

  • (String)

    the modified URL with all filters removed



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.

Parameters:

  • url (String)

    The url to remove the sorting parameters from

Returns:

  • (String)

    the modified URL with no defined sort



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.

Parameters:

  • url (String)

    The URL to remove the filter from

  • key (String)

    The filter’s key to remove

Returns:

  • (String)

    the modified URL with the filter removed



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.

Parameters:

  • url (String)

    The URL to remove the filter from

  • key (String)

    The filter’s key to match

  • value (String)

    The filter’s value to remove

Returns:

  • (String)

    the modified URL with the filter removed



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.

Parameters:

  • url (String)

    The URL to apply the filter to

  • key (String)

    The filter’s key

  • value (String)

    The value to apply using the filter

Returns:

  • (String)

    the modified URL with the filter applied



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.

Parameters:

  • url (String)

    The url to apply the sort parameters on

  • key (String)

    The sorting key to toggle

  • order (Symbol, nil) (defaults to: nil)

    Optional, sets the sorting order. If set to nil, will toggle the order instead.

Returns:

  • the value returned from the block. If no block is given, [Array(String, Symbol | nil)] is returned, which contains the URL and state respectively



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