Module: PassionView::Sortable
Defined Under Namespace
Modules: Controller
Instance Attribute Summary collapse
-
#sort_column ⇒ Object
Returns the value of attribute sort_column.
-
#sort_direction ⇒ Object
Returns the value of attribute sort_direction.
Instance Method Summary collapse
- #default_sort_column ⇒ Object
- #default_sort_direction ⇒ Object
- #initialize(items, options = {}) ⇒ Object
- #items ⇒ Object
- #order_arel ⇒ Object
- #sort_by_path(column, direction = default_sort_direction) ⇒ Object
- #sort_column_available?(name) ⇒ Boolean
- #sort_columns_available ⇒ Object
- #sort_directions_available ⇒ Object
- #sort_with(column, direction) ⇒ Object
- #sortable_params(column = nil, direction = nil) ⇒ Object
- #sortable_path(options) ⇒ Object
- #url_options ⇒ Object
Instance Attribute Details
#sort_column ⇒ Object
Returns the value of attribute sort_column.
6 7 8 |
# File 'lib/passion_view/sortable.rb', line 6 def sort_column @sort_column end |
#sort_direction ⇒ Object
Returns the value of attribute sort_direction.
6 7 8 |
# File 'lib/passion_view/sortable.rb', line 6 def sort_direction @sort_direction end |
Instance Method Details
#default_sort_column ⇒ Object
31 32 33 |
# File 'lib/passion_view/sortable.rb', line 31 def default_sort_column resource_class.primary_key end |
#default_sort_direction ⇒ Object
35 36 37 |
# File 'lib/passion_view/sortable.rb', line 35 def default_sort_direction sort_directions_available.first end |
#initialize(items, options = {}) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/passion_view/sortable.rb', line 8 def initialize(items, = {}) super sort = .delete(:sort) || {} sort_with(sort[:sort], sort[:direction]) end |
#items ⇒ Object
15 16 17 |
# File 'lib/passion_view/sortable.rb', line 15 def items super.order(order_arel[sort_column].send(sort_direction)) end |
#order_arel ⇒ Object
51 52 53 54 55 56 |
# File 'lib/passion_view/sortable.rb', line 51 def order_arel table = resource_class.arel_table pk = default_sort_column { pk => table[pk.to_sym] } end |
#sort_by_path(column, direction = default_sort_direction) ⇒ Object
39 40 41 |
# File 'lib/passion_view/sortable.rb', line 39 def sort_by_path(column, direction = default_sort_direction) sortable_path(sortable_params(column, direction)) end |
#sort_column_available?(name) ⇒ Boolean
23 24 25 |
# File 'lib/passion_view/sortable.rb', line 23 def sort_column_available?(name) sort_columns_available.include?(name.to_s) end |
#sort_columns_available ⇒ Object
19 20 21 |
# File 'lib/passion_view/sortable.rb', line 19 def sort_columns_available order_arel.keys end |
#sort_directions_available ⇒ Object
27 28 29 |
# File 'lib/passion_view/sortable.rb', line 27 def sort_directions_available %w(asc desc) end |
#sort_with(column, direction) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/passion_view/sortable.rb', line 70 def sort_with(column, direction) self.sort_column = column || default_sort_column self.sort_column = default_sort_column unless sort_columns_available.include?(sort_column) self.sort_direction = direction || default_sort_direction self.sort_direction = default_sort_direction unless %w(asc desc).include?(sort_direction) end |
#sortable_params(column = nil, direction = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/passion_view/sortable.rb', line 58 def sortable_params(column = nil, direction = nil) params = {} column ||= sort_column direction ||= sort_direction params[:sort] = column params[:direction] = direction params end |
#sortable_path(options) ⇒ Object
43 44 45 |
# File 'lib/passion_view/sortable.rb', line 43 def sortable_path() resource_path(.merge()) end |
#url_options ⇒ Object
47 48 49 |
# File 'lib/passion_view/sortable.rb', line 47 def super.merge(sortable_params) end |