Class: SqlSearchableSortable::ModelSortConfig

Inherits:
Array
  • Object
show all
Defined in:
lib/sql_search_n_sort/sql_searchable_sortable.rb

Instance Method Summary collapse

Constructor Details

#initialize(*cols) ⇒ ModelSortConfig

Returns a new instance of ModelSortConfig.



61
62
63
64
65
66
67
68
69
70
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 61

def initialize(*cols)
  cols.each do |col|
    if col.is_a? Hash
      h = col.fetch(col.keys.first)
      self << SortColumn.new(col.keys.first, h[:display_text], h.fetch(:show_asc, true), h.fetch(:show_desc, true))
    else
      self << SortColumn.new(col)
    end
  end
end

Instance Method Details

#contains_column(col) ⇒ Object



80
81
82
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 80

def contains_column(col)
  self.any? { |sc| sc.column == col }
end

#get_order(sort_by, dir, def_sort_col) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 72

def get_order(sort_by, dir, def_sort_col)
  if self.contains_column(sort_by)
    {sort_by => dir}
  else
    {def_sort_col => dir} if def_sort_col
  end
end

#select_optsObject



84
85
86
87
88
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 84

def select_opts
  return self.inject([]) do |m, sort_col|
    m + sort_col.select_opts
  end 
end