Module: Toller

Extended by:
ActiveSupport::Concern
Defined in:
lib/toller.rb,
lib/toller/sort.rb,
lib/toller/filter.rb,
lib/toller/version.rb,
lib/toller/retriever.rb,
lib/toller/scope_resolver.rb,
lib/toller/sorts/scope_handler.rb,
lib/toller/sorts/column_handler.rb,
lib/toller/filters/mutators/date.rb,
lib/toller/filters/mutators/time.rb,
lib/toller/filters/scope_handler.rb,
lib/toller/filters/column_handler.rb,
lib/toller/filters/mutators/boolean.rb,
lib/toller/filters/mutators/integer.rb,
lib/toller/filters/mutators/datetime.rb,
lib/toller/filters/mutators/common/range.rb

Overview

Toller

Query param based filtering and sorting

Defined Under Namespace

Modules: Filters, ScopeResolver, Sorts Classes: Filter, Retriever, Sort

Constant Summary collapse

VALID_TYPES =

Returns the types filter_on/sort_on recognize for their type: option.

Returns:

  • (Array<Symbol>)

    the types filter_on/sort_on recognize for their type: option

%i[string text integer boolean date datetime time scope].freeze
VERSION =

Returns the current gem version.

Returns:

  • (String)

    the current gem version

"1.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.truthy(value) ⇒ Boolean

Coerces a raw string value into a boolean, using the same rule Toller's own type: :boolean filters use internally. Handy inside a model scope backing a type: :scope filter, which receives the raw param value unmutated.

Parameters:

  • value (String)

    the raw value

Returns:

  • (Boolean)

    true if value is one of "1", "t", "true", "y", "yes"; false otherwise



36
37
38
# File 'lib/toller.rb', line 36

def self.truthy(value)
  Filters::Mutators::Boolean.call(value)
end

Instance Method Details

#filter_param_keySymbol

Override in an including controller to change the query param Toller reads filters from.

Returns:

  • (Symbol)

    the query param key holding filter params



105
106
107
# File 'lib/toller.rb', line 105

def filter_param_key
  :filters
end

#filter_paramsHash

Filter params

Returns:

  • (Hash)

    the current request's filter params, keyed by filter parameter name



88
89
90
# File 'lib/toller.rb', line 88

def filter_params
  params.fetch(filter_param_key.to_sym, {}).transform_keys { |key| key.to_s.strip.downcase }
end

#retrieve(collection) ⇒ ActiveRecord::Relation

Applies every active filter/sort for the current request to collection.

Parameters:

  • collection (ActiveRecord::Relation)

    the collection to filter/sort

Returns:

  • (ActiveRecord::Relation)

    the filtered and sorted collection



45
46
47
# File 'lib/toller.rb', line 45

def retrieve(collection)
  Retriever.filter(collection, filter_params, sort_params, retrievals)
end

#sort_param_keySymbol

Override in an including controller to change the query param Toller reads sorts from.

Returns:

  • (Symbol)

    the query param key holding sort params



114
115
116
# File 'lib/toller.rb', line 114

def sort_param_key
  :sort
end

#sort_paramsArray<String>

Sort param split

Returns:

  • (Array<String>)

    the current request's sort params, e.g. ['-published_at', 'title']



96
97
98
# File 'lib/toller.rb', line 96

def sort_params
  params.fetch(sort_param_key.to_sym, "").split(",").map { |param| param.strip.downcase }.reject(&:empty?)
end