Module: FetcheableOnApi::Sortable::ClassMethods
- Defined in:
- lib/fetcheable_on_api/sortable.rb
Overview
Class methods made available to controllers when Sortable is included.
Instance Method Summary collapse
-
#sort_by(*attrs) ⇒ Object
Define one or more sortable attributes for the controller.
Instance Method Details
#sort_by(*attrs) ⇒ Object
Define one or more sortable attributes for the controller.
This method configures which model attributes can be sorted via query parameters and how those sorts should be processed.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/fetcheable_on_api/sortable.rb', line 119 def sort_by(*attrs) = attrs. .symbolize_keys! # Validate that only supported options are provided .assert_valid_keys(:as, :class_name, :lower, :association) # Create a new configuration hash to avoid modifying parent class config self.sorts_configuration = sorts_configuration.dup attrs.each do |attr| # Initialize default configuration for this attribute sorts_configuration[attr] ||= { as: attr } # Merge in the provided options, overriding defaults sorts_configuration[attr] = sorts_configuration[attr].merge() end end |