|
| 1 | +require 'test_helper' |
| 2 | +require 'syck' |
| 3 | + |
| 4 | +describe Enumerize do |
| 5 | + let(:klass) do |
| 6 | + Class.new do |
| 7 | + include Enumerize |
| 8 | + end |
| 9 | + end |
| 10 | + |
| 11 | + let(:object) { klass.new } |
| 12 | + |
| 13 | + it 'defines method that returns nil' do |
| 14 | + klass.enumerize(:foo, :in => [:a, :b]) |
| 15 | + object.foo.must_equal nil |
| 16 | + end |
| 17 | + |
| 18 | + it 'defines setter method' do |
| 19 | + klass.enumerize(:foo, :in => [:a, :b]) |
| 20 | + object.foo = :a |
| 21 | + object.foo.must_equal 'a' |
| 22 | + end |
| 23 | + |
| 24 | + it 'returns translation' do |
| 25 | + I18n.backend.store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) |
| 26 | + klass.enumerize(:foo, :in => [:a, :b]) |
| 27 | + object.foo = :a |
| 28 | + object.foo.text.must_equal 'a text' |
| 29 | + object.foo_text.must_equal 'a text' |
| 30 | + end |
| 31 | + |
| 32 | + it 'scopes translation by i18 key' do |
| 33 | + I18n.backend.store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}}) |
| 34 | + def klass.model_name |
| 35 | + name = "ExampleClass" |
| 36 | + def name.i18n_key |
| 37 | + 'example_class' |
| 38 | + end |
| 39 | + |
| 40 | + name |
| 41 | + end |
| 42 | + klass.enumerize(:foo, :in => [:a, :b]) |
| 43 | + object.foo = :a |
| 44 | + object.foo.text.must_equal 'a text scoped' |
| 45 | + object.foo_text.must_equal 'a text scoped' |
| 46 | + end |
| 47 | + |
| 48 | + it 'returns options for select' do |
| 49 | + I18n.backend.store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) |
| 50 | + klass.enumerize(:foo, :in => [:a, :b]) |
| 51 | + klass.foo.options.must_equal [['a text', 'a'], ['b text', 'b']] |
| 52 | + end |
| 53 | + |
| 54 | + it 'dumpes value to yaml using' do |
| 55 | + klass.enumerize(:foo, :in => [:a, :b]) |
| 56 | + object.foo = :a |
| 57 | + YAML.dump(object.foo).must_equal YAML.dump('a') |
| 58 | + end |
| 59 | +end |
0 commit comments