Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Includes:
Gridify
Defined in:
lib/jquery/secret_sauce/model/find_for_grid.rb,
lib/jquery/gridify.rb

Class Method Summary collapse

Methods included from Gridify

included

Class Method Details

.find_for_grid_2(params) ⇒ Object

takes params from request and returns records serialized to json. handles the majority of the functionality for searching, sorting, and serializing records to be consumed by jqGrid.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jquery/secret_sauce/model/find_for_grid.rb', line 5

def self.find_for_grid_2(params)
  search_options = {:limit => params["rows"]}
  search_options[:offset] = (params["page"].to_i * params["rows"].to_i) - params["rows"].to_i
  search_options[:order] = "#{params['sidx']} #{params['sord']}" if params["sidx"] != ""
  if params["_search"] == "true"
    string_search_operations = {
        "eq" => "=",
        "ne" => "<>",
        "bw" => "LIKE",
        "bn" => "NOT LIKE",
        "ew" => "LIKE",
        "en" => "NOT LIKE",
        "cn" => "LIKE",
        "nc" => "NOT LIKE"
    }
    integer_search_operations = {
        "eq" => "=",
        "ne" => "<>",
        "lt" => "<",
        "le" => "<=",
        "gt" => ">",
        "ge" => ">="
    }
    if [:text, :string].include?(self.columns.select { |c| c.name == params["searchField"] }.first.type) && string_search_operations.keys.include?(params["searchOper"])
      search_options[:conditions] = "#{params["searchField"]} "
      search_options[:conditions] << "#{string_search_operations[params["searchOper"]]}"
      search_options[:conditions] << "'#{"%" if ["ew", "en", "cn", "nc"].include?(params["searchOper"])}#{params["searchString"]}#{"%" if ["nc", "cn", "bn", "bw"].include?(params["searchOper"])}'"
    elsif [:integer].include?(self.columns.select { |c| c.name == params["searchField"] }.first.type) && integer_search_operations.keys.include?(params["searchOper"])
      search_options[:conditions] = "#{params["searchField"]} #{integer_search_operations[params["searchOper"]]} #{params["searchString"]}"
    end
  end
  count = self.count(:all, :conditions => search_options[:conditions]).to_f
  # raise count.inspect
  array = self.find(:all, search_options).collect { |r| r.attributes }
  {:total => (count / params["rows"].to_f).ceil, :page => params["page"], :records => count, :rows => array}.to_json
end