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
22
23
|
# 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="") do
search_for.blank? ? all : where(search_clause(search_for))
end
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
46
47
48
49
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 46
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
25
26
27
28
29
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 25
def search_clause(search_for)
(sql_search_cols || []).inject(Arel::Nodes::Group.new(2==1)) do |m, col|
m.or self.arel_table[col].matches("%#{search_for}%")
end
end
|
#sort_cols_for_dropdown ⇒ Object
55
56
57
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 55
def sort_cols_for_dropdown
sort_config.select_opts
end
|
#sortable? ⇒ Boolean
51
52
53
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 51
def sortable?
!!sort_config
end
|
#sql_searchable(*cols) ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 31
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
42
43
44
|
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 42
def sql_sortable(*cols)
self.sort_config = ModelSortConfig.new(*cols)
end
|