jqgrid_for_rails 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,14 @@
1
+ == 1.0.1
2
+
3
+ * enhancements
4
+ * Add helper to easily create the col_model. (col_model_for_jqgrid)
5
+ * Support prefix in the id column of the json response.
6
+ * Support I18n localization of date fields in the json response.
7
+
8
+ * bug fixes
9
+ * In 'json_for_jqgrid' method do not return nil if +records+ is empty and return the
10
+ json string in spite of that.
11
+
1
12
  == 1.0.0
2
13
 
3
14
  * enhancements
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This is a simple plug-in to create a JqGrid[http://www.trirand.com/blog] javascript code easily inside rails views.
4
4
 
5
- It is extremely flexible, all the ruby code is converted to java script code, so you can use all the jqgrid supported features. It is not an abstraction layer from the jqGrid apis, instead, you type the same code you would do to crate the grid using javascript but in Ruby. The code should follow the jqgrid api definition.
5
+ It is extremely flexible, all the ruby code is converted to java script code, so you can use all the jqgrid supported features. It is not an abstraction layer from the jqGrid apis, instead, you type the same code you would do to create the grid using javascript but in Ruby. The code should follow the jqgrid api definition.
6
6
 
7
7
  For example, to get this java script code:
8
8
 
@@ -115,6 +115,44 @@ Don't forget to include the jquery and jqgrid javascript and stylesheet files!
115
115
 
116
116
  There are convenient methods available in the controllers
117
117
 
118
+ === col_model_for_jqgrid
119
+
120
+ This method easily creates an array to be used as col_model.
121
+
122
+ col_model_for_jqgrid(['inv_date', 'total' ], {:width => 100})
123
+
124
+ #=> [{:name=>"inv_date", :index=>"inv_date", :width=>100}, {:name=>"total", :index=>"total", :width=>100}]
125
+
126
+
127
+ So, in the controller:
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:
140
+
141
+ grid = [{
142
+ :url => '/invoices',
143
+ :datatype => 'json',
144
+ :mtype => 'GET',
145
+ :colNames => ['Inv No','Date'],
146
+ :colModel => @col_model,
147
+ :pager => '#invoices_pager',
148
+ :rowNum => 10,
149
+ :rowList => [10, 20, 30],
150
+ :caption => 'My first grid',
151
+ :onSelectRow => "function() { alert('Row selected!');}".to_json_var
152
+ }]
153
+
154
+ jqgrid_api 'invoices_list', grid
155
+
118
156
  === json_for_jqgrid
119
157
 
120
158
  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].
@@ -6,8 +6,8 @@ module JqgridsHelper
6
6
  #
7
7
  # ==== Parameters
8
8
  #
9
- # * +grid_id+ - This is the id of the html table tag that will contain the grid.
10
- # * +*args+ - Each item in the args array will be translated to a jqgrid api function. Each item
9
+ # * <tt>grid_id</tt> - This is the id of the html table tag that will contain the grid.
10
+ # * <tt>*args</tt> - Each item in the args array will be translated to a jqgrid api function. Each item
11
11
  # should be an array whose elements will be encoded to json to create the jqgrid api function.
12
12
  # All subsequent functions after the first one will be chained.
13
13
  # See the examples for details.
@@ -16,21 +16,19 @@ module JqgridsHelper
16
16
  #
17
17
  # This method accepts an options hash as their last parameter. The accepted options are:
18
18
  #
19
- # [:script_tags]
20
- # If false, <script> tags will not be generated. Default true.
19
+ # * <tt>:script_tags</tt> - If <tt>false</tt>, <tt><script></tt> tags will not be generated. Default +true+.
21
20
  #
22
- # [:on_document_ready]
23
- # If true, all the javascript code will be enclosed inside a +jQuery(document).ready+ function.
21
+ # * <tt>:on_document_ready</tt> - If <tt>true</tt>, all the javascript code will be enclosed inside a <tt>jQuery(document).ready</tt> function.
24
22
  #
25
23
  # ==== Examples
26
24
  #
27
25
  # A simple call to generate a navigation bar:
28
26
  #
29
27
  # jqgrid_api 'invoices_list', [:navGrid, '#invoices_pager', {:search => true, :refresh => false}]
30
- # => jQuery("#invoices_list").jqGrid("navGrid", "#invoices_pager", {"search":true,"refresh":false});
28
+ # #=> jQuery("#invoices_list").jqGrid("navGrid", "#invoices_pager", {"search":true,"refresh":false});
31
29
  #
32
30
  # A more complete example, to create a grid and a pager with a custom button, without the script tags
33
- # and with the js code enclosed inside a a +jQuery(document).ready+ function.
31
+ # and with the js code enclosed inside a <tt>jQuery(document).ready</tt> function.
34
32
  #
35
33
  # options = {:on_document_ready => true, :html_tags => false}
36
34
  #
@@ -85,14 +83,14 @@ module JqgridsHelper
85
83
  #
86
84
  # jQgrid accepts three different formats to set the pager id option.
87
85
  #
88
- # 1 - jQuery('#my_pager_div')
89
- # 2 - #my_pager_div
90
- # 3 - my_pager_div
86
+ # 1. <tt>jQuery('#my_pager_div')</tt>
87
+ # 2. <tt>#my_pager_div</tt>
88
+ # 3. <tt>my_pager_div</tt>
91
89
  #
92
90
  # ==== Example
93
91
  #
94
92
  # pager_id_from_options({:pager => "jQuery('#my_pager_id')"})
95
- # => 'my_pager_id'
93
+ # #=> 'my_pager_id'
96
94
  #
97
95
  def pager_id_from_options options
98
96
  pager_option = options[:pager]
@@ -2,55 +2,131 @@ module JqgridForRails
2
2
  module Controllers
3
3
  # Those helpers are convenience methods added to ApplicationController.
4
4
  module Helpers
5
-
6
- # Returns a json string ready to be send to a jqgrid component.
5
+
6
+ # Returns an array to be used as col_model for the grid where each item
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.
7
11
  #
8
- # +records+ should be the result of an active record query through
9
- # the +paginate+ method offered by will_paginate.
12
+ # * +columns+ - Array with the name of the columns to include in the
13
+ # col_model.
10
14
  #
11
- # +columns+ is an array with the name of the fields in the order they
12
- # should be returned.
15
+ # ==== Options
13
16
  #
14
- # === Options
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.
15
21
  #
16
- # [:id_column]
17
- # Says which is the column that should be used as the row id
22
+ # ==== Examples
18
23
  #
19
- # [:page]
20
- # Says the page number (Deprecated. The page number is now inferred from +records+)
24
+ # col_model_for_jqgrid(['inv_date', 'total' ])
21
25
  #
22
- def json_for_jqgrid records, columns = nil, options = {}
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
+ #
40
+ def col_model_for_jqgrid columns, options = {}
41
+ columns.map do |c|
42
+ h = {:name => c, :index => c}
43
+ h.merge property_from_options(c, options)
44
+ end
23
45
 
24
- return if records.empty?
46
+ end
25
47
 
26
- columns ||= records.first.attributes.keys
48
+ # Returns a json string ready to be sent to a jqgrid component.
49
+ #
50
+ # * +records+ - Should be the result of an active record query through
51
+ # the +paginate+ method offered by will_paginate.
52
+ # * +columns+ - Array with the name of the fields in the order they
53
+ # should be returned.
54
+ #
55
+ # ==== Options
56
+ #
57
+ # * <tt>:id_column</tt> - Says which is the column that should be used as
58
+ # the row id.
59
+ #
60
+ # * <tt>:id_prefix</tt> - If specified, the +column_id+ option will be
61
+ # concatenated to this prefix. This helps to keep the html id unique in
62
+ # the document.
63
+ #
64
+ # * <tt>:translate</tt> - Array with the name of the fields that should be
65
+ # localized with the <tt>I18n.l</tt> method. The field content must be
66
+ # of any class accepted by the method. I.e. Time, DateTime, Date.
67
+ #
68
+ # * <tt>:page</tt> - Says the page number (Deprecated. The page number is
69
+ # now inferred from +records+.
70
+ #
71
+ def json_for_jqgrid records, columns = nil, options = {}
72
+
73
+ columns ||= (records.first.attributes.keys rescue [])
27
74
 
28
75
  options[:id_column] ||= columns.first
29
76
  options[:page] ||= records.current_page
30
77
 
31
- { :page => options[:page],
32
- :total => records.total_pages,
33
- :records => records.total_entries,
34
- :rows => records.map do |r| {
35
- :id => r.attributes[options[:id_column]],
36
- :cell => r.attributes.values_at(*columns)}
37
- end
78
+ { :page => options[:page],
79
+ :total => records.total_pages,
80
+ :records => records.total_entries,
81
+ :rows => records.map {|r| row_from_record(r, columns, options)}
38
82
  }.to_json
39
83
  end
40
84
 
41
85
  # Returns the 'order by' string created using the params received from the jqgrid.
42
86
  #
43
- # === Example
87
+ # ==== Example
44
88
  #
45
89
  # order_by_from_params({'sidx' => 'updated_at', 'sord' => 'asc'})
46
- # => 'updated_at asc'
90
+ # #=> 'updated_at asc'
47
91
  #
48
92
  def order_by_from_params params
49
93
  order_by = params['sidx'] unless params['sidx'].blank?
50
94
  order_by << " #{params['sord']}" if params['sord'] && order_by
51
95
  order_by
52
96
  end
53
-
97
+
98
+ private
99
+
100
+ # Returns a hash with the values for a jqgrid json row.
101
+ def row_from_record(r, columns, options)
102
+ attribs = r.attributes
103
+
104
+ # Localize Date, Time or DateTime fields
105
+ locale_classes = [Time, Date, DateTime]
106
+ if options[:translate].is_a?(Array) && I18n
107
+ options[:translate].each do |col|
108
+ attribs[col.to_s] = I18n.l(attribs[col.to_s]) if locale_classes.include?(attribs[col.to_s].class)
109
+ end
110
+ end
111
+
112
+ {:id => "#{options[:id_prefix]}#{attribs[options[:id_column]]}",
113
+ :cell => attribs.values_at(*columns) }
114
+ end
115
+
116
+ def property_from_options col_name, options
117
+ h = {}
118
+ options.each do |k, v|
119
+ case v.class.to_s
120
+ when "Proc"
121
+ h[k] = v.call(col_name)
122
+ when "Hash"
123
+ h[k] = v[col_name] if v.has_key?(col_name)
124
+ else
125
+ h[k] = v
126
+ end
127
+ end
128
+ h
129
+ end
54
130
  end
55
131
  end
56
132
  end
@@ -1,4 +1,4 @@
1
1
  module JqgridForRails
2
- VERSION = "1.0.0".freeze
2
+ VERSION = "1.0.1".freeze
3
3
  end
4
4
 
@@ -31,4 +31,77 @@ class ControllerHelpersTest < ActionController::TestCase
31
31
  assert_nil @controller.order_by_from_params(params)
32
32
  end
33
33
 
34
+ test "json_for_grid with empty records result" do
35
+ records = Invoice.paginate(:page => 1)
36
+ json = @controller.json_for_jqgrid(records)
37
+ hash = ActiveSupport::JSON.decode(json)
38
+ assert_equal 0, hash['total']
39
+ assert_equal [], hash['rows']
40
+ assert_equal 1, hash['page']
41
+ assert_equal 0, hash['records']
42
+ end
43
+
44
+ test "json_for_grid with one record and id prefix" do
45
+ tmp_record = Invoice.create({:invid => 1, :invdate => '2011-01-01 00:00:00', :amount => 10, :tax => 1, :total => 11, :note => '' })
46
+ records = Invoice.paginate(:page => 1)
47
+ json = @controller.json_for_jqgrid(records, ['invdate', 'amount', 'total' ], {:id_column => 'invid', :id_prefix => 'mygrid_row_'})
48
+ hash = ActiveSupport::JSON.decode(json)
49
+ Invoice.delete(tmp_record.id)
50
+ assert_equal hash["total"], 1
51
+ assert_equal hash["page"], 1
52
+ assert_equal hash["records"], 1
53
+ assert_equal hash["rows"][0]["cell"], [Time.utc('2011-01-01 00:00:00'), 10.0, 11.0]
54
+ assert_equal hash["rows"][0]["id"], "mygrid_row_1"
55
+ end
56
+
57
+ test "col_model_for_jqgrid should return a valid model for the grid" do
58
+ columns = ['inv_date', 'amount', 'total' ]
59
+ result = @controller.col_model_for_jqgrid(columns)
60
+ assert_equal [
61
+ {:name => 'inv_date', :index => 'inv_date'},
62
+ {:name => 'amount', :index => 'amount'},
63
+ {:name => 'total', :index => 'total'}
64
+ ], result
65
+ end
66
+
67
+ test "col_model_for_jqgrid with default width for every column" do
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
75
+ end
76
+
77
+ test "col_model_for_jqgrid with explicity width for some columns" do
78
+ columns = ['inv_date', 'amount', 'total' ]
79
+ result = @controller.col_model_for_jqgrid(columns, {:width => {'inv_date' => 100, 'total' => 150}})
80
+ assert_equal [
81
+ {:name => 'inv_date', :index => 'inv_date', :width => 100},
82
+ {:name => 'amount', :index => 'amount'},
83
+ {:name => 'total', :index => 'total', :width => 150}
84
+ ], result
85
+ end
86
+
87
+ test "col_model_for_jqgrid with explicity property for some columns" do
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
95
+ end
96
+
97
+ test "col_model_for_jqgrid with proc as property" do
98
+ columns = ['inv_date', 'amount', 'total' ]
99
+ result = @controller.col_model_for_jqgrid(columns, {:property_1 => Proc.new {|c| c.camelize}})
100
+ assert_equal [
101
+ {:name => 'inv_date', :property_1 => 'InvDate', :index => 'inv_date'},
102
+ {:name => 'amount', :property_1 => 'Amount', :index => 'amount'},
103
+ {:name => 'total', :property_1 => 'Total', :index => 'total'}
104
+ ], result
105
+ end
106
+
34
107
  end
@@ -81,13 +81,12 @@ class JqgridsHelperTest < Test::Unit::TestCase
81
81
  def test_chained_functions
82
82
  @template = MockView.new
83
83
  div_id = 'my_grid'
84
- functions = [
85
- [:navButtonAdd, '#pager', { :caption => 'Add'}],
86
- [:navSeparatorAdd, '#pager']]
84
+ nav_button = [:navButtonAdd, '#pager', { :caption => 'Add'}]
85
+ nav_separator = [:navSeparatorAdd, '#pager']
87
86
 
88
87
  expected = 'jQuery("#'+div_id+'").jqGrid("navButtonAdd", "#pager", {"caption":"Add"}).jqGrid("navSeparatorAdd", "#pager");'
89
88
 
90
- assert_equal expected, @template.jqgrid_api(div_id, *functions, {:script_tags => false})
89
+ assert_equal expected, @template.jqgrid_api(div_id, nav_button, nav_separator, {:script_tags => false})
91
90
 
92
91
  end
93
92
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Juan Manuel Cuello
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-30 00:00:00 -03:00
17
+ date: 2011-04-13 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency