Module: SortableBy::Params::ClassMethods

Defined in:
lib/sortable_by/params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_sort_attributeObject

Returns the value of attribute default_sort_attribute.



6
7
8
# File 'lib/sortable_by/params.rb', line 6

def default_sort_attribute
  @default_sort_attribute
end

#default_sort_dirObject

Returns the value of attribute default_sort_dir.



6
7
8
# File 'lib/sortable_by/params.rb', line 6

def default_sort_dir
  @default_sort_dir
end

#sortable_mappingObject

Returns the value of attribute sortable_mapping.



6
7
8
# File 'lib/sortable_by/params.rb', line 6

def sortable_mapping
  @sortable_mapping
end

Instance Method Details

#sortable_by(*attributes, **options) ⇒ Object

Define attributes you wish to sort by

This optional configuration method tells the the sortable helpers how they should map attributes to queries. For example a “name” sort might need to sort on first and last name attributes

The ‘default` argument, if passed, will determine the attribute used when no sort param has been passed

Usage:

sortable_by :email, name: [:last_name, :first_name]

default: :email,
direction: :asc


25
26
27
28
29
# File 'lib/sortable_by/params.rb', line 25

def sortable_by(*attributes, **options)
  @default_sort_attribute = options.delete(:default)
  @default_sort_dir = options.delete(:direction) || :asc
  @sortable_mapping = options.merge(attributes.to_h { |att| [att, att] })
end