wando_grid 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/wando_grid.rb +167 -0
- metadata +45 -0
data/lib/wando_grid.rb
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module WandoGrids
|
3
|
+
# Author: Justin
|
4
|
+
# Comment: 构造Wando.Grid的后台数据
|
5
|
+
# date: 2013-03-04
|
6
|
+
# params: 前台wando_grid默认传入参数,用于数据查找,考虑不需要传该参数
|
7
|
+
# object: 传入你需要获取数据的 object 名字,object.class必须为ActivityRecord
|
8
|
+
# options: 传入附加的条件
|
9
|
+
# loadData: 是否自动加载数据
|
10
|
+
|
11
|
+
# wando_grid的入口函数,返回请求记录,和记录总数
|
12
|
+
def hello
|
13
|
+
puts "world"
|
14
|
+
end
|
15
|
+
|
16
|
+
def wando_grid(params, object, option = {}, toLoadData = true)
|
17
|
+
object, result = wando_grid_in_json(params, object, option, toLoadData)
|
18
|
+
|
19
|
+
result = { :total => object.count, :result => result }
|
20
|
+
render_json result
|
21
|
+
end
|
22
|
+
|
23
|
+
# 构造JSON格式的Grid记录
|
24
|
+
def wando_grid_in_json(params, object, option, toLoadData)
|
25
|
+
unless toLoadData
|
26
|
+
return [], []
|
27
|
+
end
|
28
|
+
|
29
|
+
order = "updated_at DESC"
|
30
|
+
unless params[:sort].nil?
|
31
|
+
JSON.parse(params[:sort]).each do |k|
|
32
|
+
if object.column_names.include?(k["property"])
|
33
|
+
order = k["property"].to_s + " " + k["direction"].to_s
|
34
|
+
else
|
35
|
+
order = 'id' + ' ' + k["direction"].to_s
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
default_options = {
|
41
|
+
:order => params[:order],
|
42
|
+
:offset => params[:offeset].to_i,
|
43
|
+
:start => params[:start].to_i || 0,
|
44
|
+
:limit => params[:limit].to_i || 20
|
45
|
+
}
|
46
|
+
fields, column_name, result = [], [], []
|
47
|
+
|
48
|
+
relations = JSON.parse(params[:columnsConfig])
|
49
|
+
relations.each do |o ,e|
|
50
|
+
fields << e
|
51
|
+
column_name << o
|
52
|
+
end
|
53
|
+
|
54
|
+
column_name.push("id")
|
55
|
+
offset = default_options[:offeset].to_i + default_options[:start].to_i
|
56
|
+
records = object.where(option)
|
57
|
+
.order(order)
|
58
|
+
.limit(params[:limit])
|
59
|
+
.offset(offset)
|
60
|
+
.search(build_filter_options(object, params[:filter]))
|
61
|
+
.result(:distinct => true)
|
62
|
+
.provide(*column_name)
|
63
|
+
|
64
|
+
records.each do |r|
|
65
|
+
result << Hash[r.map {|k, v| [relations[k], v] }]
|
66
|
+
end
|
67
|
+
|
68
|
+
return object.where(option), result
|
69
|
+
end
|
70
|
+
|
71
|
+
# 通过Grid的Field来构造Record记录
|
72
|
+
def get_record_by_fields(fields, object, option = {})
|
73
|
+
fields = JSON.parse(fields) unless fields.is_a?(Array)
|
74
|
+
records = object.where(option).provide(*fields)
|
75
|
+
|
76
|
+
result = { :result => records }
|
77
|
+
render_json result
|
78
|
+
end
|
79
|
+
|
80
|
+
# Grid每一列的查询
|
81
|
+
def build_filter_options(object, filter_hash)
|
82
|
+
return {} if filter_hash.nil?
|
83
|
+
|
84
|
+
fs = {}
|
85
|
+
filter_hash = filter_hash.delete_if { |key, value| value.blank? }
|
86
|
+
|
87
|
+
filter_hash.each do |columns, fvals|
|
88
|
+
fs_tmp = {}
|
89
|
+
relation = JSON.parse(params[:columnsConfig])
|
90
|
+
gf_field = relation.invert[fvals[:field]].downcase.gsub("/", "_")
|
91
|
+
gf_type = fvals[:data][:type].downcase
|
92
|
+
gf_value = fvals[:data][:value]
|
93
|
+
gf_comparison = fvals[:data][:comparison]
|
94
|
+
|
95
|
+
case gf_type
|
96
|
+
when 'numeric'
|
97
|
+
case gf_comparison
|
98
|
+
when 'gt'
|
99
|
+
gf_field += "_gt"
|
100
|
+
when 'lt'
|
101
|
+
gf_field += "_lt"
|
102
|
+
when 'eq'
|
103
|
+
gf_field += "_eq"
|
104
|
+
when 'ne'
|
105
|
+
gf_field += "_neq"
|
106
|
+
end
|
107
|
+
|
108
|
+
fs_tmp[gf_field.to_sym] = gf_value.to_i
|
109
|
+
when 'string'
|
110
|
+
gf_field += "_cont"
|
111
|
+
|
112
|
+
fs_tmp[gf_field.to_sym] = gf_value.to_s
|
113
|
+
when 'boolean'
|
114
|
+
gf_value = gf_value.downcase
|
115
|
+
|
116
|
+
if gf_value == 'true' or gf_value == 'false'
|
117
|
+
gf_field += "_" + gf_value
|
118
|
+
fs_tmp[gf_field.to_sym] = "1"
|
119
|
+
end
|
120
|
+
when 'list'
|
121
|
+
gf_field += "_cont"
|
122
|
+
|
123
|
+
fs_tmp[gf_field.to_sym] = gf_value.to_s
|
124
|
+
when 'date'
|
125
|
+
case gf_comparison
|
126
|
+
when 'gt'
|
127
|
+
gf_field += '_gt'
|
128
|
+
when 'lt'
|
129
|
+
gf_field += '_lt'
|
130
|
+
when 'eq'
|
131
|
+
gf_field += '_eq'
|
132
|
+
when 'ne'
|
133
|
+
gf_field += '_neq'
|
134
|
+
end
|
135
|
+
|
136
|
+
fs_tmp[gf_field.to_sym] = gf_value
|
137
|
+
end
|
138
|
+
|
139
|
+
unless fs_tmp.empty?
|
140
|
+
fs.merge!(fs_tmp)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
fs
|
145
|
+
end
|
146
|
+
|
147
|
+
#def wando_grid_for_array params, array
|
148
|
+
# fields, column_name, result = [], [], []
|
149
|
+
|
150
|
+
# relations = JSON.parse(params[:columnsConfig])
|
151
|
+
# relations.each do |o ,e|
|
152
|
+
# fields << e
|
153
|
+
# column_name << o
|
154
|
+
# end
|
155
|
+
|
156
|
+
# column_name.push("id")
|
157
|
+
# records = array.provide(*column_name)
|
158
|
+
|
159
|
+
# records.each do |r|
|
160
|
+
# result << Hash[r.map {|k, v| [relations[k], v] }]
|
161
|
+
# end
|
162
|
+
|
163
|
+
# render :json => { :success => true, :total => array.count, :result => result }
|
164
|
+
|
165
|
+
#end
|
166
|
+
|
167
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wando_grid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Justin_lu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-15 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: organize a extjs grid that you can load data from rails
|
15
|
+
email: gdjyluxiaoyong@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/wando_grid.rb
|
21
|
+
homepage: http://rubygems.org/gems/wando_grid
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: extjs4 grid
|
45
|
+
test_files: []
|