Module: JSONAPIonify::Api::Resource::Definitions::Sorting
- Defined in:
- lib/jsonapionify/api/resource/definitions/sorting.rb
Class Method Summary collapse
Instance Method Summary collapse
- #default_sort(options) ⇒ Object
- #define_sorting_strategy(mod, &block) ⇒ Object
- #sort_attrs_from_sort(sort_string) ⇒ Object
- #sort_fields_from_sort_string(sort_string) ⇒ Object
Class Method Details
.extended(klass) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jsonapionify/api/resource/definitions/sorting.rb', line 7 def self.extended(klass) klass.class_eval do inherited_hash_attribute :sorting_strategies delegate :sort_fields_from_sort_string, to: :class # Define Contexts context :sorted_collection, readonly: true do |context| if context.root_request? _, block = sorting_strategies.to_a.reverse.to_h.find do |mod, _| Object.const_defined?(mod, false) && context.collection.class <= Object.const_get(mod, false) end context.reset(:sort_params) instance_exec(context.collection, context.sort_params, context, &block) else context.collection end end context(:sort_params, readonly: true, persisted: true) do |context| sort_fields_from_sort_string(context.params['sort']).tap do |fields| should_error = false fields.each do |field| unless self.class.field_valid?(field.name) || field.name == id_attribute should_error = true type = self.class.type error :sort_parameter_invalid do detail "resource `#{type}` does not have field: #{field.name}" end next end end halt if should_error end end define_sorting_strategy('Object') do |collection| collection end define_sorting_strategy('Enumerable') do |collection, fields| collection.to_a.deep_sort(fields.to_hash) end define_sorting_strategy('ActiveRecord::Relation') do |collection, fields| collection.reorder(fields.to_hash).order(self.class.id_attribute) end end end |
Instance Method Details
#default_sort(options) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/jsonapionify/api/resource/definitions/sorting.rb', line 61 def default_sort() string = case when Hash, Array .map do |k, v| v.to_s.downcase == 'asc' ? "-#{k}" : k.to_s end.join(',') else .to_s end param :sort, default: string, sticky: true end |
#define_sorting_strategy(mod, &block) ⇒ Object
57 58 59 |
# File 'lib/jsonapionify/api/resource/definitions/sorting.rb', line 57 def define_sorting_strategy(mod, &block) sorting_strategies[mod.to_s] = block end |
#sort_attrs_from_sort(sort_string) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/jsonapionify/api/resource/definitions/sorting.rb', line 74 def sort_attrs_from_sort(sort_string) sort_attrs = sort_string.split(',').map do |a| a == 'id' ? id_attribute.to_s : a.to_s end sort_attrs.uniq end |
#sort_fields_from_sort_string(sort_string) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/jsonapionify/api/resource/definitions/sorting.rb', line 81 def sort_fields_from_sort_string(sort_string) field_specs = sort_string.to_s.split(',') field_specs.each_with_object(SortFieldSet.new) do |field_spec, array| field_name, resource = field_spec.split('.').map(&:to_sym).reverse # Skip unless this resource next unless self <= self.api.resource(resource || type) # Assign Sort Fields field = SortField.new(field_name) field = SortField.new(id_attribute.to_s) if field.id? array << field end.tap do |field_set| field_set << SortField.new(id_attribute.to_s) end end |