careacademy-enumerize 2.8.0
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.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +69 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +327 -0
- data/Gemfile +6 -0
- data/Gemfile.global +12 -0
- data/Gemfile.mongo_mapper +6 -0
- data/Gemfile.rails60 +6 -0
- data/Gemfile.rails61 +6 -0
- data/Gemfile.rails70 +9 -0
- data/Gemfile.railsmaster +5 -0
- data/MIT-LICENSE +22 -0
- data/README.md +641 -0
- data/Rakefile +17 -0
- data/enumerize.gemspec +22 -0
- data/lib/enumerize/activemodel.rb +47 -0
- data/lib/enumerize/activerecord.rb +142 -0
- data/lib/enumerize/attribute.rb +192 -0
- data/lib/enumerize/attribute_map.rb +40 -0
- data/lib/enumerize/base.rb +112 -0
- data/lib/enumerize/hooks/formtastic.rb +27 -0
- data/lib/enumerize/hooks/sequel_dataset.rb +17 -0
- data/lib/enumerize/hooks/simple_form.rb +37 -0
- data/lib/enumerize/hooks/uniqueness.rb +22 -0
- data/lib/enumerize/integrations/rails_admin.rb +18 -0
- data/lib/enumerize/integrations/rspec/matcher.rb +164 -0
- data/lib/enumerize/integrations/rspec.rb +19 -0
- data/lib/enumerize/module.rb +33 -0
- data/lib/enumerize/module_attributes.rb +12 -0
- data/lib/enumerize/mongoid.rb +29 -0
- data/lib/enumerize/predicatable.rb +23 -0
- data/lib/enumerize/predicates.rb +76 -0
- data/lib/enumerize/scope/activerecord.rb +53 -0
- data/lib/enumerize/scope/mongoid.rb +50 -0
- data/lib/enumerize/scope/sequel.rb +56 -0
- data/lib/enumerize/sequel.rb +62 -0
- data/lib/enumerize/set.rb +81 -0
- data/lib/enumerize/utils.rb +12 -0
- data/lib/enumerize/value.rb +47 -0
- data/lib/enumerize/version.rb +5 -0
- data/lib/enumerize.rb +90 -0
- data/lib/sequel/plugins/enumerize.rb +18 -0
- data/spec/enumerize/integrations/rspec/matcher_spec.rb +261 -0
- data/spec/spec_helper.rb +30 -0
- data/test/activemodel_test.rb +114 -0
- data/test/activerecord_test.rb +679 -0
- data/test/attribute_map_test.rb +70 -0
- data/test/attribute_test.rb +141 -0
- data/test/base_test.rb +230 -0
- data/test/formtastic_test.rb +152 -0
- data/test/module_attributes_test.rb +52 -0
- data/test/mongo_mapper_test.rb +83 -0
- data/test/mongoid_test.rb +164 -0
- data/test/multiple_test.rb +65 -0
- data/test/predicates_test.rb +65 -0
- data/test/rails_admin_test.rb +27 -0
- data/test/sequel_test.rb +344 -0
- data/test/set_test.rb +166 -0
- data/test/simple_form_test.rb +156 -0
- data/test/support/mock_controller.rb +31 -0
- data/test/support/shared_enums.rb +43 -0
- data/test/support/view_test_helper.rb +46 -0
- data/test/test_helper.rb +53 -0
- data/test/value_test.rb +158 -0
- metadata +143 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
module Enumerize
|
6
|
+
class AttributeMapTest < MiniTest::Spec
|
7
|
+
subject { AttributeMap.new }
|
8
|
+
|
9
|
+
def make_attr(name)
|
10
|
+
Attribute.new(nil, name, :in => %[a b])
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'empty when no attrs' do
|
14
|
+
expect(subject).must_be_empty
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'not empty when attr added' do
|
18
|
+
subject << make_attr(:a)
|
19
|
+
expect(subject).wont_be_empty
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'iterates over added attrs' do
|
23
|
+
attr_1 = make_attr(:a)
|
24
|
+
attr_2 = make_attr(:b)
|
25
|
+
|
26
|
+
subject << attr_1
|
27
|
+
subject << attr_2
|
28
|
+
|
29
|
+
count = 0
|
30
|
+
actual = []
|
31
|
+
|
32
|
+
subject.each do |element|
|
33
|
+
count += 1
|
34
|
+
actual << element
|
35
|
+
end
|
36
|
+
|
37
|
+
expect(count).must_equal 2
|
38
|
+
expect(actual).must_equal [attr_1, attr_2]
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'reads attribute by name' do
|
42
|
+
attr = make_attr(:a)
|
43
|
+
subject << attr
|
44
|
+
expect(subject[:a]).must_equal attr
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'reads attribute by name using string' do
|
48
|
+
attr = make_attr(:a)
|
49
|
+
subject << attr
|
50
|
+
expect(subject['a']).must_equal attr
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'updates dependants' do
|
54
|
+
attr = make_attr(:a)
|
55
|
+
dependant = MiniTest::Mock.new
|
56
|
+
dependant.expect(:<<, nil, [attr])
|
57
|
+
subject.add_dependant dependant
|
58
|
+
subject << attr
|
59
|
+
dependant.verify
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'adds attrs to dependant' do
|
63
|
+
attr = make_attr(:a)
|
64
|
+
subject << attr
|
65
|
+
dependant = AttributeMap.new
|
66
|
+
subject.add_dependant dependant
|
67
|
+
expect(dependant[:a]).must_equal attr
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
describe Enumerize::Attribute do
|
6
|
+
def attr
|
7
|
+
@attr ||= nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def build_attr(*args, &block)
|
11
|
+
@attr = Enumerize::Attribute.new(*args, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'returns values' do
|
15
|
+
build_attr nil, :foo, :in => [:a, :b]
|
16
|
+
expect(attr.values).must_equal %w[a b]
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'returns frozen values' do
|
20
|
+
build_attr nil, :foo, :in => [:a, :b]
|
21
|
+
expect(attr.values.map(&:frozen?)).must_equal [true, true]
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'converts name to symbol' do
|
25
|
+
build_attr nil, 'foo', :in => %w[a b]
|
26
|
+
expect(attr.name).must_equal :foo
|
27
|
+
end
|
28
|
+
|
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
|
+
expect(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
|
+
expect(attr.i18n_scopes).must_equal %w[bar buzz]
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'accepts only string scopes' do
|
42
|
+
expect(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
|
+
expect(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
|
+
expect(attr.options(:only => :a)).must_equal [['a text', 'a']]
|
58
|
+
expect(attr.options(:only => [:b])).must_equal [['b text', 'b']]
|
59
|
+
expect(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
|
+
expect(attr.options(:except => :a)).must_equal [['b text', 'b']]
|
67
|
+
expect(attr.options(:except => :b)).must_equal [['a text', 'a']]
|
68
|
+
expect(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
|
+
expect(proc { attr.options(:except => [], :only => []) }).must_raise ArgumentError
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'values hash' do
|
81
|
+
before do
|
82
|
+
build_attr nil, :foo, :in => {:a => 1, :b => 2}
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'returns hash keys as values' do
|
86
|
+
expect(attr.values).must_equal %w[a b]
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'finds values by hash values' do
|
90
|
+
expect(attr.find_value(1)).must_equal 'a'
|
91
|
+
expect(attr.find_value(2)).must_equal 'b'
|
92
|
+
end
|
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
|
+
expect(attr).must_respond_to :a
|
99
|
+
expect(attr).must_respond_to :b
|
100
|
+
|
101
|
+
expect(attr.a.value).must_equal 1
|
102
|
+
expect(attr.b.value).must_equal 2
|
103
|
+
expect(attr.a.text).must_equal 'A'
|
104
|
+
expect(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
|
+
expect(attr.values).must_equal %w[a b c]
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'finds values by hash values' do
|
117
|
+
expect(attr.find_value(1)).must_equal 'a'
|
118
|
+
expect(attr.find_value(2)).must_equal 'b'
|
119
|
+
expect(attr.find_value(0)).must_equal 'c'
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'finds all values by hash values' do
|
123
|
+
expect(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
|
+
expect(attr.values).must_equal %w[a b]
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'finds values by hash values' do
|
137
|
+
expect(attr.find_value(true)).must_equal 'a'
|
138
|
+
expect(attr.find_value(false)).must_equal 'b'
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
data/test/base_test.rb
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
describe Enumerize::Base do
|
6
|
+
let(:kklass) do
|
7
|
+
Class.new do
|
8
|
+
extend Enumerize
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:subklass) do
|
13
|
+
Class.new(kklass)
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:object) { kklass.new }
|
17
|
+
|
18
|
+
it 'returns nil when not set' do
|
19
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
20
|
+
expect(object.foo).must_be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns value that was set' do
|
24
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
25
|
+
object.foo = :a
|
26
|
+
expect(object.foo).must_equal 'a'
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns translation' do
|
30
|
+
store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
|
31
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
32
|
+
object.foo = :a
|
33
|
+
expect(object.foo.text).must_equal 'a text'
|
34
|
+
expect(object.foo_text).must_equal 'a text'
|
35
|
+
expect(object.foo_text).must_equal 'a text'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns nil as translation when value is nil' do
|
40
|
+
store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
|
41
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
42
|
+
expect(object.foo_text).must_be_nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'scopes translation by i18n key' do
|
47
|
+
def kklass.model_name
|
48
|
+
name = String.new("ExampleClass")
|
49
|
+
def name.i18n_key
|
50
|
+
'example_class'
|
51
|
+
end
|
52
|
+
|
53
|
+
name
|
54
|
+
end
|
55
|
+
|
56
|
+
store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}}) do
|
57
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
58
|
+
object.foo = :a
|
59
|
+
expect(object.foo.text).must_equal 'a text scoped'
|
60
|
+
expect(object.foo_text).must_equal 'a text scoped'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'returns humanized value if there are no translations' do
|
65
|
+
store_translations(:en, :enumerize => {}) do
|
66
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
67
|
+
object.foo = :a
|
68
|
+
expect(object.foo_text).must_equal 'A'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'stores value as string' do
|
73
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
74
|
+
object.foo = :a
|
75
|
+
expect(object.instance_variable_get(:@foo)).must_be_instance_of String
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'handles default value' do
|
79
|
+
kklass.enumerize(:foo, :in => [:a, :b], :default => :b)
|
80
|
+
expect(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
|
+
expect(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 })
|
90
|
+
expect(object.foo).must_equal 'b'
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'raises exception on invalid default value' do
|
94
|
+
expect(proc {
|
95
|
+
kklass.enumerize(:foo, :in => [:a, :b], :default => :c)
|
96
|
+
}).must_raise ArgumentError
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'has enumerized attributes' do
|
100
|
+
expect(kklass.enumerized_attributes).must_be_empty
|
101
|
+
kklass.enumerize(:foo, :in => %w[a b])
|
102
|
+
expect(kklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
|
103
|
+
end
|
104
|
+
|
105
|
+
it "doesn't override existing method" do
|
106
|
+
method = kklass.method(:name)
|
107
|
+
kklass.enumerize(:name, :in => %w[a b], :default => 'a')
|
108
|
+
expect(kklass.method(:name)).must_equal method
|
109
|
+
end
|
110
|
+
|
111
|
+
it "inherits enumerized attributes from a parent class" do
|
112
|
+
kklass.enumerize(:foo, :in => %w[a b])
|
113
|
+
expect(subklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
|
114
|
+
end
|
115
|
+
|
116
|
+
it "inherits enumerized attributes from a grandparent class" do
|
117
|
+
kklass.enumerize(:foo, :in => %w[a b])
|
118
|
+
expect(Class.new(subklass).enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
|
119
|
+
end
|
120
|
+
|
121
|
+
it "doesn't add enumerized attributes to parent class" do
|
122
|
+
kklass.enumerize(:foo, :in => %w[a b])
|
123
|
+
subklass.enumerize(:bar, :in => %w[c d])
|
124
|
+
|
125
|
+
expect(kklass.enumerized_attributes[:bar]).must_be_nil
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'adds new parent class attributes to subclass' do
|
129
|
+
subklass = Class.new(kklass)
|
130
|
+
kklass.enumerize :foo, :in => %w[a b]
|
131
|
+
expect(subklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'stores nil value' do
|
135
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
136
|
+
object.foo = nil
|
137
|
+
expect(object.instance_variable_get(:@foo)).must_be_nil
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'casts value to string for validation' do
|
141
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
142
|
+
object.foo = :c
|
143
|
+
expect(object.read_attribute_for_validation(:foo)).must_equal 'c'
|
144
|
+
end
|
145
|
+
|
146
|
+
it "doesn't cast nil to string for validation" do
|
147
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
148
|
+
object.foo = nil
|
149
|
+
expect(object.read_attribute_for_validation(:foo)).must_be_nil
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'calls super in the accessor method' do
|
153
|
+
accessors = Module.new do
|
154
|
+
def attributes
|
155
|
+
@attributes ||= {}
|
156
|
+
end
|
157
|
+
|
158
|
+
def foo
|
159
|
+
attributes[:foo]
|
160
|
+
end
|
161
|
+
|
162
|
+
def foo=(v)
|
163
|
+
attributes[:foo] = v
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
klass = Class.new do
|
168
|
+
include accessors
|
169
|
+
extend Enumerize
|
170
|
+
|
171
|
+
enumerize :foo, :in => %w[test]
|
172
|
+
end
|
173
|
+
|
174
|
+
object = klass.new
|
175
|
+
expect(object.foo).must_be_nil
|
176
|
+
expect(object.attributes).must_equal({})
|
177
|
+
|
178
|
+
object.foo = 'test'
|
179
|
+
expect(object.foo).must_equal 'test'
|
180
|
+
expect(object.attributes).must_equal(:foo => 'test')
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'stores hash values' do
|
184
|
+
kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
|
185
|
+
|
186
|
+
object.foo = :a
|
187
|
+
expect(object.instance_variable_get(:@foo)).must_equal 1
|
188
|
+
expect(object.foo).must_equal 'a'
|
189
|
+
|
190
|
+
object.foo = :b
|
191
|
+
expect(object.instance_variable_get(:@foo)).must_equal 2
|
192
|
+
expect(object.foo).must_equal 'b'
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'returns custom value' do
|
196
|
+
kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
|
197
|
+
|
198
|
+
object.foo = :a
|
199
|
+
expect(object.foo_value).must_equal 1
|
200
|
+
|
201
|
+
object.foo = :b
|
202
|
+
expect(object.foo_value).must_equal 2
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'allows initialize method with arguments' do
|
206
|
+
klass = Class.new do
|
207
|
+
extend Enumerize
|
208
|
+
|
209
|
+
def initialize(argument, key_word_argument: nil); end
|
210
|
+
end
|
211
|
+
|
212
|
+
klass.new('arg1', key_word_argument: 'kwargs1')
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'allows initialize method with arguments for inherited classes' do
|
216
|
+
parent_klass = Class.new do
|
217
|
+
def initialize(argument, key_word_argument: nil); end
|
218
|
+
end
|
219
|
+
|
220
|
+
klass = Class.new(parent_klass) do
|
221
|
+
extend Enumerize
|
222
|
+
|
223
|
+
def initialize(argument, key_word_argument: nil)
|
224
|
+
super
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
klass.new('arg1', key_word_argument: 'kwargs1')
|
229
|
+
end
|
230
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
Formtastic::FormBuilder.action_class_finder = Formtastic::ActionClassFinder
|
6
|
+
Formtastic::FormBuilder.input_class_finder = Formtastic::InputClassFinder
|
7
|
+
|
8
|
+
class FormtasticSpec < MiniTest::Spec
|
9
|
+
include ViewTestHelper
|
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
|
19
|
+
end
|
20
|
+
|
21
|
+
class User < Struct.new(:sex, :age)
|
22
|
+
extend ActiveModel::Naming
|
23
|
+
include ActiveModel::Conversion
|
24
|
+
|
25
|
+
extend Enumerize
|
26
|
+
|
27
|
+
enumerize :sex, :in => [:male, :female]
|
28
|
+
|
29
|
+
def persisted?
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
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
|
+
|
53
|
+
before { $VERBOSE = nil }
|
54
|
+
after { $VERBOSE = true }
|
55
|
+
|
56
|
+
let(:user) { User.new }
|
57
|
+
let(:post) { Post.new }
|
58
|
+
|
59
|
+
it 'renders select with enumerized values' do
|
60
|
+
concat(semantic_form_for(user) do |f|
|
61
|
+
f.input :sex
|
62
|
+
end)
|
63
|
+
|
64
|
+
assert_select 'select option[value=male]'
|
65
|
+
assert_select 'select option[value=female]'
|
66
|
+
end
|
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
|
+
|
111
|
+
it 'renders radio buttons with enumerized values' do
|
112
|
+
concat(semantic_form_for(user) do |f|
|
113
|
+
f.input :sex, :as => :radio
|
114
|
+
end)
|
115
|
+
|
116
|
+
assert_select 'input[type=radio][value=male]'
|
117
|
+
assert_select 'input[type=radio][value=female]'
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'does not affect not enumerized attributes' do
|
121
|
+
concat(semantic_form_for(user) do |f|
|
122
|
+
f.input(:age)
|
123
|
+
end)
|
124
|
+
|
125
|
+
assert_select 'input[type=text]'
|
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
|
152
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ModuleAttributesSpec < MiniTest::Spec
|
6
|
+
it 'inherits attribute from the module' do
|
7
|
+
mod = Module.new do
|
8
|
+
extend Enumerize
|
9
|
+
enumerize :sex, :in => %w[male female], :default => 'male'
|
10
|
+
end
|
11
|
+
|
12
|
+
klass = Class.new
|
13
|
+
klass.send :include, mod
|
14
|
+
expect(klass.enumerized_attributes[:sex]).must_be_instance_of Enumerize::Attribute
|
15
|
+
expect(klass.new.sex).must_equal 'male'
|
16
|
+
expect(klass.sex).must_be_instance_of Enumerize::Attribute
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'uses new attributes from the module' do
|
20
|
+
mod = Module.new do
|
21
|
+
extend Enumerize
|
22
|
+
end
|
23
|
+
|
24
|
+
klass = Class.new
|
25
|
+
klass.send :include, mod
|
26
|
+
mod.enumerize :sex, :in => %w[male female], :default => 'male'
|
27
|
+
expect(klass.enumerized_attributes[:sex]).must_be_instance_of Enumerize::Attribute
|
28
|
+
expect(klass.new.sex).must_equal 'male'
|
29
|
+
expect(klass.sex).must_be_instance_of Enumerize::Attribute
|
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
|
+
expect(object).wont_be :valid?
|
50
|
+
expect(object.errors[:sex]).must_include 'is not included in the list'
|
51
|
+
end
|
52
|
+
end
|