enumerize 0.3.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -1
  3. data/.rspec +2 -0
  4. data/.travis.yml +38 -11
  5. data/CHANGELOG.md +258 -0
  6. data/Gemfile +5 -11
  7. data/Gemfile.global +20 -0
  8. data/Gemfile.mongo_mapper +7 -0
  9. data/Gemfile.rails42 +7 -0
  10. data/Gemfile.rails50 +7 -0
  11. data/Gemfile.rails52 +7 -0
  12. data/README.md +405 -17
  13. data/Rakefile +7 -1
  14. data/enumerize.gemspec +6 -3
  15. data/lib/enumerize/activemodel.rb +47 -0
  16. data/lib/enumerize/activerecord.rb +127 -2
  17. data/lib/enumerize/attribute.rb +167 -7
  18. data/lib/enumerize/attribute_map.rb +4 -0
  19. data/lib/enumerize/base.rb +60 -61
  20. data/lib/enumerize/hooks/formtastic.rb +11 -12
  21. data/lib/enumerize/hooks/sequel_dataset.rb +17 -0
  22. data/lib/enumerize/hooks/simple_form.rb +21 -8
  23. data/lib/enumerize/hooks/uniqueness.rb +22 -0
  24. data/lib/enumerize/integrations/rails_admin.rb +18 -0
  25. data/lib/enumerize/integrations/rspec/matcher.rb +161 -0
  26. data/lib/enumerize/integrations/rspec.rb +19 -0
  27. data/lib/enumerize/module.rb +33 -0
  28. data/lib/enumerize/module_attributes.rb +3 -2
  29. data/lib/enumerize/mongoid.rb +29 -0
  30. data/lib/enumerize/predicatable.rb +23 -0
  31. data/lib/enumerize/predicates.rb +76 -0
  32. data/lib/enumerize/scope/activerecord.rb +49 -0
  33. data/lib/enumerize/scope/mongoid.rb +46 -0
  34. data/lib/enumerize/scope/sequel.rb +52 -0
  35. data/lib/enumerize/sequel.rb +62 -0
  36. data/lib/enumerize/set.rb +81 -0
  37. data/lib/enumerize/utils.rb +12 -0
  38. data/lib/enumerize/value.rb +19 -46
  39. data/lib/enumerize/version.rb +3 -1
  40. data/lib/enumerize.rb +56 -4
  41. data/lib/sequel/plugins/enumerize.rb +18 -0
  42. data/spec/enumerize/integrations/rspec/matcher_spec.rb +260 -0
  43. data/spec/spec_helper.rb +30 -0
  44. data/test/activemodel_test.rb +114 -0
  45. data/test/activerecord_test.rb +542 -8
  46. data/test/attribute_map_test.rb +2 -0
  47. data/test/attribute_test.rb +102 -4
  48. data/test/base_test.rb +61 -39
  49. data/test/formtastic_test.rb +102 -17
  50. data/test/module_attributes_test.rb +25 -2
  51. data/test/mongo_mapper_test.rb +84 -0
  52. data/test/mongoid_test.rb +98 -7
  53. data/test/multiple_test.rb +59 -0
  54. data/test/predicates_test.rb +65 -0
  55. data/test/rails_admin_test.rb +27 -0
  56. data/test/sequel_test.rb +341 -0
  57. data/test/set_test.rb +166 -0
  58. data/test/simple_form_test.rb +110 -4
  59. data/test/support/mock_controller.rb +11 -1
  60. data/test/support/shared_enums.rb +43 -0
  61. data/test/support/view_test_helper.rb +6 -0
  62. data/test/test_helper.rb +25 -6
  63. data/test/value_test.rb +102 -13
  64. metadata +62 -28
  65. data/gemfiles/Gemfile-ar-3.1.x +0 -13
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  describe Enumerize::Attribute do
@@ -14,15 +16,64 @@ describe Enumerize::Attribute do
14
16
  attr.values.must_equal %w[a b]
15
17
  end
16
18
 
19
+ it 'returns frozen values' do
20
+ build_attr nil, :foo, :in => [:a, :b]
21
+ attr.values.map(&:frozen?).must_equal [true, true]
22
+ end
23
+
17
24
  it 'converts name to symbol' do
18
25
  build_attr nil, 'foo', :in => %w[a b]
19
26
  attr.name.must_equal :foo
20
27
  end
21
28
 
22
- it 'returns options for select' do
23
- store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
24
- build_attr nil, :foo, :in => %w[a b]
25
- attr.options.must_equal [['a text', 'a'], ['b text', 'b']]
29
+ it 'uses custom value class' do
30
+ value_class = Class.new(Enumerize::Value)
31
+ build_attr nil, 'foo', :in => %w[a b], :value_class => value_class
32
+ attr.values.first.must_be_instance_of value_class
33
+ end
34
+
35
+ describe 'i18n scopes' do
36
+ it 'returns scopes from options' do
37
+ build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
38
+ attr.i18n_scopes.must_equal %w[bar buzz]
39
+ end
40
+
41
+ it 'accepts only string scopes' do
42
+ proc { build_attr nil, 'foo', :in => %w[a b], :i18n_scope => [%w[bar buzz], "bar.buzz"] }.must_raise ArgumentError
43
+ end
44
+ end
45
+
46
+ describe 'options for select' do
47
+ it 'returns all options for select' do
48
+ store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
49
+ build_attr nil, :foo, :in => %w[a b]
50
+ attr.options.must_equal [['a text', 'a'], ['b text', 'b']]
51
+ end
52
+ end
53
+
54
+ it 'returns requested options for select via :only' do
55
+ store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
56
+ build_attr nil, :foo, :in => %w[a b]
57
+ attr.options(:only => :a).must_equal [['a text', 'a']]
58
+ attr.options(:only => [:b]).must_equal [['b text', 'b']]
59
+ attr.options(:only => []).must_equal []
60
+ end
61
+ end
62
+
63
+ it 'returns requested options for select via :except' do
64
+ store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
65
+ build_attr nil, :foo, :in => %w[a b]
66
+ attr.options(:except => :a).must_equal [['b text', 'b']]
67
+ attr.options(:except => :b).must_equal [['a text', 'a']]
68
+ attr.options(:except => []).must_equal [['a text', 'a'], ['b text', 'b']]
69
+ end
70
+ end
71
+
72
+ it 'does not work with both :only and :except' do
73
+ store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
74
+ build_attr nil, :foo, :in => %w[a b]
75
+ proc { attr.options(:except => [], :only => []) }.must_raise ArgumentError
76
+ end
26
77
  end
27
78
  end
28
79
 
@@ -40,4 +91,51 @@ describe Enumerize::Attribute do
40
91
  attr.find_value(2).must_equal 'b'
41
92
  end
42
93
  end
94
+
95
+ it 'sets up shortcut methods for each value' do
96
+ build_attr nil, :foo, :in => {:a => 1, :b => 2}
97
+
98
+ attr.must_respond_to :a
99
+ attr.must_respond_to :b
100
+
101
+ attr.a.value.must_equal 1
102
+ attr.b.value.must_equal 2
103
+ attr.a.text.must_equal 'A'
104
+ attr.b.text.must_equal 'B'
105
+ end
106
+
107
+ describe 'values hash with zero' do
108
+ before do
109
+ build_attr nil, :foo, :in => {:a => 1, :b => 2, :c => 0}
110
+ end
111
+
112
+ it 'returns hash keys as values' do
113
+ attr.values.must_equal %w[a b c]
114
+ end
115
+
116
+ it 'finds values by hash values' do
117
+ attr.find_value(1).must_equal 'a'
118
+ attr.find_value(2).must_equal 'b'
119
+ attr.find_value(0).must_equal 'c'
120
+ end
121
+
122
+ it 'finds all values by hash values' do
123
+ attr.find_values(1, 2, 0).must_equal ['a', 'b', 'c']
124
+ end
125
+ end
126
+
127
+ describe 'boolean values hash' do
128
+ before do
129
+ build_attr nil, :foo, :in => {:a => true, :b => false}
130
+ end
131
+
132
+ it 'returns hash keys as values' do
133
+ attr.values.must_equal %w[a b]
134
+ end
135
+
136
+ it 'finds values by hash values' do
137
+ attr.find_value(true).must_equal 'a'
138
+ attr.find_value(false).must_equal 'b'
139
+ end
140
+ end
43
141
  end
data/test/base_test.rb CHANGED
@@ -1,32 +1,34 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  describe Enumerize::Base do
4
- let(:klass) do
6
+ let(:kklass) do
5
7
  Class.new do
6
- include Enumerize
8
+ extend Enumerize
7
9
  end
8
10
  end
9
11
 
10
12
  let(:subklass) do
11
- Class.new(klass)
13
+ Class.new(kklass)
12
14
  end
13
15
 
14
- let(:object) { klass.new }
16
+ let(:object) { kklass.new }
15
17
 
16
18
  it 'returns nil when not set' do
17
- klass.enumerize(:foo, :in => [:a, :b])
18
- object.foo.must_equal nil
19
+ kklass.enumerize(:foo, :in => [:a, :b])
20
+ object.foo.must_be_nil
19
21
  end
20
22
 
21
23
  it 'returns value that was set' do
22
- klass.enumerize(:foo, :in => [:a, :b])
24
+ kklass.enumerize(:foo, :in => [:a, :b])
23
25
  object.foo = :a
24
26
  object.foo.must_equal 'a'
25
27
  end
26
28
 
27
29
  it 'returns translation' do
28
30
  store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
29
- klass.enumerize(:foo, :in => [:a, :b])
31
+ kklass.enumerize(:foo, :in => [:a, :b])
30
32
  object.foo = :a
31
33
  object.foo.text.must_equal 'a text'
32
34
  object.foo_text.must_equal 'a text'
@@ -36,14 +38,14 @@ describe Enumerize::Base do
36
38
 
37
39
  it 'returns nil as translation when value is nil' do
38
40
  store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
39
- klass.enumerize(:foo, :in => [:a, :b])
40
- object.foo_text.must_equal nil
41
+ kklass.enumerize(:foo, :in => [:a, :b])
42
+ object.foo_text.must_be_nil
41
43
  end
42
44
  end
43
45
 
44
- it 'scopes translation by i18 key' do
45
- def klass.model_name
46
- name = "ExampleClass"
46
+ it 'scopes translation by i18n key' do
47
+ def kklass.model_name
48
+ name = String.new("ExampleClass")
47
49
  def name.i18n_key
48
50
  'example_class'
49
51
  end
@@ -52,7 +54,7 @@ describe Enumerize::Base do
52
54
  end
53
55
 
54
56
  store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}}) do
55
- klass.enumerize(:foo, :in => [:a, :b])
57
+ kklass.enumerize(:foo, :in => [:a, :b])
56
58
  object.foo = :a
57
59
  object.foo.text.must_equal 'a text scoped'
58
60
  object.foo_text.must_equal 'a text scoped'
@@ -61,80 +63,90 @@ describe Enumerize::Base do
61
63
 
62
64
  it 'returns humanized value if there are no translations' do
63
65
  store_translations(:en, :enumerize => {}) do
64
- klass.enumerize(:foo, :in => [:a, :b])
66
+ kklass.enumerize(:foo, :in => [:a, :b])
65
67
  object.foo = :a
66
68
  object.foo_text.must_equal 'A'
67
69
  end
68
70
  end
69
71
 
70
72
  it 'stores value as string' do
71
- klass.enumerize(:foo, :in => [:a, :b])
73
+ kklass.enumerize(:foo, :in => [:a, :b])
72
74
  object.foo = :a
73
75
  object.instance_variable_get(:@foo).must_be_instance_of String
74
76
  end
75
77
 
76
78
  it 'handles default value' do
77
- klass.enumerize(:foo, :in => [:a, :b], :default => :b)
79
+ kklass.enumerize(:foo, :in => [:a, :b], :default => :b)
80
+ object.foo.must_equal 'b'
81
+ end
82
+
83
+ it 'handles default value with lambda' do
84
+ kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { :b })
85
+ object.foo.must_equal 'b'
86
+ end
87
+
88
+ it 'injects object instance into lamda default value' do
89
+ kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { |obj| :b if obj.is_a? kklass })
78
90
  object.foo.must_equal 'b'
79
91
  end
80
92
 
81
93
  it 'raises exception on invalid default value' do
82
94
  proc {
83
- klass.enumerize(:foo, :in => [:a, :b], :default => :c)
95
+ kklass.enumerize(:foo, :in => [:a, :b], :default => :c)
84
96
  }.must_raise ArgumentError
85
97
  end
86
98
 
87
99
  it 'has enumerized attributes' do
88
- klass.enumerized_attributes.must_be_empty
89
- klass.enumerize(:foo, :in => %w[a b])
90
- klass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
100
+ kklass.enumerized_attributes.must_be_empty
101
+ kklass.enumerize(:foo, :in => %w[a b])
102
+ kklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
91
103
  end
92
104
 
93
105
  it "doesn't override existing method" do
94
- method = klass.method(:name)
95
- klass.enumerize(:name, :in => %w[a b], :default => 'a')
96
- klass.method(:name).must_equal method
106
+ method = kklass.method(:name)
107
+ kklass.enumerize(:name, :in => %w[a b], :default => 'a')
108
+ kklass.method(:name).must_equal method
97
109
  end
98
110
 
99
111
  it "inherits enumerized attributes from a parent class" do
100
- klass.enumerize(:foo, :in => %w[a b])
112
+ kklass.enumerize(:foo, :in => %w[a b])
101
113
  subklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
102
114
  end
103
115
 
104
116
  it "inherits enumerized attributes from a grandparent class" do
105
- klass.enumerize(:foo, :in => %w[a b])
117
+ kklass.enumerize(:foo, :in => %w[a b])
106
118
  Class.new(subklass).enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
107
119
  end
108
120
 
109
121
  it "doesn't add enumerized attributes to parent class" do
110
- klass.enumerize(:foo, :in => %w[a b])
122
+ kklass.enumerize(:foo, :in => %w[a b])
111
123
  subklass.enumerize(:bar, :in => %w[c d])
112
124
 
113
- klass.enumerized_attributes[:bar].must_equal nil
125
+ kklass.enumerized_attributes[:bar].must_be_nil
114
126
  end
115
127
 
116
128
  it 'adds new parent class attributes to subclass' do
117
- subklass = Class.new(klass)
118
- klass.enumerize :foo, :in => %w[a b]
129
+ subklass = Class.new(kklass)
130
+ kklass.enumerize :foo, :in => %w[a b]
119
131
  subklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
120
132
  end
121
133
 
122
134
  it 'stores nil value' do
123
- klass.enumerize(:foo, :in => [:a, :b])
135
+ kklass.enumerize(:foo, :in => [:a, :b])
124
136
  object.foo = nil
125
- object.instance_variable_get(:@foo).must_equal nil
137
+ object.instance_variable_get(:@foo).must_be_nil
126
138
  end
127
139
 
128
140
  it 'casts value to string for validation' do
129
- klass.enumerize(:foo, :in => [:a, :b])
141
+ kklass.enumerize(:foo, :in => [:a, :b])
130
142
  object.foo = :c
131
143
  object.read_attribute_for_validation(:foo).must_equal 'c'
132
144
  end
133
145
 
134
146
  it "doesn't cast nil to string for validation" do
135
- klass.enumerize(:foo, :in => [:a, :b])
147
+ kklass.enumerize(:foo, :in => [:a, :b])
136
148
  object.foo = nil
137
- object.read_attribute_for_validation(:foo).must_equal nil
149
+ object.read_attribute_for_validation(:foo).must_be_nil
138
150
  end
139
151
 
140
152
  it 'calls super in the accessor method' do
@@ -154,22 +166,22 @@ describe Enumerize::Base do
154
166
 
155
167
  klass = Class.new do
156
168
  include accessors
157
- include Enumerize
169
+ extend Enumerize
158
170
 
159
171
  enumerize :foo, :in => %w[test]
160
172
  end
161
173
 
162
174
  object = klass.new
163
175
  object.foo.must_be_nil
164
- object.attributes.must_equal({:foo => nil})
176
+ object.attributes.must_equal({})
165
177
 
166
178
  object.foo = 'test'
167
179
  object.foo.must_equal 'test'
168
180
  object.attributes.must_equal(:foo => 'test')
169
181
  end
170
182
 
171
- it 'returns stores hash values' do
172
- klass.enumerize(:foo, :in => {:a => 1, :b => 2})
183
+ it 'stores hash values' do
184
+ kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
173
185
 
174
186
  object.foo = :a
175
187
  object.instance_variable_get(:@foo).must_equal 1
@@ -179,4 +191,14 @@ describe Enumerize::Base do
179
191
  object.instance_variable_get(:@foo).must_equal 2
180
192
  object.foo.must_equal 'b'
181
193
  end
194
+
195
+ it 'returns custom value' do
196
+ kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
197
+
198
+ object.foo = :a
199
+ object.foo_value.must_equal 1
200
+
201
+ object.foo = :b
202
+ object.foo_value.must_equal 2
203
+ end
182
204
  end
@@ -1,31 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
- if defined? Formtastic::Helpers::InputHelper
4
- module Formtastic
5
- module Helpers
6
- module InputHelper
7
- remove_method :input_class
8
- def input_class(as)
9
- input_class_with_const_defined(as)
10
- end
11
- end
12
- end
13
- end
14
- end
5
+ Formtastic::FormBuilder.action_class_finder = Formtastic::ActionClassFinder
6
+ Formtastic::FormBuilder.input_class_finder = Formtastic::InputClassFinder
15
7
 
16
8
  class FormtasticSpec < MiniTest::Spec
17
9
  include ViewTestHelper
18
- if defined? Formtastic::SemanticFormHelper
19
- include Formtastic::SemanticFormHelper
20
- else
21
- include Formtastic::Helpers::FormHelper
10
+ include Formtastic::Helpers::FormHelper
11
+
12
+ class Thing < Struct.new(:name)
13
+ extend ActiveModel::Naming
14
+ include ActiveModel::Conversion
15
+
16
+ def persisted?
17
+ false
18
+ end
22
19
  end
23
20
 
24
21
  class User < Struct.new(:sex, :age)
25
22
  extend ActiveModel::Naming
26
23
  include ActiveModel::Conversion
27
24
 
28
- include Enumerize
25
+ extend Enumerize
29
26
 
30
27
  enumerize :sex, :in => [:male, :female]
31
28
 
@@ -34,10 +31,30 @@ class FormtasticSpec < MiniTest::Spec
34
31
  end
35
32
  end
36
33
 
34
+ class Post < Struct.new(:category, :title)
35
+ extend ActiveModel::Naming
36
+ include ActiveModel::Conversion
37
+
38
+ extend Enumerize
39
+
40
+ enumerize :categories, :in => [:music, :games], :multiple => true
41
+
42
+ def persisted?
43
+ false
44
+ end
45
+ end
46
+
47
+ class Registration < Struct.new(:sex)
48
+ extend Enumerize
49
+
50
+ enumerize :sex, in: [:male, :female]
51
+ end
52
+
37
53
  before { $VERBOSE = nil }
38
54
  after { $VERBOSE = true }
39
55
 
40
56
  let(:user) { User.new }
57
+ let(:post) { Post.new }
41
58
 
42
59
  it 'renders select with enumerized values' do
43
60
  concat(semantic_form_for(user) do |f|
@@ -48,6 +65,49 @@ class FormtasticSpec < MiniTest::Spec
48
65
  assert_select 'select option[value=female]'
49
66
  end
50
67
 
68
+ it 'renders multiple select with enumerized values' do
69
+ concat(semantic_form_for(post) do |f|
70
+ f.input(:categories)
71
+ end)
72
+
73
+ assert_select 'select[multiple=multiple]'
74
+ assert_select 'select option[value=music]'
75
+ assert_select 'select option[value=games]'
76
+ end
77
+
78
+ it 'renders multiple select with selected enumerized value' do
79
+ post.categories << :music
80
+
81
+ concat(semantic_form_for(post) do |f|
82
+ f.input(:categories)
83
+ end)
84
+
85
+ assert_select 'select[multiple=multiple]'
86
+ assert_select 'select option[value=music][selected=selected]'
87
+ assert_select 'select option[value=games][selected=selected]', count: 0
88
+ end
89
+
90
+ it 'renders checkboxes with enumerized values' do
91
+ concat(semantic_form_for(post) do |f|
92
+ f.input(:categories, as: :check_boxes)
93
+ end)
94
+
95
+ assert_select 'select[multiple=multiple]', count: 0
96
+ assert_select 'input[type=checkbox][value=music]'
97
+ assert_select 'input[type=checkbox][value=games]'
98
+ end
99
+
100
+ it 'renders checkboxes with selected enumerized value' do
101
+ post.categories << :music
102
+
103
+ concat(semantic_form_for(post) do |f|
104
+ f.input(:categories, as: :check_boxes)
105
+ end)
106
+
107
+ assert_select 'input[type=checkbox][value=music][checked=checked]'
108
+ assert_select 'input[type=checkbox][value=games][checked=checked]', count: 0
109
+ end
110
+
51
111
  it 'renders radio buttons with enumerized values' do
52
112
  concat(semantic_form_for(user) do |f|
53
113
  f.input :sex, :as => :radio
@@ -64,4 +124,29 @@ class FormtasticSpec < MiniTest::Spec
64
124
 
65
125
  assert_select 'input[type=text]'
66
126
  end
127
+
128
+ it 'does not affect not enumerized classes' do
129
+ concat(semantic_form_for(Thing.new) do |f|
130
+ f.input(:name)
131
+ end)
132
+
133
+ assert_select 'input[type=text]'
134
+ end
135
+
136
+ it 'renders select with enumerized values for non-ActiveModel object' do
137
+ concat(semantic_form_for(Registration.new, as: 'registration', url: '/') do |f|
138
+ f.input(:sex)
139
+ end)
140
+
141
+ assert_select 'select option[value=male]'
142
+ assert_select 'select option[value=female]'
143
+ end
144
+
145
+ it 'does not affect forms without object' do
146
+ concat(semantic_form_for('') do |f|
147
+ f.input(:name)
148
+ end)
149
+
150
+ assert_select 'input[type=text]'
151
+ end
67
152
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class ModuleAttributesSpec < MiniTest::Spec
4
6
  it 'inherits attribute from the module' do
5
7
  mod = Module.new do
6
- include Enumerize
8
+ extend Enumerize
7
9
  enumerize :sex, :in => %w[male female], :default => 'male'
8
10
  end
9
11
 
@@ -16,7 +18,7 @@ class ModuleAttributesSpec < MiniTest::Spec
16
18
 
17
19
  it 'uses new attributes from the module' do
18
20
  mod = Module.new do
19
- include Enumerize
21
+ extend Enumerize
20
22
  end
21
23
 
22
24
  klass = Class.new
@@ -26,4 +28,25 @@ class ModuleAttributesSpec < MiniTest::Spec
26
28
  klass.new.sex.must_equal 'male'
27
29
  klass.sex.must_be_instance_of Enumerize::Attribute
28
30
  end
31
+
32
+ it 'validates attributes' do
33
+ mod = Module.new do
34
+ extend Enumerize
35
+ enumerize :sex, :in => %w[male female]
36
+ end
37
+
38
+ klass = Class.new do
39
+ include ActiveModel::Validations
40
+ include mod
41
+
42
+ def self.model_name
43
+ ActiveModel::Name.new(self, nil, 'name')
44
+ end
45
+ end
46
+
47
+ object = klass.new
48
+ object.sex = 'wrong'
49
+ object.wont_be :valid?
50
+ object.errors[:sex].must_include 'is not included in the list'
51
+ end
29
52
  end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ begin
6
+
7
+ silence_warnings do
8
+ require 'mongo_mapper'
9
+ end
10
+
11
+ MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
12
+ MongoMapper.database = 'enumerize-test-suite-of-mongomapper'
13
+
14
+ describe Enumerize do
15
+ class MongoMapperUser
16
+ include MongoMapper::Document
17
+ extend Enumerize
18
+
19
+ key :sex
20
+ key :role
21
+ key :foo
22
+
23
+ enumerize :sex, :in => %w[male female]
24
+ enumerize :role, :in => %w[admin user], :default => 'user'
25
+ enumerize :foo, :in => %w[bar baz], :skip_validations => true
26
+ end
27
+
28
+ before { $VERBOSE = nil }
29
+ after { $VERBOSE = true }
30
+
31
+ let(:model) { MongoMapperUser }
32
+
33
+ it 'sets nil if invalid value is passed' do
34
+ user = model.new
35
+ user.sex = :invalid
36
+ user.sex.must_be_nil
37
+ end
38
+
39
+ it 'saves value' do
40
+ model.delete_all
41
+ user = model.new
42
+ user.sex = :female
43
+ user.save!
44
+ user.sex.must_equal 'female'
45
+ end
46
+
47
+ it 'loads value' do
48
+ model.delete_all
49
+ model.create!(:sex => :male)
50
+ store_translations(:en, :enumerize => {:sex => {:male => 'Male'}}) do
51
+ user = model.first
52
+ user.sex.must_equal 'male'
53
+ user.sex_text.must_equal 'Male'
54
+ end
55
+ end
56
+
57
+ it 'has default value' do
58
+ model.new.role.must_equal 'user'
59
+ end
60
+
61
+ it 'validates inclusion' do
62
+ user = model.new
63
+ user.role = 'wrong'
64
+ user.wont_be :valid?
65
+ end
66
+
67
+ it 'does not validate inclusion when :skip_validations option passed' do
68
+ user = model.new
69
+ user.foo = 'wrong'
70
+ user.must_be :valid?
71
+ end
72
+
73
+ it 'assigns value on loaded record' do
74
+ model.delete_all
75
+ model.create!(:sex => :male)
76
+ user = model.first
77
+ user.sex = :female
78
+ user.sex.must_equal 'female'
79
+ end
80
+ end
81
+
82
+ rescue LoadError
83
+ # Skip
84
+ end