Module: FetcheableOnApi::Sortable
- Defined in:
- lib/fetcheable_on_api/sortable.rb
Overview
Sortable implements support for JSONAPI-compliant sorting via sort query parameters.
This module enables controllers to process sort parameters in the format:
sort=field1,-field2,+field3 where:
- No prefix or
+prefix means ascending order -prefix means descending order- Multiple fields are comma-separated and applied in order
It supports:
- Single and multiple field sorting
- Ascending and descending sort directions
- Association sorting with custom class names
- Case-insensitive sorting with the
loweroption - Field aliasing for different database column names
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- SORT_ORDER =
Maps sort direction prefixes to Arel sort methods. Used to parse the sort parameter and determine ascending vs descending order.
{ '+' => :asc, # Explicit ascending (same as no prefix) '-' => :desc, # Explicit descending }.freeze
Class Method Summary collapse
-
.included(base) ⇒ Object
Hook called when Sortable is included in a class.
Class Method Details
.included(base) ⇒ Object
Hook called when Sortable is included in a class. Sets up the class to support sort configuration and provides the sort_by class method.
76 77 78 79 80 81 82 83 |
# File 'lib/fetcheable_on_api/sortable.rb', line 76 def self.included(base) base.class_eval do extend ClassMethods # Store sort configurations per class to avoid conflicts between controllers class_attribute :sorts_configuration, instance_writer: false self.sorts_configuration = {} end end |