module TableSortable::Controller

Attributes

column_offset[R]
column_order[R]
template_path[R]
translation_key[R]

Public Instance Methods

columns() click to toggle source
# File lib/table_sortable/controller.rb, line 87
def columns
  @columns.sort_by(column_order)
end
define_column(col_name, *options) click to toggle source
# File lib/table_sortable/controller.rb, line 66
def define_column(col_name, *options)
  options = default_column_options.merge(options.extract_options!)
  @columns.add(col_name, options.merge(controller: self))
end
define_column_offset(offset) click to toggle source
# File lib/table_sortable/controller.rb, line 75
def define_column_offset(offset)
  @column_offset = offset
end
define_column_order(*order) click to toggle source
# File lib/table_sortable/controller.rb, line 71
def define_column_order(*order)
  @column_order = order
end
define_columns(*args) click to toggle source
# File lib/table_sortable/controller.rb, line 52
def define_columns(*args)
  options = args.extract_options!
  column_offset = options[:offset] || 0
  translation_key = options[:translation_key]
  template_path = options[:template_path]
  columns   = args
  define_translation_key translation_key
  define_template_path template_path
  define_column_offset column_offset
  columns.each do |column|
    define_column column, translation_key: translation_key
  end
end
define_template_path(path) click to toggle source
# File lib/table_sortable/controller.rb, line 83
def define_template_path(path)
  @template_path = path.blank? ? nil : File.join(path, "")
end
define_translation_key(key) click to toggle source
# File lib/table_sortable/controller.rb, line 79
def define_translation_key(key)
  @translation_key = key
end

Private Instance Methods

default_column_options() click to toggle source
# File lib/table_sortable/controller.rb, line 93
def default_column_options
  {translation_key: @translation_key, template_path: @template_path}
end
filter_and_sort(scope, params = nil) click to toggle source
# File lib/table_sortable/controller.rb, line 97
def filter_and_sort(scope, params = nil)
  populate_params(params)

  actions = [->(records) { records }]
  ordered_actions(scope.first).reverse.each_with_index do |action, i|
    actions << ->(records) { action.used? ? (actions[i].call(action.run(records))) : actions[i].call(records) }
  end
  scope = actions.last.call(scope) unless scope.blank?
  if @query_params.page
    scope = Result.new(scope, @query_params.page, @query_params.page_size)
  end

  scope
end
initialize_table_sortable() click to toggle source
# File lib/table_sortable/controller.rb, line 112
def initialize_table_sortable
  @columns = TableSortable::Columns.new
  define_column_offset 0
end
ordered_actions(record = nil) click to toggle source
# File lib/table_sortable/controller.rb, line 117
def ordered_actions(record = nil)
  filter_actions =  @columns.map{|col| col.filter }
  sort_actions =    @columns.map{|col| col.sorter }
  (filter_actions+sort_actions).sort{ |a,b| (a.method(record) && b.method(record)) ? (a.method(record) <=> b.method(record)) : b.method(record) ? 1 : -1 }
end
populate_params(params = nil) click to toggle source
# File lib/table_sortable/controller.rb, line 123
def populate_params(params = nil)
  @query_params = QueryParams.new(params || self.params, columns, column_offset)
end