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) ⇒ 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



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

#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



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.

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



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.

Parameters:

  • url (String)

    The url to remove the sorting and filtering from

Returns:

  • (String)

    the modified URL with all filters and sorting removed



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.

Parameters:

  • url (String)

    The url to remove the filter from

Returns:

  • (String)

    the modified URL with all filters removed



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.

Parameters:

  • url (String)

    The url to remove the sorting parameters from

Returns:

  • (String)

    the modified URL with no defined sort



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.

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



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.

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



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.

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



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.

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



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