Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:cassiomarques/enumerate_it

  • Loading branch information...
commit 2f131524a68b173c5c6fd38de77effc3675c38dc 2 parents 0421b53 + e41ee6c
@cassiomarques authored
View
2  README.rdoc
@@ -225,7 +225,7 @@ located on enumerations.'enumeration_name'.'key' :
You can also translate specific values:
- RelationshipStatis.t(1) # => 'Casado'
+ RelationshipStatus.t(1) # => 'Casado'
== Installation
View
6 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
@@ -319,7 +323,7 @@ def create_helper_methods(klass, attribute_name)
def create_scopes(klass, attribute_name)
klass.enumeration.keys.each do |option|
if respond_to? :scope
- scope option, where(attribute_name => klass.enumeration[option].first)
+ scope option, lambda { where(attribute_name => klass.enumeration[option].first)}
end
end
end
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']
Please sign in to comment.
Something went wrong with that request. Please try again.