Permalink
Browse files

Merge pull request #22 from mhfs/to_json

Add EnumerateIt::Base.to_json
  • Loading branch information...
2 parents 3df97b7 + f36ba82 commit e41ee6cb23b1c39b7c95d4598fe538a6ef31429a @cassiomarques cassiomarques committed Apr 27, 2012
Showing with 17 additions and 0 deletions.
  1. +4 −0 lib/enumerate_it.rb
  2. +12 −0 spec/enumerate_it_spec.rb
  3. +1 −0 spec/spec_helper.rb
View
4 lib/enumerate_it.rb
@@ -224,6 +224,10 @@ def self.to_a
enumeration.values.map {|value| [translate(value[1]), value[0]] }.sort_by { |value| value[0] }
end
+ def self.to_json
+ enumeration.values.collect {|value| { value: value[0], label: translate(value[1]) } }.to_json
+ end
+
def self.t(value)
target = to_a.detect { |item| item[1] == value }
target ? target[0] : value
View
12 spec/enumerate_it_spec.rb
@@ -232,6 +232,18 @@ def self.scope(name, whatever); end
end
end
+ describe ".to_json" do
+ it "gives a valid json back" do
+ I18n.locale = :inexsistent
+ TestEnumerationWithoutArray.to_json.should == '[{"value":"1","label":"Value One"},{"value":"2","label":"Value Two"}]'
+ end
+
+ it "give translated values when available" do
+ I18n.locale = :pt
+ TestEnumerationWithoutArray.to_json.should == '[{"value":"1","label":"Primeiro Valor"},{"value":"2","label":"Value Two"}]'
+ end
+ end
+
describe ".t" do
it "translates a given value" do
I18n.locale = :pt
View
1 spec/spec_helper.rb
@@ -7,5 +7,6 @@
require 'rubygems'
require "active_support"
require "active_support/core_ext/string/inflections"
+require 'active_support/core_ext/object/to_json'
I18n.load_path = Dir['spec/i18n/*.yml']

0 comments on commit e41ee6c

Please sign in to comment.