Module: SqlSearchableSortable
- Defined in:
- lib/sql_search_n_sort/sql_searchable_sortable.rb
Defined Under Namespace
Classes: ModelSortConfig, SortColumn
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.extended(base) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 3
def self.extended(base)
base.class_eval do
attr_accessor :ssns_sortable
class << self
attr_accessor :default_sort_col, :default_sort_dir, :sql_search_cols, :sort_config
end
scope :sql_search, ->(search_for) { where(search_clause(search_for)) }
scope :sql_sort, ->(scope_sort_col=nil, scope_sort_dir=nil) do
scope_sort_col ||= default_sort_col
scope_sort_dir ||= default_sort_dir || :asc
order(sort_config.get_order(scope_sort_col, scope_sort_dir, default_sort_col))
end
end
end
|
Instance Method Details
#default_sql_sort(col, dir = nil) ⇒ Object
44
45
46
47
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 44
def default_sql_sort(col, dir=nil)
self.default_sort_col = col
self.default_sort_dir = dir == :asc ? nil : dir
end
|
#search_clause(search_for) ⇒ Object
23
24
25
26
27
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 23
def search_clause(search_for)
(sql_search_cols || []).inject("1=2 ") do |m, col|
m << " or #{col} like '%#{search_for}%'"
end
end
|
#sort_cols_for_dropdown ⇒ Object
53
54
55
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 53
def sort_cols_for_dropdown
sort_config.select_opts
end
|
#sortable? ⇒ Boolean
49
50
51
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 49
def sortable?
!!sort_config
end
|
#sql_searchable(*cols) ⇒ Object
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 29
def sql_searchable(*cols)
self.sql_search_cols = (cols ||= [])
.select do |c|
col_name = c.is_a?(Hash) ? col.keys.first.to_s : c.to_s
raise(Exceptions::UnsearchableType.new(self, col_name)) \
if ![:string, :text].include?(self.columns_hash[col_name].type)
model_name.name.constantize.column_names.include?(col_name)
end
end
|
#sql_sortable(*cols) ⇒ Object
40
41
42
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 40
def sql_sortable(*cols)
self.sort_config = ModelSortConfig.new(*cols)
end
|