enumerize 0.8.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -2
- data/CHANGELOG.md +91 -0
- data/Gemfile +4 -22
- data/{Gemfile.rails4 → Gemfile.global} +2 -5
- data/Gemfile.mongo_mapper +5 -0
- data/Gemfile.rails40 +5 -0
- data/README.md +171 -8
- data/Rakefile +2 -4
- data/lib/enumerize/activerecord.rb +3 -29
- data/lib/enumerize/attribute.rb +41 -9
- data/lib/enumerize/base.rb +25 -10
- data/lib/enumerize/hooks/sequel_dataset.rb +19 -0
- data/lib/enumerize/integrations/rspec/matcher.rb +107 -26
- data/lib/enumerize/mongoid.rb +13 -0
- data/lib/enumerize/predicatable.rb +7 -16
- data/lib/enumerize/predicates.rb +3 -1
- data/lib/enumerize/scope/activerecord.rb +37 -0
- data/lib/enumerize/scope/mongoid.rb +35 -0
- data/lib/enumerize/scope/sequel.rb +40 -0
- data/lib/enumerize/sequel.rb +57 -0
- data/lib/enumerize/set.rb +18 -8
- data/lib/enumerize/value.rb +8 -7
- data/lib/enumerize/version.rb +1 -1
- data/lib/enumerize.rb +23 -1
- data/spec/enumerize/integrations/rspec/matcher_spec.rb +258 -0
- data/spec/spec_helper.rb +28 -0
- data/test/activerecord_test.rb +58 -7
- data/test/attribute_test.rb +22 -0
- data/test/formtastic_test.rb +3 -12
- data/test/mongo_mapper_test.rb +6 -0
- data/test/mongoid_test.rb +55 -6
- data/test/multiple_test.rb +21 -0
- data/test/predicates_test.rb +6 -0
- data/test/sequel_test.rb +291 -0
- data/test/set_test.rb +14 -0
- data/test/simple_form_test.rb +0 -1
- data/test/support/view_test_helper.rb +4 -0
- data/test/test_helper.rb +23 -2
- data/test/value_test.rb +51 -21
- metadata +37 -8
- data/lib/enumerize/form_helper.rb +0 -23
- data/test/rspec_matcher_test.rb +0 -76
- data/test/rspec_spec.rb +0 -13
@@ -0,0 +1,258 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
silence_warnings do
|
4
|
+
ActiveRecord::Migration.verbose = false
|
5
|
+
ActiveRecord::Base.logger = Logger.new(nil)
|
6
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
7
|
+
end
|
8
|
+
|
9
|
+
ActiveRecord::Base.connection.instance_eval do
|
10
|
+
create_table :users do |t|
|
11
|
+
t.string :sex
|
12
|
+
t.string :role
|
13
|
+
t.string :account_type
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class User < ActiveRecord::Base
|
18
|
+
extend Enumerize
|
19
|
+
|
20
|
+
enumerize :sex, :in => [:male, :female], scope: true
|
21
|
+
enumerize :role, :in => [:user, :admin], scope: :having_role
|
22
|
+
enumerize :account_type, :in => [:basic, :premium]
|
23
|
+
end
|
24
|
+
|
25
|
+
RSpec.describe Enumerize::Integrations::RSpec::Matcher do
|
26
|
+
|
27
|
+
let(:model) do
|
28
|
+
Class.new do
|
29
|
+
extend Enumerize
|
30
|
+
|
31
|
+
def self.name
|
32
|
+
'Model'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
subject do
|
38
|
+
model.new
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'without qualifier' do
|
42
|
+
|
43
|
+
it 'accepts when has defined a enumerize' do
|
44
|
+
model.enumerize(:sex, :in => [:male, :female])
|
45
|
+
expect(subject).to enumerize(:sex)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'rejects when has not defined a enumerize' do
|
49
|
+
message = 'Expected Model to define enumerize :sex'
|
50
|
+
expect do
|
51
|
+
expect(subject).to enumerize(:sex)
|
52
|
+
end.to fail_with(message)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#in' do
|
57
|
+
|
58
|
+
context 'defined as array' do
|
59
|
+
|
60
|
+
before do
|
61
|
+
model.enumerize(:sex, :in => [:male, :female])
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'accepts the right params as an array' do
|
65
|
+
expect(subject).to enumerize(:sex).in([:male, :female])
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'accepts the right params as regular params' do
|
69
|
+
expect(subject).to enumerize(:sex).in(:male, :female)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'accepts the the right params in a different order' do
|
73
|
+
expect(subject).to enumerize(:sex).in(:female, :male)
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'rejects wrong params' do
|
77
|
+
message = 'Expected Model to define enumerize :sex in: "boy", "girl"'
|
78
|
+
expect do
|
79
|
+
expect(subject).to enumerize(:sex).in(:boy, :girl)
|
80
|
+
end.to fail_with(message)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'has the right message when negated' do
|
84
|
+
message = 'Did not expect Model to define enumerize :sex in: "female", "male"'
|
85
|
+
expect do
|
86
|
+
expect(subject).to_not enumerize(:sex).in(:male, :female)
|
87
|
+
end.to fail_with(message)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'defined as hash' do
|
92
|
+
|
93
|
+
before do
|
94
|
+
model.enumerize(:sex, :in => { male: 0, female: 1 })
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'accepts the right params as an array' do
|
98
|
+
expect(subject).to enumerize(:sex).in(:male, :female)
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'accepts the right params as a hash' do
|
102
|
+
expect(subject).to enumerize(:sex).in(male: 0, female: 1)
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'accepts the right params as a hash in a different order' do
|
106
|
+
expect(subject).to enumerize(:sex).in(female: 1, male: 0)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'rejects wrong keys' do
|
110
|
+
message = 'Expected Model to define enumerize :sex in: "{:boy=>0, :girl=>1}"'
|
111
|
+
expect do
|
112
|
+
expect(subject).to enumerize(:sex).in(boy: 0, girl: 1)
|
113
|
+
end.to fail_with(message)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'rejects wrong values' do
|
117
|
+
message = 'Expected Model to define enumerize :sex in: "{:male=>2, :female=>3}"'
|
118
|
+
expect do
|
119
|
+
expect(subject).to enumerize(:sex).in(male: 2, female: 3)
|
120
|
+
end.to fail_with(message)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'has the right description' do
|
125
|
+
matcher = enumerize(:sex).in(:male, :female)
|
126
|
+
expect(matcher.description).to eq('define enumerize :sex in: "female", "male"')
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe '#with_default' do
|
131
|
+
|
132
|
+
before do
|
133
|
+
model.enumerize(:sex, :in => [:male, :female], default: :female)
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'accepts the right default value' do
|
137
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_default(:female)
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'rejects the wrong default value' do
|
141
|
+
message = 'Expected Model to define enumerize :sex in: "female", "male" with "male" as default value'
|
142
|
+
expect do
|
143
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_default(:male)
|
144
|
+
end.to fail_with(message)
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'rejects if the `in` is wrong with a correct default value' do
|
148
|
+
message = 'Expected Model to define enumerize :sex in: "boy", "girl" with "female" as default value'
|
149
|
+
expect do
|
150
|
+
expect(subject).to enumerize(:sex).in(:boy, :girl).with_default(:female)
|
151
|
+
end.to fail_with(message)
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'has the right description' do
|
155
|
+
matcher = enumerize(:sex).in(:male, :female).with_default(:female)
|
156
|
+
message = 'define enumerize :sex in: "female", "male" with "female" as default value'
|
157
|
+
expect(matcher.description).to eq(message)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe '#with_i18n_scope' do
|
162
|
+
|
163
|
+
context 'defined as string' do
|
164
|
+
|
165
|
+
before do
|
166
|
+
model.enumerize(:sex, :in => [:male, :female], i18n_scope: 'sex')
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'accepts the right i18n_scope' do
|
170
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_i18n_scope('sex')
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'rejects the wrong i18n_scope' do
|
174
|
+
message = 'Expected Model to define enumerize :sex in: "female", "male" i18n_scope: "gender"'
|
175
|
+
expect do
|
176
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_i18n_scope('gender')
|
177
|
+
end.to fail_with(message)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context 'defined as array' do
|
182
|
+
|
183
|
+
before do
|
184
|
+
model.enumerize(:sex, :in => [:male, :female], i18n_scope: ['sex', 'more.sex'])
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'accepts the wrong i18n_scope' do
|
188
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_i18n_scope(['sex', 'more.sex'])
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'rejects the wrong i18n_scope' do
|
192
|
+
message = 'Expected Model to define enumerize :sex in: "female", "male" i18n_scope: ["sex"]'
|
193
|
+
expect do
|
194
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_i18n_scope(['sex'])
|
195
|
+
end.to fail_with(message)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe '#with_predicates' do
|
201
|
+
|
202
|
+
it 'accepts when predicates is defined as a boolean' do
|
203
|
+
model.enumerize(:sex, :in => [:male, :female], predicates: true)
|
204
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_predicates(true)
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'accepts when predicates is defined as a hash' do
|
208
|
+
model.enumerize(:sex, :in => [:male, :female], predicates: { prefix: true })
|
209
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_predicates(prefix: true)
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'rejects when predicates is not defined' do
|
213
|
+
model.enumerize(:sex, :in => [:male, :female])
|
214
|
+
message = 'Expected Model to define enumerize :sex in: "female", "male" predicates: true'
|
215
|
+
expect do
|
216
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_predicates(true)
|
217
|
+
end.to fail_with(message)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
describe '#with_multiple' do
|
222
|
+
|
223
|
+
it 'accepts when has defined the multiple' do
|
224
|
+
model.enumerize(:sex, :in => [:male, :female], multiple: true)
|
225
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_multiple(true)
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'rejects when has not defined the multiple' do
|
229
|
+
model.enumerize(:sex, :in => [:male, :female])
|
230
|
+
message = 'Expected Model to define enumerize :sex in: "female", "male" multiple: true'
|
231
|
+
expect do
|
232
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_multiple(true)
|
233
|
+
end.to fail_with(message)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
describe '#with_scope' do
|
238
|
+
|
239
|
+
subject do
|
240
|
+
User.new
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'accepts when scope is defined as a boolean' do
|
244
|
+
expect(subject).to enumerize(:sex).in(:male, :female).with_scope(true)
|
245
|
+
end
|
246
|
+
|
247
|
+
it 'accepts when scope is defined as a hash' do
|
248
|
+
expect(subject).to enumerize(:role).in(:user, :admin).with_scope(scope: :having_role)
|
249
|
+
end
|
250
|
+
|
251
|
+
it 'rejects when scope is not defined' do
|
252
|
+
message = 'Expected User to define enumerize :account_type in: "basic", "premium" scope: true'
|
253
|
+
expect do
|
254
|
+
expect(subject).to enumerize(:account_type).in(:basic, :premium).with_scope(true)
|
255
|
+
end.to fail_with(message)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'enumerize'
|
3
|
+
require 'rspec/matchers/fail_matchers'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.disable_monkey_patching!
|
7
|
+
config.order = :random
|
8
|
+
Kernel.srand config.seed
|
9
|
+
config.filter_run focus: true
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
|
12
|
+
config.expect_with :rspec do |expectations|
|
13
|
+
expectations.syntax = :expect
|
14
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
15
|
+
end
|
16
|
+
|
17
|
+
config.mock_with :rspec do |mocks|
|
18
|
+
mocks.syntax = :expect
|
19
|
+
mocks.verify_partial_doubles = true
|
20
|
+
end
|
21
|
+
|
22
|
+
if config.files_to_run.one?
|
23
|
+
config.default_formatter = 'doc'
|
24
|
+
end
|
25
|
+
|
26
|
+
config.warnings = true
|
27
|
+
config.include RSpec::Matchers::FailMatchers
|
28
|
+
end
|
data/test/activerecord_test.rb
CHANGED
@@ -15,13 +15,13 @@ ActiveRecord::Base.connection.instance_eval do
|
|
15
15
|
t.string :lambda_role
|
16
16
|
t.string :name
|
17
17
|
t.string :interests
|
18
|
-
t.
|
18
|
+
t.integer :status
|
19
19
|
t.string :account_type, :default => :basic
|
20
20
|
end
|
21
21
|
|
22
22
|
create_table :documents do |t|
|
23
23
|
t.string :visibility
|
24
|
-
t.timestamps
|
24
|
+
t.timestamps null: true
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -57,6 +57,7 @@ end
|
|
57
57
|
|
58
58
|
class UniqStatusUser < User
|
59
59
|
validates :status, uniqueness: true
|
60
|
+
validates :sex, presence: true
|
60
61
|
end
|
61
62
|
|
62
63
|
describe Enumerize::ActiveRecordSupport do
|
@@ -92,7 +93,8 @@ describe Enumerize::ActiveRecordSupport do
|
|
92
93
|
it 'does not set default value for not selected attributes' do
|
93
94
|
User.delete_all
|
94
95
|
User.create!(:sex => :male)
|
95
|
-
|
96
|
+
|
97
|
+
assert_equal ['id'], User.select(:id).first.attributes.keys
|
96
98
|
end
|
97
99
|
|
98
100
|
it 'has default value with lambda' do
|
@@ -128,9 +130,18 @@ describe Enumerize::ActiveRecordSupport do
|
|
128
130
|
user.errors[:role].must_include 'is not included in the list'
|
129
131
|
end
|
130
132
|
|
131
|
-
it 'validates inclusion when using write_attribute' do
|
133
|
+
it 'validates inclusion when using write_attribute with string attribute' do
|
134
|
+
user = User.new
|
135
|
+
user.send(:write_attribute, 'role', 'wrong')
|
136
|
+
user.read_attribute_for_validation(:role).must_equal 'wrong'
|
137
|
+
user.wont_be :valid?
|
138
|
+
user.errors[:role].must_include 'is not included in the list'
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'validates inclusion when using write_attribute with symbol attribute' do
|
132
142
|
user = User.new
|
133
143
|
user.send(:write_attribute, :role, 'wrong')
|
144
|
+
user.read_attribute_for_validation(:role).must_equal 'wrong'
|
134
145
|
user.wont_be :valid?
|
135
146
|
user.errors[:role].must_include 'is not included in the list'
|
136
147
|
end
|
@@ -143,7 +154,7 @@ describe Enumerize::ActiveRecordSupport do
|
|
143
154
|
|
144
155
|
it "uses persisted value for validation if it hasn't been set" do
|
145
156
|
user = User.create! :sex => :male
|
146
|
-
User.find(user).read_attribute_for_validation(:sex).must_equal 'male'
|
157
|
+
User.find(user.id).read_attribute_for_validation(:sex).must_equal 'male'
|
147
158
|
end
|
148
159
|
|
149
160
|
it 'is valid with empty string assigned' do
|
@@ -179,6 +190,13 @@ describe Enumerize::ActiveRecordSupport do
|
|
179
190
|
interests.must_equal %w(music dancing)
|
180
191
|
end
|
181
192
|
|
193
|
+
it 'stores multiple value passed passed to new' do
|
194
|
+
user = User.new(interests: [:music, :dancing])
|
195
|
+
user.save!
|
196
|
+
user.interests.must_equal %w(music dancing)
|
197
|
+
User.find(user.id).interests.must_equal %w(music dancing)
|
198
|
+
end
|
199
|
+
|
182
200
|
it 'returns invalid multiple value for validation' do
|
183
201
|
user = User.new
|
184
202
|
user.interests << :music
|
@@ -199,6 +217,22 @@ describe Enumerize::ActiveRecordSupport do
|
|
199
217
|
user.must_be :valid?
|
200
218
|
end
|
201
219
|
|
220
|
+
it 'stores custom values for multiple attributes' do
|
221
|
+
User.delete_all
|
222
|
+
|
223
|
+
klass = Class.new(User)
|
224
|
+
klass.enumerize :interests, in: { music: 0, sports: 1, dancing: 2, programming: 3}, multiple: true
|
225
|
+
|
226
|
+
user = klass.new
|
227
|
+
user.interests << :music
|
228
|
+
user.read_attribute(:interests).must_equal [0]
|
229
|
+
user.interests.must_equal %w(music)
|
230
|
+
user.save
|
231
|
+
|
232
|
+
user = klass.find(user.id)
|
233
|
+
user.interests.must_equal %w(music)
|
234
|
+
end
|
235
|
+
|
202
236
|
it 'adds scope' do
|
203
237
|
User.delete_all
|
204
238
|
|
@@ -215,6 +249,12 @@ describe Enumerize::ActiveRecordSupport do
|
|
215
249
|
User.having_role(:admin).must_equal [user_1]
|
216
250
|
end
|
217
251
|
|
252
|
+
it 'ignores not enumerized values that passed to the scope method' do
|
253
|
+
User.delete_all
|
254
|
+
|
255
|
+
User.with_status(:foo).must_equal []
|
256
|
+
end
|
257
|
+
|
218
258
|
it 'allows either key or value as valid' do
|
219
259
|
user_1 = User.new(status: :active)
|
220
260
|
user_2 = User.new(status: 1)
|
@@ -258,6 +298,18 @@ describe Enumerize::ActiveRecordSupport do
|
|
258
298
|
user.errors[:status].wont_be :empty?
|
259
299
|
end
|
260
300
|
|
301
|
+
it 'is valid after #becomes' do
|
302
|
+
User.delete_all
|
303
|
+
user = User.new
|
304
|
+
user.sex = :male
|
305
|
+
user.save!
|
306
|
+
|
307
|
+
uniq_user = User.find(user.id).becomes(UniqStatusUser)
|
308
|
+
uniq_user.valid?
|
309
|
+
|
310
|
+
uniq_user.errors.must_be_empty
|
311
|
+
end
|
312
|
+
|
261
313
|
it 'supports multiple attributes in #becomes' do
|
262
314
|
User.delete_all
|
263
315
|
|
@@ -291,7 +343,6 @@ describe Enumerize::ActiveRecordSupport do
|
|
291
343
|
user = User.create(:status => :active)
|
292
344
|
user.status = :blocked
|
293
345
|
|
294
|
-
|
295
|
-
assert_equal expected, user.changes.to_yaml
|
346
|
+
assert_equal [1, 2], YAML.load(user.changes.to_yaml)[:status]
|
296
347
|
end
|
297
348
|
end
|
data/test/attribute_test.rb
CHANGED
@@ -19,6 +19,12 @@ describe Enumerize::Attribute do
|
|
19
19
|
attr.name.must_equal :foo
|
20
20
|
end
|
21
21
|
|
22
|
+
it 'uses custom value class' do
|
23
|
+
value_class = Class.new(Enumerize::Value)
|
24
|
+
build_attr nil, 'foo', :in => %w[a b], :value_class => value_class
|
25
|
+
attr.values.first.must_be_instance_of value_class
|
26
|
+
end
|
27
|
+
|
22
28
|
describe 'i18n scopes' do
|
23
29
|
it 'returns scopes from options' do
|
24
30
|
build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
|
@@ -79,6 +85,18 @@ describe Enumerize::Attribute do
|
|
79
85
|
end
|
80
86
|
end
|
81
87
|
|
88
|
+
it 'sets up shortcut methods for each value' do
|
89
|
+
build_attr nil, :foo, :in => {:a => 1, :b => 2}
|
90
|
+
|
91
|
+
attr.must_respond_to :a
|
92
|
+
attr.must_respond_to :b
|
93
|
+
|
94
|
+
attr.a.value.must_equal 1
|
95
|
+
attr.b.value.must_equal 2
|
96
|
+
attr.a.text.must_equal 'A'
|
97
|
+
attr.b.text.must_equal 'B'
|
98
|
+
end
|
99
|
+
|
82
100
|
describe 'values hash with zero' do
|
83
101
|
before do
|
84
102
|
build_attr nil, :foo, :in => {:a => 1, :b => 2, :c => 0}
|
@@ -93,6 +111,10 @@ describe Enumerize::Attribute do
|
|
93
111
|
attr.find_value(2).must_equal 'b'
|
94
112
|
attr.find_value(0).must_equal 'c'
|
95
113
|
end
|
114
|
+
|
115
|
+
it 'finds all values by hash values' do
|
116
|
+
attr.find_values(1, 2, 0).must_equal ['a', 'b', 'c']
|
117
|
+
end
|
96
118
|
end
|
97
119
|
|
98
120
|
describe 'boolean values hash' do
|
data/test/formtastic_test.rb
CHANGED
@@ -1,16 +1,7 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
|
3
|
-
|
4
|
-
|
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
|
2
|
+
|
3
|
+
Formtastic::FormBuilder.action_class_finder = Formtastic::ActionClassFinder
|
4
|
+
Formtastic::FormBuilder.input_class_finder = Formtastic::InputClassFinder
|
14
5
|
|
15
6
|
class FormtasticSpec < MiniTest::Spec
|
16
7
|
include ViewTestHelper
|
data/test/mongo_mapper_test.rb
CHANGED
data/test/mongoid_test.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
begin
|
4
|
+
|
3
5
|
silence_warnings do
|
4
6
|
require 'mongoid'
|
5
7
|
end
|
6
8
|
|
7
9
|
Mongoid.configure do |config|
|
8
|
-
config.
|
9
|
-
config.
|
10
|
-
config.include_root_in_json = true
|
10
|
+
config.connect_to('enumerize-test-suite')
|
11
|
+
config.options = { use_utc: true, include_root_in_json: true }
|
11
12
|
end
|
12
13
|
|
13
14
|
describe Enumerize do
|
@@ -17,9 +18,10 @@ describe Enumerize do
|
|
17
18
|
|
18
19
|
field :sex
|
19
20
|
field :role
|
20
|
-
enumerize :sex,
|
21
|
-
enumerize :
|
22
|
-
enumerize :
|
21
|
+
enumerize :sex, :in => %w[male female], scope: true
|
22
|
+
enumerize :status, :in => %w[notice warning error], scope: true
|
23
|
+
enumerize :role, :in => %w[admin user], :default => 'user', scope: :having_role
|
24
|
+
enumerize :mult, :in => %w[one two three four], :multiple => true
|
23
25
|
end
|
24
26
|
|
25
27
|
before { $VERBOSE = nil }
|
@@ -55,6 +57,14 @@ describe Enumerize do
|
|
55
57
|
model.new.role.must_equal 'user'
|
56
58
|
end
|
57
59
|
|
60
|
+
it 'uses after_initialize callback to set default value' do
|
61
|
+
model.delete_all
|
62
|
+
model.create!(sex: 'male', role: nil)
|
63
|
+
|
64
|
+
user = model.where(sex: 'male').first
|
65
|
+
user.role.must_equal 'user'
|
66
|
+
end
|
67
|
+
|
58
68
|
it 'validates inclusion' do
|
59
69
|
user = model.new
|
60
70
|
user.role = 'wrong'
|
@@ -76,4 +86,43 @@ describe Enumerize do
|
|
76
86
|
user = model.first
|
77
87
|
user.mult.to_a.must_equal ['one', 'two']
|
78
88
|
end
|
89
|
+
|
90
|
+
it 'adds scope' do
|
91
|
+
model.delete_all
|
92
|
+
|
93
|
+
user_1 = model.create!(sex: :male, role: :admin)
|
94
|
+
user_2 = model.create!(sex: :female, role: :user)
|
95
|
+
|
96
|
+
model.with_sex(:male).to_a.must_equal [user_1]
|
97
|
+
model.with_sex(:female).to_a.must_equal [user_2]
|
98
|
+
model.with_sex(:male, :female).to_set.must_equal [user_1, user_2].to_set
|
99
|
+
|
100
|
+
model.without_sex(:male).to_a.must_equal [user_2]
|
101
|
+
model.without_sex(:female).to_a.must_equal [user_1]
|
102
|
+
model.without_sex(:male, :female).to_a.must_equal []
|
103
|
+
|
104
|
+
model.having_role(:admin).to_a.must_equal [user_1]
|
105
|
+
model.having_role(:user).to_a.must_equal [user_2]
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'chains scopes' do
|
109
|
+
model.delete_all
|
110
|
+
|
111
|
+
user_1 = model.create!(status: :notice)
|
112
|
+
user_2 = model.create!(status: :warning)
|
113
|
+
user_3 = model.create!(status: :error)
|
114
|
+
|
115
|
+
model.with_status(:notice, :warning).with_status(:notice, :error).to_a.must_equal [user_1]
|
116
|
+
model.with_status(:notice, :warning).union.with_status(:notice, :error).to_a.must_equal [user_1, user_2, user_3]
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'ignores not enumerized values that passed to the scope method' do
|
120
|
+
model.delete_all
|
121
|
+
|
122
|
+
model.with_sex(:foo).must_equal []
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
rescue LoadError
|
127
|
+
# Skip
|
79
128
|
end
|
data/test/multiple_test.rb
CHANGED
@@ -24,6 +24,21 @@ describe Enumerize::Base do
|
|
24
24
|
object.foos.must_equal %w(a c)
|
25
25
|
end
|
26
26
|
|
27
|
+
it 'sets default value as single value' do
|
28
|
+
klass.enumerize :foos, in: %w(a b c), default: 'b', multiple: true
|
29
|
+
object.foos.must_equal %w(b)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'sets default value as array of one element' do
|
33
|
+
klass.enumerize :foos, in: %w(a b c), default: %w(b), multiple: true
|
34
|
+
object.foos.must_equal %w(b)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'sets default value as array of several elements' do
|
38
|
+
klass.enumerize :foos, in: %w(a b c), default: %w(b c), multiple: true
|
39
|
+
object.foos.must_equal %w(b c)
|
40
|
+
end
|
41
|
+
|
27
42
|
it "doesn't define _text method" do
|
28
43
|
klass.enumerize :foos, in: %w(a b c), multiple: true
|
29
44
|
object.wont_respond_to :foos_text
|
@@ -33,4 +48,10 @@ describe Enumerize::Base do
|
|
33
48
|
klass.enumerize :foos, in: %w(a b c), multiple: true
|
34
49
|
object.wont_respond_to :foos_value
|
35
50
|
end
|
51
|
+
|
52
|
+
it "cannot define multiple with scope" do
|
53
|
+
assert_raises ArgumentError do
|
54
|
+
klass.enumerize :foos, in: %w(a b c), multiple: true, scope: true
|
55
|
+
end
|
56
|
+
end
|
36
57
|
end
|
data/test/predicates_test.rb
CHANGED
@@ -15,6 +15,12 @@ describe Enumerize::Predicates do
|
|
15
15
|
object.must_respond_to :b?
|
16
16
|
end
|
17
17
|
|
18
|
+
it 'creates predicate methods when enumerized values have dash in it' do
|
19
|
+
klass.enumerize(:foo, in: %w(foo-bar bar-foo), predicates: true)
|
20
|
+
object.must_respond_to :foo_bar?
|
21
|
+
object.must_respond_to :bar_foo?
|
22
|
+
end
|
23
|
+
|
18
24
|
it 'creates predicate methods on multiple attribute' do
|
19
25
|
klass.enumerize(:foo, in: %w(a b), predicates: true, multiple: true)
|
20
26
|
object.must_respond_to :a?
|