Module: SqlSearchableSortable

Defined in:
lib/sql_search_n_sort/sql_searchable_sortable.rb

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
24
25
26
# 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
      attr_accessor :default_sort_dir
      attr_accessor :sql_sort_cols
      attr_accessor :sql_search_cols
    end

    #:Note remember when debugging from here "base" doesn't exist
    scope :sql_search, ->(search_for) { where(search_clause(search_for)) }
    
    scope :sql_sort, ->(sort_by=nil, dir=nil) do
      sort_by ||= default_sort_col
      dir ||= default_sort_dir || :asc
      if sql_sort_cols.any? { |c| c.is_a?(Hash) ? c.has_key?(sort_by) : c == sort_by }
        order(sort_by => dir)
      else
        default_sort_col ? order(default_sort_col => dir) : order(nil)
      end
    end
  end
end

Instance Method Details

#default_sql_sort(col, dir = nil) ⇒ Object



48
49
50
51
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 48

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



28
29
30
31
32
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 28

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_dropdownObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 57

def sort_cols_for_dropdown
  sql_sort_cols = self.sql_sort_cols
  sql_sort_cols ||= []
  return sql_sort_cols.inject([]) do |m, col|
    if col.is_a?(Hash) 
      h = col.fetch(col.keys.first)
      show_asc = h[:show_asc].nil? ? true : h[:show_asc]
      show_desc = h[:showdesc].nil? ? true : h[:show_desc]
      display_text = h[:display] || col.keys.first.to_s.humanize
      m << [display_text, col.keys.first.to_s] if show_asc
      m << ["#{display_text} [desc]", "#{col.keys.first} desc"] if show_desc
    else
      m << [col.to_s.humanize, col.to_s]
      m << ["#{col.to_s.humanize} [desc]", "#{col} desc"]
    end 
    m
  end 
end

#sortable?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 53

def sortable?
  !!sql_sort_cols
end

#sql_searchable(*cols) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 34

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



44
45
46
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 44

def sql_sortable(*cols)
  self.sql_sort_cols = cols
end