Sha256: 5843b8a2ed6cc56e41136be5c97a162995c0664686cd072d5aab4db3d505c4b5
Contents?: true
Size: 1.08 KB
Versions: 5
Compression:
Stored size: 1.08 KB
Contents
module ApiMe class Sorting attr_accessor :sort_criteria, :sort_reverse, :scope def initialize(scope:, sort_params:) self.scope = scope if sort_params self.sort_criteria = sort_params[:criteria] || default_sort_criteria self.sort_reverse = sort_params[:reverse] end end def results sorting? ? sort(sort_criteria).scope : scope end def sort_meta return {} unless sorting? { criteria: sort_criteria.is_blank? || sort_criteria === '' ? default_sort_criteria : sort_criteria, reverse: sort_reverse, record_count: scope.size, total_records: scope.total_count } end protected def sort(criteria = default_sort_criteria) self.scope = if sort_reverse === 'true' scope.order(criteria => :desc) else scope.order(criteria => :asc) end self end private def default_sort_criteria sort_criteria = 'id' end def sorting? sort_criteria || sort_reverse end end end
Version data entries
5 entries across 5 versions & 1 rubygems
Version | Path |
---|---|
api_me-0.9.4 | lib/api_me/sorting.rb |
api_me-0.9.3 | lib/api_me/sorting.rb |
api_me-0.9.2 | lib/api_me/sorting.rb |
api_me-0.9.1 | lib/api_me/sorting.rb |
api_me-0.9.0 | lib/api_me/sorting.rb |