Class: ModelSortConfig
- Inherits:
-
Array
- Object
- Array
- ModelSortConfig
- Defined in:
- lib/sql_search_n_sort/model_sort_config.rb
Instance Method Summary collapse
- #get_order(sort_by, dir, def_sort_col, base_class) ⇒ Object
-
#initialize(*cols) ⇒ ModelSortConfig
constructor
A new instance of ModelSortConfig.
- #select_opts ⇒ Object
- #sortable_column(col) ⇒ Object
Constructor Details
#initialize(*cols) ⇒ ModelSortConfig
Returns a new instance of ModelSortConfig.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/sql_search_n_sort/model_sort_config.rb', line 3 def initialize(*cols) cols.each do |col| if col.is_a? Hash opts_hash = col.fetch(col.keys.first) self << SortColumn.new(col.keys.first, opts_hash) else self << SortColumn.new(col) end end end |
Instance Method Details
#get_order(sort_by, dir, def_sort_col, base_class) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/sql_search_n_sort/model_sort_config.rb', line 16 def get_order(sort_by, dir, def_sort_col, base_class) if sort_column_obj = self.sortable_column(sort_by) # {sort_by => dir} ar_class = sort_column_obj.joined_table ? sort_column_obj.joined_table.to_s.classify.constantize : base_class ar_class.arel_table[sort_by].send(dir) else {def_sort_col => dir} if def_sort_col end end |
#select_opts ⇒ Object
31 32 33 34 35 |
# File 'lib/sql_search_n_sort/model_sort_config.rb', line 31 def select_opts return self.inject([]) do |m, sort_col| m + sort_col.select_opts end end |
#sortable_column(col) ⇒ Object
26 27 28 29 |
# File 'lib/sql_search_n_sort/model_sort_config.rb', line 26 def sortable_column(col) #returns nil if no matching sortable columns self.select { |model_sort_config| model_sort_config.send("column") == col.to_s }.first end |