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

NOTES: Comment.joins(:article).order(Article.arel_table).to_sql=> “SELECT ‘comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline`”



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 6

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

  #:Note remember when debugging from here "base" doesn't exist
  #These scopes get called on a model class from within an index action in a controller
  # ...like a class method
  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 #use model's default sort col if no args present
    scope_sort_dir ||= default_sort_dir || :asc #same for direction
    order(sort_config.get_order(scope_sort_col, scope_sort_dir, default_sort_col, self))
  end
end
end

Instance Method Details

#default_sql_sort(col, dir = nil) ⇒ Object



50
51
52
53
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 50

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
33
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 28

def search_clause(search_for)
  (sql_search_cols || []).inject(nil) do |query, col|
    search_check = arel_table[col].matches("%#{sanitize_sql_like(search_for)}%")
    query.nil? ? search_check : query.or(search_check)
  end
end

#sort_cols_for_dropdownObject



59
60
61
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 59

def sort_cols_for_dropdown
  sort_config.select_opts 
end

#sortable?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 55

def sortable?
  !!sort_config
end

#sql_searchable(*cols) ⇒ Object



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

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 exception if not string or text type field
      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



46
47
48
# File 'lib/sql_search_n_sort/sql_searchable_sortable.rb', line 46

def sql_sortable(*cols)
  self.sort_config = ModelSortConfig.new(*cols)
end