jqgrid_for_rails 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/CHANGELOG.rdoc +13 -0
- data/README.rdoc +7 -19
- data/lib/app/helpers/jqgrids_helper.rb +56 -0
- data/lib/jqgrid_for_rails/controllers/helpers.rb +76 -47
- data/lib/jqgrid_for_rails/version.rb +1 -1
- data/test/controllers/helpers_test.rb +39 -49
- data/test/jqgrids_helper_test.rb +77 -29
- metadata +33 -52
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZmIyYjQyZjRjOWQ4MmRmMzJlODVjOTQ3OGExZDQzNTI4NTVhNDViOQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTFhODFhYzgwZjkwZTVmYzU0YjQ0MzlhMTg4MzlmYWM4MDcyY2JlNA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YjM5NjhmZDA5MjEwMDM4MjI3MzU0NmE4Y2U3NmM5MmE1MWRmYmIxMWM1Nzhh
|
10
|
+
MDI3YzczNzM3ZDMxZTBmN2MwMjM5MmJlYTNjNDJlNTk3M2Q4MjhhMmIwNjM1
|
11
|
+
MzNmOGU2ZGNkZGM1ODM4NDAxNWYyOWQyYjg1Mjk2ZTcxODNhMTQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjFkYzQ2YTk1MzY4NWIxNTQ3NDFlNzYzYWRhZjQ5NTA1ZjFiNmI2NWRjMTgy
|
14
|
+
ZGU5NTY3YzlhMjViZDAzMTNkZGUyZDk5Yjk3ODQ5ZjRhZjBjNWE5Mzc1ZTA0
|
15
|
+
NzBmN2RlYmRjNTY0MTkwOTdiNmZjYTExYjQ5NTY4YjY3MDUxMDM=
|
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
== 1.0.2
|
2
|
+
|
3
|
+
* enhancements
|
4
|
+
* Support joins and includes in column names.
|
5
|
+
* A proc is now accepted by id_column option in json_for_jqgrid method.
|
6
|
+
* Allow the column name in the 'order_by' helper to be quoted.
|
7
|
+
* View helpers are now tested using ActionView::TestCase.
|
8
|
+
* col_model_for_jqgrid is moved from controller helpers to view helpers.
|
9
|
+
* :format option added to json_for_jqgrid helper.
|
10
|
+
|
11
|
+
* bug fixes
|
12
|
+
* Fixes issue when using id_column in json_for_jqgrid method.
|
13
|
+
|
1
14
|
== 1.0.1
|
2
15
|
|
3
16
|
* enhancements
|
data/README.rdoc
CHANGED
@@ -111,39 +111,23 @@ You can do it better using +content_for+ in the view:
|
|
111
111
|
|
112
112
|
Don't forget to include the jquery and jqgrid javascript and stylesheet files!
|
113
113
|
|
114
|
-
== Controllers
|
115
|
-
|
116
|
-
There are convenient methods available in the controllers
|
117
|
-
|
118
114
|
=== col_model_for_jqgrid
|
119
115
|
|
120
|
-
This
|
116
|
+
This helper easily creates an array to be used as col_model.
|
121
117
|
|
122
118
|
col_model_for_jqgrid(['inv_date', 'total' ], {:width => 100})
|
123
119
|
|
124
120
|
#=> [{:name=>"inv_date", :index=>"inv_date", :width=>100}, {:name=>"total", :index=>"total", :width=>100}]
|
125
121
|
|
126
122
|
|
127
|
-
So
|
128
|
-
|
129
|
-
# app/controllers/invoices_controller.rb
|
130
|
-
|
131
|
-
class InvoicesController < ApplicationController
|
132
|
-
|
133
|
-
def show
|
134
|
-
@col_model = col_model_for_jqgrid(['inv_date', 'total' ], {:width => 180})
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
|
139
|
-
And then you can use it when creating the grid in the helper:
|
123
|
+
So you you can use it when creating the grid in the helper:
|
140
124
|
|
141
125
|
grid = [{
|
142
126
|
:url => '/invoices',
|
143
127
|
:datatype => 'json',
|
144
128
|
:mtype => 'GET',
|
145
129
|
:colNames => ['Inv No','Date'],
|
146
|
-
:colModel =>
|
130
|
+
:colModel => col_model_for_jqgrid(['Inv No','Date']),
|
147
131
|
:pager => '#invoices_pager',
|
148
132
|
:rowNum => 10,
|
149
133
|
:rowList => [10, 20, 30],
|
@@ -153,6 +137,10 @@ And then you can use it when creating the grid in the helper:
|
|
153
137
|
|
154
138
|
jqgrid_api 'invoices_list', grid
|
155
139
|
|
140
|
+
== Controllers
|
141
|
+
|
142
|
+
There are convenient methods available in the controllers
|
143
|
+
|
156
144
|
=== json_for_jqgrid
|
157
145
|
|
158
146
|
This method generates the json response for the grid. It takes the records found by the +paginate+ method offered by will_paginate[https://github.com/mislav/will_paginate].
|
@@ -107,6 +107,47 @@ module JqgridsHelper
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
|
+
# Returns an array to be used as col_model for the grid where each item
|
111
|
+
# is a hash for each column. Each hash will have at least two keys by
|
112
|
+
# default, the +name+ and the +index+, whose values will be the name of
|
113
|
+
# the column. Other keys will be included if present in the +options+
|
114
|
+
# hash.
|
115
|
+
#
|
116
|
+
# * +columns+ - Array with the name of the columns to include in the
|
117
|
+
# col_model.
|
118
|
+
#
|
119
|
+
# ==== Options
|
120
|
+
#
|
121
|
+
# Every item in the options hash will be a property in the col_model for
|
122
|
+
# every column, unless the item is also a hash, where only will be included
|
123
|
+
# in the specified columns. An item can be also a Proc, that will be called
|
124
|
+
# passing +columns+ as parameter.
|
125
|
+
#
|
126
|
+
# ==== Examples
|
127
|
+
#
|
128
|
+
# col_model_for_jqgrid(['inv_date', 'total' ])
|
129
|
+
#
|
130
|
+
# #=> [{:name=>"inv_date", :index=>"inv_date"}, {:name=>"total", :index=>"total"}]
|
131
|
+
#
|
132
|
+
# col_model_for_jqgrid(['inv_date', 'total' ], {:width => 100})
|
133
|
+
#
|
134
|
+
# #=> [{:name=>"inv_date", :index=>"inv_date", :width=>100}, {:name=>"total", :index=>"total", :width=>100}]
|
135
|
+
#
|
136
|
+
# col_model_for_jqgrid(['inv_date', 'total'], {:width => {'inv_date' => 100}})
|
137
|
+
#
|
138
|
+
# #=> [{:name => 'inv_date', :index => 'inv_date', :width => 100}, {:name => 'total', :index => 'total'}]
|
139
|
+
#
|
140
|
+
# col_model_for_jqgrid(['inv_date'], {:prop => Proc.new {|c| c.camelize}})
|
141
|
+
#
|
142
|
+
# #=> [{:name => 'inv_date', :index => 'inv_date', :prop => 'InvDate'}]
|
143
|
+
#
|
144
|
+
def col_model_for_jqgrid columns, options = {}
|
145
|
+
columns.map do |c|
|
146
|
+
h = {:name => c, :index => c}
|
147
|
+
h.merge property_from_options(c, options)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
110
151
|
private
|
111
152
|
|
112
153
|
def wrap_with_document_ready! str
|
@@ -117,5 +158,20 @@ private
|
|
117
158
|
str.replace("<script>\n#{str}\n</script>")
|
118
159
|
end
|
119
160
|
|
161
|
+
def property_from_options col_name, options
|
162
|
+
h = {}
|
163
|
+
options.each do |k, v|
|
164
|
+
case v.class.to_s
|
165
|
+
when "Proc"
|
166
|
+
h[k] = v.call(col_name)
|
167
|
+
when "Hash"
|
168
|
+
h[k] = v[col_name] if v.has_key?(col_name)
|
169
|
+
else
|
170
|
+
h[k] = v
|
171
|
+
end
|
172
|
+
end
|
173
|
+
h
|
174
|
+
end
|
175
|
+
|
120
176
|
end
|
121
177
|
|
@@ -3,46 +3,12 @@ module JqgridForRails
|
|
3
3
|
# Those helpers are convenience methods added to ApplicationController.
|
4
4
|
module Helpers
|
5
5
|
|
6
|
-
#
|
7
|
-
# is a hash for each column. Each hash will have at least two keys by
|
8
|
-
# default, the +name+ and the +index+, whose values will be the name of
|
9
|
-
# the column. Other keys will be included if present in the +options+
|
10
|
-
# hash.
|
11
|
-
#
|
12
|
-
# * +columns+ - Array with the name of the columns to include in the
|
13
|
-
# col_model.
|
14
|
-
#
|
15
|
-
# ==== Options
|
16
|
-
#
|
17
|
-
# Every item in the options hash will be a property in the col_model for
|
18
|
-
# every column, unless the item is also a hash, where only will be included
|
19
|
-
# in the specified columns. An item can be also a Proc, that will be called
|
20
|
-
# passing +columns+ as parameter.
|
21
|
-
#
|
22
|
-
# ==== Examples
|
23
|
-
#
|
24
|
-
# col_model_for_jqgrid(['inv_date', 'total' ])
|
25
|
-
#
|
26
|
-
# #=> [{:name=>"inv_date", :index=>"inv_date"}, {:name=>"total", :index=>"total"}]
|
27
|
-
#
|
28
|
-
# col_model_for_jqgrid(['inv_date', 'total' ], {:width => 100})
|
29
|
-
#
|
30
|
-
# #=> [{:name=>"inv_date", :index=>"inv_date", :width=>100}, {:name=>"total", :index=>"total", :width=>100}]
|
31
|
-
#
|
32
|
-
# col_model_for_jqgrid(['inv_date', 'total'], {:width => {'inv_date' => 100}})
|
33
|
-
#
|
34
|
-
# #=> [{:name => 'inv_date', :index => 'inv_date', :width => 100}, {:name => 'total', :index => 'total'}]
|
35
|
-
#
|
36
|
-
# col_model_for_jqgrid(['inv_date'], {:prop => Proc.new {|c| c.camelize}})
|
37
|
-
#
|
38
|
-
# #=> [{:name => 'inv_date', :index => 'inv_date', :prop => 'InvDate'}]
|
39
|
-
#
|
6
|
+
# DEPRECATED! Moved to jqgrids_helper.rb
|
40
7
|
def col_model_for_jqgrid columns, options = {}
|
41
8
|
columns.map do |c|
|
42
9
|
h = {:name => c, :index => c}
|
43
10
|
h.merge property_from_options(c, options)
|
44
11
|
end
|
45
|
-
|
46
12
|
end
|
47
13
|
|
48
14
|
# Returns a json string ready to be sent to a jqgrid component.
|
@@ -55,7 +21,9 @@ module JqgridForRails
|
|
55
21
|
# ==== Options
|
56
22
|
#
|
57
23
|
# * <tt>:id_column</tt> - Says which is the column that should be used as
|
58
|
-
# the row id.
|
24
|
+
# the row id. This should a be the column name, but it can also be a
|
25
|
+
# Proc, in which case the record will be passed to the proc. See examples
|
26
|
+
# for more info.
|
59
27
|
#
|
60
28
|
# * <tt>:id_prefix</tt> - If specified, the +column_id+ option will be
|
61
29
|
# concatenated to this prefix. This helps to keep the html id unique in
|
@@ -65,9 +33,29 @@ module JqgridForRails
|
|
65
33
|
# localized with the <tt>I18n.l</tt> method. The field content must be
|
66
34
|
# of any class accepted by the method. I.e. Time, DateTime, Date.
|
67
35
|
#
|
36
|
+
# * <tt>:format</tt> - Says the proc and the columns where to apply it. It
|
37
|
+
# can be used to change the format of the field as well as to run any other
|
38
|
+
# code on it. If the key of the hash is not a column name but an array of
|
39
|
+
# column names, the proc is applied to all of them.
|
40
|
+
#
|
68
41
|
# * <tt>:page</tt> - Says the page number (Deprecated. The page number is
|
69
42
|
# now inferred from +records+.
|
70
43
|
#
|
44
|
+
# ==== Examples
|
45
|
+
#
|
46
|
+
# records = Invoice.paginate(:page => 1)
|
47
|
+
# my_proc = Proc.new {|r| "invid_#{r.invid}" }
|
48
|
+
# json_for_jqgrid(records, ['invdate', 'amount', 'total' ], {:id_column => my_proc })
|
49
|
+
#
|
50
|
+
# # => {"rows":[{"cell":["2011-01-01T00:00:00Z",10.0,11.0],"id":"invid_1"}],"total":1,"page":1,"records":1}
|
51
|
+
#
|
52
|
+
# records = Invoice.paginate(:page => 1)
|
53
|
+
# my_format = Proc.new {|val| val + 100 }
|
54
|
+
# json_for_jqgrid(records, ['invdate', 'amount', 'total' ], {:format => { 'total' => my_format })
|
55
|
+
#
|
56
|
+
# # Because of the :format option, 100 is added to 'total' field.
|
57
|
+
# # => {"rows":[{"cell":["2011-01-01T00:00:00Z",10.0,111.0],"id":"invid_1"}],"total":2,"page":1,"records":1}
|
58
|
+
#
|
71
59
|
def json_for_jqgrid records, columns = nil, options = {}
|
72
60
|
|
73
61
|
columns ||= (records.first.attributes.keys rescue [])
|
@@ -83,34 +71,51 @@ module JqgridForRails
|
|
83
71
|
end
|
84
72
|
|
85
73
|
# Returns the 'order by' string created using the params received from the jqgrid.
|
74
|
+
# If +quote_char+ is specified, the column name used in the 'order by' clause will
|
75
|
+
# be quoted.
|
86
76
|
#
|
87
77
|
# ==== Example
|
88
78
|
#
|
89
79
|
# order_by_from_params({'sidx' => 'updated_at', 'sord' => 'asc'})
|
90
80
|
# #=> 'updated_at asc'
|
91
81
|
#
|
92
|
-
|
93
|
-
|
82
|
+
# order_by_from_params({'sidx' => 'updated_at', 'sord' => 'desc'}, '`')
|
83
|
+
# #=> '`updated_at` desc'
|
84
|
+
#
|
85
|
+
def order_by_from_params params, quote_char = nil
|
86
|
+
order_by = quote(params['sidx'], quote_char) unless params['sidx'].blank?
|
94
87
|
order_by << " #{params['sord']}" if params['sord'] && order_by
|
95
88
|
order_by
|
96
89
|
end
|
97
90
|
|
98
91
|
private
|
99
92
|
|
93
|
+
def quote value, quote_char
|
94
|
+
"#{quote_char}#{value}#{quote_char}"
|
95
|
+
end
|
96
|
+
|
100
97
|
# Returns a hash with the values for a jqgrid json row.
|
101
98
|
def row_from_record(r, columns, options)
|
102
|
-
attribs
|
99
|
+
localize(attribs, options[:translate]) if options[:translate]
|
103
100
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
attribs[col.to_s] = I18n.l(attribs[col.to_s]) if locale_classes.include?(attribs[col.to_s].class)
|
109
|
-
end
|
101
|
+
format(attribs, options[:format]) if options[:format]
|
102
|
+
|
103
|
+
values = columns.map do |column|
|
104
|
+
column.split('.').inject(r) { |o, a| o.public_send(a) }
|
110
105
|
end
|
111
106
|
|
112
|
-
{
|
113
|
-
|
107
|
+
{
|
108
|
+
:id => "#{options[:id_prefix]}#{id_column_from_options(r, options)}",
|
109
|
+
:cell => values
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
def id_column_from_options record, options
|
114
|
+
if options[:id_column].class.to_s == 'Proc'
|
115
|
+
options[:id_column].call(record)
|
116
|
+
else
|
117
|
+
record.attributes[options[:id_column]]
|
118
|
+
end
|
114
119
|
end
|
115
120
|
|
116
121
|
def property_from_options col_name, options
|
@@ -127,6 +132,30 @@ module JqgridForRails
|
|
127
132
|
end
|
128
133
|
h
|
129
134
|
end
|
135
|
+
|
136
|
+
# Localize Date, Time or DateTime fields
|
137
|
+
def localize attribs, translate
|
138
|
+
locale_classes = [Time, Date, DateTime]
|
139
|
+
if translate.is_a?(Array) && I18n
|
140
|
+
translate.each do |col|
|
141
|
+
attribs[col.to_s] = I18n.l(attribs[col.to_s]) if locale_classes.include?(attribs[col.to_s].class)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# Format fields
|
147
|
+
def format attribs, format
|
148
|
+
if format.is_a?(Hash)
|
149
|
+
format.each do |col, pr|
|
150
|
+
if col.is_a?(Array)
|
151
|
+
col.each {|c| attribs[c] = pr.call(attribs[c])}
|
152
|
+
else
|
153
|
+
attribs[col] = pr.call(attribs[col])
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
130
159
|
end
|
131
160
|
end
|
132
161
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'test/test_helper'
|
1
|
+
require 'test/test_helper'
|
2
2
|
|
3
3
|
class MockController < ApplicationController
|
4
4
|
end
|
@@ -31,6 +31,11 @@ class ControllerHelpersTest < ActionController::TestCase
|
|
31
31
|
assert_nil @controller.order_by_from_params(params)
|
32
32
|
end
|
33
33
|
|
34
|
+
test "order_by_from with quoting char" do
|
35
|
+
params = {'sidx' => 'updated_at', 'sord' => 'desc'}
|
36
|
+
assert_equal '`updated_at` desc', @controller.order_by_from_params(params, '`')
|
37
|
+
end
|
38
|
+
|
34
39
|
test "json_for_grid with empty records result" do
|
35
40
|
records = Invoice.paginate(:page => 1)
|
36
41
|
json = @controller.json_for_jqgrid(records)
|
@@ -44,64 +49,49 @@ class ControllerHelpersTest < ActionController::TestCase
|
|
44
49
|
test "json_for_grid with one record and id prefix" do
|
45
50
|
tmp_record = Invoice.create({:invid => 1, :invdate => '2011-01-01 00:00:00', :amount => 10, :tax => 1, :total => 11, :note => '' })
|
46
51
|
records = Invoice.paginate(:page => 1)
|
47
|
-
json = @controller.json_for_jqgrid(records, ['
|
52
|
+
json = @controller.json_for_jqgrid(records, ['amount', 'total' ], {:id_column => 'invid', :id_prefix => 'mygrid_row_'})
|
48
53
|
hash = ActiveSupport::JSON.decode(json)
|
49
54
|
Invoice.delete(tmp_record.id)
|
50
|
-
assert_equal hash["total"]
|
51
|
-
assert_equal hash["page"]
|
52
|
-
assert_equal hash["records"]
|
53
|
-
assert_equal hash["rows"][0]["cell"]
|
54
|
-
assert_equal hash["rows"][0]["id"]
|
55
|
+
assert_equal 1, hash["total"]
|
56
|
+
assert_equal 1, hash["page"]
|
57
|
+
assert_equal 1, hash["records"]
|
58
|
+
assert_equal [10.0, 11.0], hash["rows"][0]["cell"]
|
59
|
+
assert_equal "mygrid_row_1", hash["rows"][0]["id"]
|
55
60
|
end
|
56
61
|
|
57
|
-
test "
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
], result
|
65
|
-
end
|
62
|
+
test "json_for_jqgrid with proc for id_column" do
|
63
|
+
tmp_record = Invoice.create({:invid => 1, :invdate => '2011-01-01 00:00:00', :amount => 10, :tax => 1, :total => 11, :note => '' })
|
64
|
+
records = Invoice.paginate(:page => 1)
|
65
|
+
my_proc = Proc.new { |r| "invid_#{r.invid}" }
|
66
|
+
json = @controller.json_for_jqgrid(records, ['invdate', 'amount', 'total' ], {:id_column => my_proc })
|
67
|
+
hash = ActiveSupport::JSON.decode(json)
|
68
|
+
Invoice.delete(tmp_record.id)
|
66
69
|
|
67
|
-
|
68
|
-
columns = ['inv_date', 'amount', 'total' ]
|
69
|
-
result = @controller.col_model_for_jqgrid(columns, {:width => 100})
|
70
|
-
assert_equal [
|
71
|
-
{:name => 'inv_date', :index => 'inv_date', :width => 100},
|
72
|
-
{:name => 'amount', :index => 'amount', :width => 100},
|
73
|
-
{:name => 'total', :index => 'total', :width => 100}
|
74
|
-
], result
|
70
|
+
assert_equal 'invid_1', hash["rows"].first["id"]
|
75
71
|
end
|
76
72
|
|
77
|
-
test "
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
], result
|
85
|
-
end
|
73
|
+
test "json_for_jqgrid should allow to format field with a proc" do
|
74
|
+
tmp_record = Invoice.create({:invid => 1, :invdate => '2011-01-01 00:00:00', :amount => 10, :tax => 1, :total => 11, :note => '123,456.123' })
|
75
|
+
records = Invoice.paginate(:page => 1)
|
76
|
+
my_proc = Proc.new { |r| (r.gsub(/,/, '').to_f) }
|
77
|
+
json = @controller.json_for_jqgrid(records, ['invdate', 'amount', 'total', 'note' ], {:format => {'note' => my_proc }})
|
78
|
+
hash = ActiveSupport::JSON.decode(json)
|
79
|
+
Invoice.delete(tmp_record.id)
|
86
80
|
|
87
|
-
|
88
|
-
columns = ['inv_date', 'amount', 'total' ]
|
89
|
-
result = @controller.col_model_for_jqgrid(columns, {:property_1 => {'inv_date' => 'high', 'total' => 'low'}, :property_2 => true})
|
90
|
-
assert_equal [
|
91
|
-
{:name => 'inv_date', :index => 'inv_date', :property_1 => 'high', :property_2 => true},
|
92
|
-
{:name => 'amount', :index => 'amount', :property_2 => true},
|
93
|
-
{:name => 'total', :index => 'total', :property_1 => 'low', :property_2 => true}
|
94
|
-
], result
|
81
|
+
assert_equal 123456.123, hash["rows"].first["cell"].last
|
95
82
|
end
|
96
83
|
|
97
|
-
test "
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
84
|
+
test "json_for_jqgrid should allow to format many fields with a proc" do
|
85
|
+
tmp_record = Invoice.create({:invid => 1, :invdate => '2011-01-01 00:00:00', :amount => 10, :tax => 1, :total => 11, :note => '123,456.123' })
|
86
|
+
records = Invoice.paginate(:page => 1)
|
87
|
+
my_proc = Proc.new { |r| (r * 2) }
|
88
|
+
json = @controller.json_for_jqgrid(records, ['invdate', 'amount', 'total', 'note' ], {:format => {['amount', 'total'] => my_proc }})
|
89
|
+
hash = ActiveSupport::JSON.decode(json)
|
90
|
+
Invoice.delete(tmp_record.id)
|
91
|
+
|
92
|
+
assert_equal 20, hash["rows"].first["cell"][1]
|
93
|
+
assert_equal 22, hash["rows"].first["cell"][2]
|
105
94
|
end
|
106
95
|
|
96
|
+
|
107
97
|
end
|
data/test/jqgrids_helper_test.rb
CHANGED
@@ -1,14 +1,17 @@
|
|
1
|
+
require "action_view/test_case"
|
1
2
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
3
|
|
3
4
|
class MockView < ActionView::Base
|
4
5
|
include JqgridsHelper
|
5
6
|
end
|
6
7
|
|
7
|
-
class JqgridsHelperTest <
|
8
|
-
|
9
|
-
def test_jqgrid_small
|
8
|
+
class JqgridsHelperTest < ActionView::TestCase
|
10
9
|
|
10
|
+
def setup
|
11
11
|
@template = MockView.new
|
12
|
+
end
|
13
|
+
|
14
|
+
test "jqgrid small" do
|
12
15
|
|
13
16
|
grid_id = 'grid_id'
|
14
17
|
grid_options = [{:url => "/jqGridModel?model=Wine" }]
|
@@ -18,12 +21,9 @@ class JqgridsHelperTest < Test::Unit::TestCase
|
|
18
21
|
expected << '</script>'
|
19
22
|
|
20
23
|
assert_equal(expected, @template.jqgrid_api(grid_id, grid_options, {:script_tags => true}))
|
21
|
-
|
22
24
|
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
@template = MockView.new
|
26
|
+
test "jqgrid_medium" do
|
27
27
|
|
28
28
|
grid_id = 'grid_id'
|
29
29
|
pager_id = 'gridpager'
|
@@ -38,23 +38,7 @@ class JqgridsHelperTest < Test::Unit::TestCase
|
|
38
38
|
assert_equal(expected, @template.jqgrid_api(grid_id, grid_options, options))
|
39
39
|
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
options[:script_tags] ||= true
|
44
|
-
|
45
|
-
js = 'jQuery(document).ready(function() {
|
46
|
-
jQuery("#' + grid_id + '").jqGrid({
|
47
|
-
"pager":jQuery(\'#' + pager_id + '\')
|
48
|
-
});
|
49
|
-
});'
|
50
|
-
js.gsub!(/\n\s+/, '')
|
51
|
-
js = "<script>\n#{js}\n</script>" if options[:script_tags]
|
52
|
-
js
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_jqgrid_nav_options
|
57
|
-
@template = MockView.new
|
41
|
+
test "jqgrid nav_options" do
|
58
42
|
|
59
43
|
grid_id = 'grid_id'
|
60
44
|
pager_id = 'pager_id'
|
@@ -69,17 +53,14 @@ class JqgridsHelperTest < Test::Unit::TestCase
|
|
69
53
|
assert_equal(expected, @template.jqgrid_api(grid_id, [grid_options], [:navGrid, "##{pager_id}", {:del => true }, {}, {}, {:closeOnEscape => true}], options))
|
70
54
|
end
|
71
55
|
|
72
|
-
|
73
|
-
@template = MockView.new
|
74
|
-
|
56
|
+
test "pager_id_from_options" do
|
75
57
|
assert_equal 'my_pager_div', @template.send(:pager_id_from_options, {:pager => "jQuery('#my_pager_div')"})
|
76
58
|
assert_equal 'my_pager_div', @template.send(:pager_id_from_options, {:pager => 'jQuery("#my_pager_div")'})
|
77
59
|
assert_equal 'my_pager_div', @template.send(:pager_id_from_options, {:pager => "#my_pager_div"})
|
78
60
|
assert_equal 'my_pager_div', @template.send(:pager_id_from_options, {:pager => "my_pager_div"})
|
79
61
|
end
|
80
62
|
|
81
|
-
|
82
|
-
@template = MockView.new
|
63
|
+
test "chained_functions" do
|
83
64
|
div_id = 'my_grid'
|
84
65
|
nav_button = [:navButtonAdd, '#pager', { :caption => 'Add'}]
|
85
66
|
nav_separator = [:navSeparatorAdd, '#pager']
|
@@ -90,4 +71,71 @@ class JqgridsHelperTest < Test::Unit::TestCase
|
|
90
71
|
|
91
72
|
end
|
92
73
|
|
74
|
+
test "col_model_for_jqgrid should return a valid model for the grid" do
|
75
|
+
columns = ['inv_date', 'amount', 'total' ]
|
76
|
+
result = @template.col_model_for_jqgrid(columns)
|
77
|
+
assert_equal [
|
78
|
+
{:name => 'inv_date', :index => 'inv_date'},
|
79
|
+
{:name => 'amount', :index => 'amount'},
|
80
|
+
{:name => 'total', :index => 'total'}
|
81
|
+
], result
|
82
|
+
end
|
83
|
+
|
84
|
+
test "col_model_for_jqgrid with default width for every column" do
|
85
|
+
columns = ['inv_date', 'amount', 'total' ]
|
86
|
+
result = @template.col_model_for_jqgrid(columns, {:width => 100})
|
87
|
+
assert_equal [
|
88
|
+
{:name => 'inv_date', :index => 'inv_date', :width => 100},
|
89
|
+
{:name => 'amount', :index => 'amount', :width => 100},
|
90
|
+
{:name => 'total', :index => 'total', :width => 100}
|
91
|
+
], result
|
92
|
+
end
|
93
|
+
|
94
|
+
test "col_model_for_jqgrid with explicity width for some columns" do
|
95
|
+
columns = ['inv_date', 'amount', 'total' ]
|
96
|
+
result = @template.col_model_for_jqgrid(columns, {:width => {'inv_date' => 100, 'total' => 150}})
|
97
|
+
assert_equal [
|
98
|
+
{:name => 'inv_date', :index => 'inv_date', :width => 100},
|
99
|
+
{:name => 'amount', :index => 'amount'},
|
100
|
+
{:name => 'total', :index => 'total', :width => 150}
|
101
|
+
], result
|
102
|
+
end
|
103
|
+
|
104
|
+
test "col_model_for_jqgrid with explicity property for some columns" do
|
105
|
+
columns = ['inv_date', 'amount', 'total' ]
|
106
|
+
result = @template.col_model_for_jqgrid(columns, {:property_1 => {'inv_date' => 'high', 'total' => 'low'}, :property_2 => true})
|
107
|
+
assert_equal [
|
108
|
+
{:name => 'inv_date', :index => 'inv_date', :property_1 => 'high', :property_2 => true},
|
109
|
+
{:name => 'amount', :index => 'amount', :property_2 => true},
|
110
|
+
{:name => 'total', :index => 'total', :property_1 => 'low', :property_2 => true}
|
111
|
+
], result
|
112
|
+
end
|
113
|
+
|
114
|
+
test "col_model_for_jqgrid with proc as property" do
|
115
|
+
columns = ['inv_date', 'amount', 'total' ]
|
116
|
+
result = @template.col_model_for_jqgrid(columns, {:property_1 => Proc.new {|c| c.camelize}})
|
117
|
+
assert_equal [
|
118
|
+
{:name => 'inv_date', :property_1 => 'InvDate', :index => 'inv_date'},
|
119
|
+
{:name => 'amount', :property_1 => 'Amount', :index => 'amount'},
|
120
|
+
{:name => 'total', :property_1 => 'Total', :index => 'total'}
|
121
|
+
], result
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def expected_grid grid_id, pager_id, options
|
127
|
+
|
128
|
+
options[:script_tags] ||= true
|
129
|
+
|
130
|
+
js = 'jQuery(document).ready(function() {
|
131
|
+
jQuery("#' + grid_id + '").jqGrid({
|
132
|
+
"pager":jQuery(\'#' + pager_id + '\')
|
133
|
+
});
|
134
|
+
});'
|
135
|
+
js.gsub!(/\n\s+/, '')
|
136
|
+
js = "<script>\n#{js}\n</script>" if options[:script_tags]
|
137
|
+
js
|
138
|
+
|
139
|
+
end
|
140
|
+
|
93
141
|
end
|
metadata
CHANGED
@@ -1,44 +1,35 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: jqgrid_for_rails
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 1
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
version: 1.0.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
6
|
+
authors:
|
12
7
|
- Juan Manuel Cuello
|
13
8
|
autorequire:
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2013-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: will_paginate
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
version: "0"
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
31
20
|
type: :runtime
|
32
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
33
27
|
description: Simple solution to create jqgrids easily using Rails
|
34
28
|
email: juanmacuello@gmail.com
|
35
29
|
executables: []
|
36
|
-
|
37
30
|
extensions: []
|
38
|
-
|
39
31
|
extra_rdoc_files: []
|
40
|
-
|
41
|
-
files:
|
32
|
+
files:
|
42
33
|
- .gitignore
|
43
34
|
- CHANGELOG.rdoc
|
44
35
|
- MIT-LICENSE
|
@@ -62,37 +53,27 @@ files:
|
|
62
53
|
- test/schema.rb
|
63
54
|
- test/test_helper.rb
|
64
55
|
- uninstall.rb
|
65
|
-
has_rdoc: true
|
66
56
|
homepage: http://github.com/Juanmcuello/jqgrid_for_rails
|
67
57
|
licenses: []
|
68
|
-
|
58
|
+
metadata: {}
|
69
59
|
post_install_message:
|
70
60
|
rdoc_options: []
|
71
|
-
|
72
|
-
require_paths:
|
61
|
+
require_paths:
|
73
62
|
- lib
|
74
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
requirements:
|
85
|
-
- - ">="
|
86
|
-
- !ruby/object:Gem::Version
|
87
|
-
segments:
|
88
|
-
- 0
|
89
|
-
version: "0"
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
90
73
|
requirements: []
|
91
|
-
|
92
74
|
rubyforge_project:
|
93
|
-
rubygems_version:
|
75
|
+
rubygems_version: 2.0.3
|
94
76
|
signing_key:
|
95
|
-
specification_version:
|
77
|
+
specification_version: 4
|
96
78
|
summary: Simple solution to create jqgrids easily using Rails
|
97
79
|
test_files: []
|
98
|
-
|