Module: Sortiri::Model::ClassMethods
- Defined in:
- lib/sortiri/model.rb
Instance Attribute Summary collapse
-
#sortable_columns ⇒ Object
readonly
Returns the value of attribute sortable_columns.
Instance Method Summary collapse
- #parse_sorting(sort_query_string) ⇒ Object
- #sortable(against:, default_sort:, associated_against: {}) ⇒ Object
-
#sorted(sort_query_string = nil) ⇒ Object
sort_query_string is a string seperated by comma and takes - sign when it’s descending.
-
#sorted!(sort_query_string = nil) ⇒ Object
this function will override order by clauses which defined before sorted! call and force them to use its way.
Instance Attribute Details
#sortable_columns ⇒ Object (readonly)
Returns the value of attribute sortable_columns.
8 9 10 |
# File 'lib/sortiri/model.rb', line 8 def sortable_columns @sortable_columns end |
Instance Method Details
#parse_sorting(sort_query_string) ⇒ Object
27 28 29 30 31 |
# File 'lib/sortiri/model.rb', line 27 def parse_sorting(sort_query_string) sort_string = (sort_query_string.presence || default_sort) whitelisted_columns = Sortiri::Parser.new(sortable_columns: sortable_columns, sort_string: sort_string).whitelisted_columns Sortiri::Generators::OrderByGenerator.new(sortable_columns: sortable_columns, whitelisted_columns: whitelisted_columns) end |
#sortable(against:, default_sort:, associated_against: {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sortiri/model.rb', line 10 def sortable(against:, default_sort:, associated_against: {}) @sortable_columns ||= [] columns = against.map do |column| Sortiri::ActiveRecord::Column.new(name: column, model: self) end association_columns = associated_against.map do |association_name, columns_array| columns_array.map do |column| Sortiri::ActiveRecord::ForeignColumn.new(name: column, model: self, association_name: association_name) end end.flatten @sortable_columns = columns + association_columns @default_sort = default_sort end |
#sorted(sort_query_string = nil) ⇒ Object
sort_query_string is a string seperated by comma and takes - sign when it’s descending. all of the examples below are valid. request => GET /users?sort=-id,name,email || params => ‘-id,name,email’ request => GET /users?sort=name,email || params => ‘name,email’ request => GET /users || params => nil
38 39 40 41 |
# File 'lib/sortiri/model.rb', line 38 def sorted(sort_query_string = nil) generator = parse_sorting(sort_query_string) generator.sort(self) end |
#sorted!(sort_query_string = nil) ⇒ Object
this function will override order by clauses which defined before sorted! call and force them to use its way. every example above are still valid for this function.
46 47 48 49 |
# File 'lib/sortiri/model.rb', line 46 def sorted!(sort_query_string = nil) generator = parse_sorting(sort_query_string) generator.sort(self, reorder: true) end |