Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cassiomarques/enumerate_it
Browse files Browse the repository at this point in the history
  • Loading branch information
cassiomarques committed Apr 27, 2012
2 parents 0421b53 + e41ee6c commit 2f13152
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion lib/enumerate_it.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/enumerate_it_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 2f13152

Please sign in to comment.