Skip to content

Commit

Permalink
:format option added to json_for_jqgrid helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanmcuello committed Jun 10, 2011
1 parent 1cec459 commit 62969c0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Expand Up @@ -3,6 +3,7 @@
* Allow the column name in the 'order_by' helper to be quoted.
* View helpers are now tested using ActionView::TestCase.
* col_model_for_jqgrid is moved from controller helpers to view helpers.
* :format option added to json_for_jqgrid helper.

* bug fixes
* Fixes issue when using id_column in json_for_jqgrid method.
Expand Down
45 changes: 38 additions & 7 deletions lib/jqgrid_for_rails/controllers/helpers.rb
Expand Up @@ -33,6 +33,11 @@ def col_model_for_jqgrid columns, options = {}
# localized with the <tt>I18n.l</tt> method. The field content must be
# of any class accepted by the method. I.e. Time, DateTime, Date.
#
# * <tt>:format</tt> - Says the proc and the columns where to apply it. It
# can be used to change the format of the field as well as to run any other
# code on it. If the key of the hash is not a column name but an array of
# column names, the proc is applied to all of them.
#
# * <tt>:page</tt> - Says the page number (Deprecated. The page number is
# now inferred from +records+.
#
Expand All @@ -44,6 +49,13 @@ def col_model_for_jqgrid columns, options = {}
#
# # => {"rows":[{"cell":["2011-01-01T00:00:00Z",10.0,11.0],"id":"invid_1"}],"total":1,"page":1,"records":1}
#
# records = Invoice.paginate(:page => 1)
# my_format = Proc.new {|val| val + 100 }
# json_for_jqgrid(records, ['invdate', 'amount', 'total' ], {:format => { 'total' => my_format })
#
# # Because of the :format option, 100 is added to 'total' field.
# # => {"rows":[{"cell":["2011-01-01T00:00:00Z",10.0,111.0],"id":"invid_1"}],"total":2,"page":1,"records":1}
#
def json_for_jqgrid records, columns = nil, options = {}

columns ||= (records.first.attributes.keys rescue [])
Expand Down Expand Up @@ -86,13 +98,9 @@ def quote value, quote_char
def row_from_record(r, columns, options)
attribs = r.attributes

# Localize Date, Time or DateTime fields
locale_classes = [Time, Date, DateTime]
if options[:translate].is_a?(Array) && I18n
options[:translate].each do |col|
attribs[col.to_s] = I18n.l(attribs[col.to_s]) if locale_classes.include?(attribs[col.to_s].class)
end
end
localize(attribs, options[:translate]) if options[:translate]

format(attribs, options[:format]) if options[:format]

{:id => "#{options[:id_prefix]}#{id_column_from_options(r, options)}",
:cell => attribs.values_at(*columns) }
Expand Down Expand Up @@ -121,6 +129,29 @@ def property_from_options col_name, options
h
end

# Localize Date, Time or DateTime fields
def localize attribs, translate
locale_classes = [Time, Date, DateTime]
if translate.is_a?(Array) && I18n
translate.each do |col|
attribs[col.to_s] = I18n.l(attribs[col.to_s]) if locale_classes.include?(attribs[col.to_s].class)
end
end
end

# Format fields
def format attribs, format
if format.is_a?(Hash)
format.each do |col, pr|
if col.is_a?(Array)
col.each {|c| attribs[c] = pr.call(attribs[c])}
else
attribs[col] = pr.call(attribs[col])
end
end
end
end

end
end
end
Expand Down
24 changes: 24 additions & 0 deletions test/controllers/helpers_test.rb
Expand Up @@ -70,4 +70,28 @@ def setup
assert_equal 'invid_1', hash["rows"].first["id"]
end

test "json_for_jqgrid should allow to format field with a proc" do
tmp_record = Invoice.create({:invid => 1, :invdate => '2011-01-01 00:00:00', :amount => 10, :tax => 1, :total => 11, :note => '123,456.123' })
records = Invoice.paginate(:page => 1)
my_proc = Proc.new { |r| (r.gsub(/,/, '').to_f) }
json = @controller.json_for_jqgrid(records, ['invdate', 'amount', 'total', 'note' ], {:format => {'note' => my_proc }})
hash = ActiveSupport::JSON.decode(json)
Invoice.delete(tmp_record.id)

assert_equal 123456.123, hash["rows"].first["cell"].last
end

test "json_for_jqgrid should allow to format many fields with a proc" do
tmp_record = Invoice.create({:invid => 1, :invdate => '2011-01-01 00:00:00', :amount => 10, :tax => 1, :total => 11, :note => '123,456.123' })
records = Invoice.paginate(:page => 1)
my_proc = Proc.new { |r| (r * 2) }
json = @controller.json_for_jqgrid(records, ['invdate', 'amount', 'total', 'note' ], {:format => {['amount', 'total'] => my_proc }})
hash = ActiveSupport::JSON.decode(json)
Invoice.delete(tmp_record.id)

assert_equal 20, hash["rows"].first["cell"][1]
assert_equal 22, hash["rows"].first["cell"][2]
end


end

0 comments on commit 62969c0

Please sign in to comment.