Module: SortOut::ActiveRecord
- Defined in:
- lib/sort_out/active_record.rb
Instance Method Summary collapse
- #sort_out(column, direction = nil) ⇒ Object
- #sortable(options = {}) ⇒ Object
- #sortable_column(column) ⇒ Object
- #sortable_direction(column, direction) ⇒ Object
Instance Method Details
#sort_out(column, direction = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sort_out/active_record.rb', line 10 def sort_out(column, direction = nil) if column.is_a? Hash direction = column[:direction] column = column[:sort] end sort_column = sortable_column(column).to_s.downcase sort_column << sortable_direction(column, direction) model = self.order(sort_column) @sortable_options[:after].each do |order| model = model.order(order.join(' ')) if order[0].to_s != sort_column end model end |
#sortable(options = {}) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/sort_out/active_record.rb', line 3 def sortable( = {}) = { :default_column => [:name, :asc], :after => [], :by => [] } @sortable_options = .merge() unless .nil? @sortable_options[:default_column] = [@sortable_options[:default_column], :asc] unless @sortable_options[:default_column].is_a? Array @sortable_options[:by] << @sortable_options[:default_column][0] end |
#sortable_column(column) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sort_out/active_record.rb', line 26 def sortable_column(column) @sortable_options[:by].each do |field| if field.is_a? Array and field[0].to_s == column return field[1] elsif field.to_s == column return column end end @sortable_options[:default_column][0] end |
#sortable_direction(column, direction) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/sort_out/active_record.rb', line 37 def sortable_direction(column, direction) if direction.present? " desc" elsif column.blank? " #{@sortable_options[:default_column][1].to_s}" else "" end end |