Module: WandoGrids

Defined in:
lib/wando_grid.rb

Instance Method Summary collapse

Instance Method Details

#build_filter_options(object, filter_hash) ⇒ Object

Grid每一列的查询



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/wando_grid.rb', line 77

def build_filter_options(object, filter_hash)
  return {} if filter_hash.nil?

  fs          = {}
  filter_hash = filter_hash.delete_if { |key, value| value.blank? }

  filter_hash.each do |columns, fvals|
    fs_tmp        = {}
    relation      = JSON.parse(params[:columnsConfig])
    gf_field      = relation.invert[fvals[:field]].downcase.gsub("/", "_")
    gf_type       = fvals[:data][:type].downcase
    gf_value      = fvals[:data][:value]
    gf_comparison = fvals[:data][:comparison]

    case gf_type
      when 'numeric'
        case gf_comparison
          when 'gt'
            gf_field += "_gt"
          when 'lt'
            gf_field += "_lt"
          when 'eq'
            gf_field += "_eq"
          when 'ne'
            gf_field += "_neq"
        end

        fs_tmp[gf_field.to_sym] = gf_value.to_i
      when 'string'
        gf_field += "_cont"

        fs_tmp[gf_field.to_sym] = gf_value.to_s
      when 'boolean'
        gf_value = gf_value.downcase

        if gf_value == 'true' or gf_value == 'false'
          gf_field += "_" + gf_value
          fs_tmp[gf_field.to_sym] = "1"
        end
      when 'list'
        gf_field += "_cont"

        fs_tmp[gf_field.to_sym] = gf_value.to_s
      when 'date'
        case gf_comparison
          when 'gt'
            gf_field += '_gt'
          when 'lt'
            gf_field += '_lt'
          when 'eq'
            gf_field += '_eq'
          when 'ne'
            gf_field += '_neq'
        end

        fs_tmp[gf_field.to_sym] = gf_value
    end

    unless fs_tmp.empty?
      fs.merge!(fs_tmp)
    end
  end

  fs
end

#get_record_by_fields(fields, object, option = {}) ⇒ Object

通过Grid的Field来构造Record记录



68
69
70
71
72
73
74
# File 'lib/wando_grid.rb', line 68

def get_record_by_fields(fields, object, option = {})
  fields = JSON.parse(fields) unless fields.is_a?(Array)
  records = object.where(option).provide(*fields)

  result = { :result => records }
  render_json result
end

#wando_grid(params, object, option = {}, toLoadData = true) ⇒ Object

wando_grid的入口函数,返回请求记录,和记录总数



12
13
14
15
16
17
# File 'lib/wando_grid.rb', line 12

def wando_grid(params, object, option = {}, toLoadData = true)
  object, result = wando_grid_in_json(params, object, option, toLoadData)
  
  result = { :total => object.count, :result => result }
  render_json  result
end

#wando_grid_for_array(params, array) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/wando_grid.rb', line 143

def wando_grid_for_array params, array
 fields, column_name, result = [], [], []

 relations = JSON.parse(params[:columnsConfig])
 relations.each do |o ,e|
   fields << e
   column_name << o
 end

 column_name.push("id")
 records = array.provide(*column_name)

 records.each do |r|
   result << Hash[r.map {|k, v| [relations[k], v] }]
 end

 render :json => { :success => true, :total => array.count, :result => result }
  
end

#wando_grid_in_json(params, object, option, toLoadData) ⇒ Object

构造JSON格式的Grid记录



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wando_grid.rb', line 20

def wando_grid_in_json(params, object, option, toLoadData)
  unless toLoadData
    return [], []
  end

  order = "updated_at DESC"
  unless params[:sort].nil?
    JSON.parse(params[:sort]).each do |k|
      if object.column_names.include?(k["property"])
        order = k["property"].to_s + " " + k["direction"].to_s
      else
        order = 'id' + ' ' + k["direction"].to_s
      end
    end
  end

  default_options = {
                      :order  => params[:order],
                      :offset => params[:offeset].to_i,
                      :start  => params[:start].to_i || 0,
                      :limit  => params[:limit].to_i || 20
                    }
  fields, column_name, result = [], [], []

  relations = JSON.parse(params[:columnsConfig])
  relations.each do |o ,e|
    fields << e
    column_name << o
  end

  column_name.push("id")
  offset = default_options[:offeset].to_i + default_options[:start].to_i
  records = object.where(option)
                  .order(order)
                  .limit(params[:limit])
                  .offset(offset)
                  .search(build_filter_options(object, params[:filter]))
                  .result(:distinct => true)
                  .provide(*column_name)

  records.each do |r|
    result << Hash[r.map {|k, v| [relations[k], v] }]
  end

  return object.where(option), result
end