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.



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

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



78
79
80
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 78

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

#get_order(sort_by, dir, def_sort_col) ⇒ Object



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

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



82
83
84
85
86
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 82

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