enumerate_it 1.3.0 → 1.3.1.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +40 -0
- data/.travis.yml +23 -8
- data/Appraisals +11 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +33 -31
- data/README.md +7 -12
- data/Rakefile +12 -8
- data/enumerate_it.gemspec +6 -2
- data/gemfiles/{activesupport_4_0.gemfile → rails_3.0.gemfile} +2 -2
- data/gemfiles/{activesupport_3_0.gemfile → rails_3.1.gemfile} +2 -2
- data/gemfiles/{activesupport_3_1.gemfile → rails_3.2.gemfile} +2 -2
- data/gemfiles/{activesupport_3_2.gemfile → rails_4.0.gemfile} +2 -2
- data/gemfiles/rails_4.1.gemfile +8 -0
- data/gemfiles/rails_4.2.gemfile +8 -0
- data/gemfiles/rails_5.0.gemfile +8 -0
- data/lib/enumerate_it/base.rb +98 -86
- data/lib/enumerate_it/class_methods.rb +33 -25
- data/lib/enumerate_it/version.rb +1 -1
- data/lib/generators/enumerate_it/enum/enum_generator.rb +5 -7
- data/lib/generators/enumerate_it/install/install_generator.rb +1 -1
- data/spec/enumerate_it/base_spec.rb +100 -86
- data/spec/enumerate_it_spec.rb +77 -73
- data/spec/spec_helper.rb +1 -1
- data/spec/support/test_classes.rb +4 -4
- metadata +27 -12
- data/.document +0 -5
- data/gemfiles/activesupport_4_1.gemfile +0 -8
- data/gemfiles/activesupport_4_2.gemfile +0 -8
@@ -1,30 +1,31 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module EnumerateIt
|
3
4
|
module ClassMethods
|
4
5
|
def has_enumeration_for(attribute, options = {})
|
5
|
-
self.enumerations =
|
6
|
+
self.enumerations = enumerations.dup
|
6
7
|
|
7
|
-
define_enumeration_class
|
8
|
-
create_enumeration_humanize_method
|
9
|
-
store_enumeration
|
8
|
+
define_enumeration_class(attribute, options)
|
9
|
+
create_enumeration_humanize_method(options[:with], attribute)
|
10
|
+
store_enumeration(options[:with], attribute)
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
handle_options(attribute, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def handle_options(attribute, options)
|
18
|
+
set_validations(attribute, options) unless options[:skip_validation]
|
14
19
|
|
15
20
|
if options[:create_helpers]
|
16
|
-
create_helper_methods
|
17
|
-
|
18
|
-
|
21
|
+
%w(create_helper_methods create_mutator_methods create_polymorphic_methods).each do |method|
|
22
|
+
send(method, options[:with], attribute, options[:create_helpers])
|
23
|
+
end
|
19
24
|
end
|
20
25
|
|
21
|
-
if options[:create_scopes]
|
22
|
-
create_scopes options[:with], attribute, options[:create_scopes]
|
23
|
-
end
|
26
|
+
create_scopes options[:with], attribute, options[:create_scopes] if options[:create_scopes]
|
24
27
|
end
|
25
28
|
|
26
|
-
private
|
27
|
-
|
28
29
|
def store_enumeration(klass, attribute)
|
29
30
|
enumerations[attribute] = klass
|
30
31
|
end
|
@@ -32,7 +33,7 @@ module EnumerateIt
|
|
32
33
|
def create_enumeration_humanize_method(klass, attribute_name)
|
33
34
|
class_eval do
|
34
35
|
define_method "#{attribute_name}_humanize" do
|
35
|
-
values = klass.enumeration.values.detect { |v| v[0] ==
|
36
|
+
values = klass.enumeration.values.detect { |v| v[0] == send(attribute_name) }
|
36
37
|
|
37
38
|
values ? klass.translate(values[1]) : nil
|
38
39
|
end
|
@@ -45,7 +46,7 @@ module EnumerateIt
|
|
45
46
|
class_eval do
|
46
47
|
klass.enumeration.keys.each do |option|
|
47
48
|
define_method "#{prefix_name}#{option}?" do
|
48
|
-
|
49
|
+
send(attribute_name) == klass.enumeration[option].first
|
49
50
|
end
|
50
51
|
end
|
51
52
|
end
|
@@ -56,7 +57,7 @@ module EnumerateIt
|
|
56
57
|
|
57
58
|
klass.enumeration.keys.each do |key|
|
58
59
|
if respond_to? :scope
|
59
|
-
scope "#{prefix_name}#{key}",
|
60
|
+
scope "#{prefix_name}#{key}", -> { where(attribute_name => klass.enumeration[key].first) }
|
60
61
|
end
|
61
62
|
end
|
62
63
|
end
|
@@ -67,7 +68,7 @@ module EnumerateIt
|
|
67
68
|
class_eval do
|
68
69
|
klass.enumeration.each_pair do |key, values|
|
69
70
|
define_method "#{prefix_name}#{key}!" do
|
70
|
-
|
71
|
+
send "#{attribute_name}=", values.first
|
71
72
|
end
|
72
73
|
end
|
73
74
|
end
|
@@ -77,11 +78,11 @@ module EnumerateIt
|
|
77
78
|
return unless helpers.is_a?(Hash) && helpers[:polymorphic]
|
78
79
|
options = helpers[:polymorphic]
|
79
80
|
suffix = options.is_a?(Hash) && options[:suffix]
|
80
|
-
suffix ||=
|
81
|
+
suffix ||= '_object'
|
81
82
|
|
82
83
|
class_eval do
|
83
84
|
define_method "#{attribute_name}#{suffix}" do
|
84
|
-
value =
|
85
|
+
value = public_send(attribute_name)
|
85
86
|
|
86
87
|
klass.const_get(klass.key_for(value).to_s.camelize).new if value
|
87
88
|
end
|
@@ -89,14 +90,21 @@ module EnumerateIt
|
|
89
90
|
end
|
90
91
|
|
91
92
|
def define_enumeration_class(attribute, options)
|
92
|
-
if options[:with]
|
93
|
-
|
94
|
-
|
93
|
+
return if options[:with]
|
94
|
+
|
95
|
+
inner_enum_class_name = attribute.to_s.camelize.to_sym
|
96
|
+
|
97
|
+
options[:with] = if constants.include?(inner_enum_class_name)
|
98
|
+
const_get(inner_enum_class_name)
|
99
|
+
else
|
100
|
+
attribute.to_s.camelize.constantize
|
95
101
|
end
|
96
102
|
end
|
97
103
|
|
98
104
|
def set_validations(attribute, options)
|
99
|
-
|
105
|
+
if respond_to?(:validates_inclusion_of)
|
106
|
+
validates_inclusion_of(attribute, in: options[:with].list, allow_blank: true)
|
107
|
+
end
|
100
108
|
|
101
109
|
if options[:required] && respond_to?(:validates_presence_of)
|
102
110
|
opts = options[:required].is_a?(Hash) ? options[:required] : {}
|
data/lib/enumerate_it/version.rb
CHANGED
@@ -9,14 +9,14 @@ module EnumerateIt
|
|
9
9
|
|
10
10
|
class_option :lang, type: 'string', desc: 'Lang to use in i18n', default: 'en'
|
11
11
|
|
12
|
-
desc
|
12
|
+
desc 'Creates a locale file on config/locales'
|
13
13
|
def create_locale
|
14
|
-
template
|
14
|
+
template 'locale.yml', File.join('config/locales', "#{singular_name}.yml")
|
15
15
|
end
|
16
16
|
|
17
|
-
desc
|
17
|
+
desc 'Creates an initializer file that extends ActiveRecord::Base with Enumerate_it'
|
18
18
|
def create_enumerate_it
|
19
|
-
template
|
19
|
+
template 'enumerate_it.rb', File.join('app/enumerations', "#{singular_name}.rb")
|
20
20
|
end
|
21
21
|
|
22
22
|
protected
|
@@ -37,9 +37,7 @@ module EnumerateIt
|
|
37
37
|
if attributes.first.type == :string
|
38
38
|
attributes.map(&:name)
|
39
39
|
else
|
40
|
-
attributes.map
|
41
|
-
[attribute.name, attribute.type]
|
42
|
-
end
|
40
|
+
attributes.map { |attribute| [attribute.name, attribute.type] }
|
43
41
|
end
|
44
42
|
end
|
45
43
|
end
|
@@ -4,7 +4,7 @@ module EnumerateIt
|
|
4
4
|
source_root File.expand_path('../templates', __FILE__)
|
5
5
|
|
6
6
|
def copy_initializer_file
|
7
|
-
template
|
7
|
+
template 'enumerate_it_initializer.rb', File.join('config/initializers/', 'enumerate_it.rb')
|
8
8
|
end
|
9
9
|
end
|
10
10
|
end
|
@@ -3,55 +3,57 @@
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
4
4
|
|
5
5
|
describe EnumerateIt::Base do
|
6
|
-
it
|
7
|
-
[TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3]
|
6
|
+
it 'creates constants for each enumeration value' do
|
7
|
+
constants = [TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3]
|
8
|
+
|
9
|
+
constants.each_with_index do |constant, idx|
|
8
10
|
expect(constant).to eq((idx + 1).to_s)
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
12
|
-
it
|
14
|
+
it 'creates constants for camel case values' do
|
13
15
|
expect(TestEnumerationWithCamelCase::IPHONE).to eq('iPhone')
|
14
16
|
end
|
15
17
|
|
16
|
-
it
|
18
|
+
it 'creates constants replacing its dashes with underscores' do
|
17
19
|
expect(TestEnumerationWithDash::PT_BR).to eq('pt-BR')
|
18
20
|
end
|
19
21
|
|
20
|
-
describe
|
22
|
+
describe '.list' do
|
21
23
|
it "creates a method that returns the allowed values in the enumeration's class" do
|
22
|
-
expect(TestEnumeration.list).to eq(
|
24
|
+
expect(TestEnumeration.list).to eq(%w(1 2 3))
|
23
25
|
end
|
24
26
|
|
25
|
-
context
|
27
|
+
context 'specifying a default sort mode' do
|
26
28
|
subject { create_enumeration_class_with_sort_mode(sort_mode).list }
|
27
29
|
|
28
|
-
context
|
30
|
+
context 'by value' do
|
29
31
|
let(:sort_mode) { :value }
|
30
32
|
|
31
33
|
it { should eq(%w(0 1 2 3)) }
|
32
34
|
end
|
33
35
|
|
34
|
-
context
|
36
|
+
context 'by name' do
|
35
37
|
let(:sort_mode) { :name }
|
36
38
|
|
37
39
|
it { should eq(%w(2 1 3 0)) }
|
38
40
|
end
|
39
41
|
|
40
|
-
context
|
42
|
+
context 'by translation' do
|
41
43
|
let(:sort_mode) { :translation }
|
42
44
|
|
43
45
|
it { should eq(%w(3 2 0 1)) }
|
44
46
|
end
|
45
47
|
|
46
|
-
context
|
48
|
+
context 'by nothing' do
|
47
49
|
let(:sort_mode) { :none }
|
48
50
|
|
49
51
|
it { should eq(%w(1 2 3 0)) }
|
50
52
|
end
|
51
53
|
end
|
52
|
-
|
54
|
+
end
|
53
55
|
|
54
|
-
it
|
56
|
+
it 'creates a method that returns the enumeration specification' do
|
55
57
|
expect(TestEnumeration.enumeration).to eq(
|
56
58
|
value_1: ['1', 'Hey, I am 1!'],
|
57
59
|
value_2: ['2', 'Hey, I am 2!'],
|
@@ -59,29 +61,29 @@ describe EnumerateIt::Base do
|
|
59
61
|
)
|
60
62
|
end
|
61
63
|
|
62
|
-
describe
|
63
|
-
it
|
64
|
+
describe '.length' do
|
65
|
+
it 'returns the length of the enumeration' do
|
64
66
|
expect(TestEnumeration.length).to eq(3)
|
65
67
|
end
|
66
68
|
end
|
67
69
|
|
68
|
-
describe
|
70
|
+
describe '.each_translation' do
|
69
71
|
it "yields each enumeration's value translation" do
|
70
72
|
translations = []
|
71
73
|
TestEnumeration.each_translation do |translation|
|
72
74
|
translations << translation
|
73
75
|
end
|
74
|
-
expect(translations).to eq([
|
76
|
+
expect(translations).to eq(['Hey, I am 1!', 'Hey, I am 2!', 'Hey, I am 3!'])
|
75
77
|
end
|
76
78
|
end
|
77
79
|
|
78
|
-
describe
|
79
|
-
it
|
80
|
-
expect(TestEnumeration.translations).to eq([
|
80
|
+
describe '.translations' do
|
81
|
+
it 'returns all translations' do
|
82
|
+
expect(TestEnumeration.translations).to eq(['Hey, I am 1!', 'Hey, I am 2!', 'Hey, I am 3!'])
|
81
83
|
end
|
82
84
|
end
|
83
85
|
|
84
|
-
describe
|
86
|
+
describe '.each_value' do
|
85
87
|
it "yields each enumeration's value" do
|
86
88
|
values = []
|
87
89
|
TestEnumeration.each_value do |value|
|
@@ -91,151 +93,161 @@ describe EnumerateIt::Base do
|
|
91
93
|
end
|
92
94
|
end
|
93
95
|
|
94
|
-
describe
|
95
|
-
it
|
96
|
-
expect(TestEnumeration.to_a)
|
96
|
+
describe '.to_a' do
|
97
|
+
it 'returns an array with the values and human representations' do
|
98
|
+
expect(TestEnumeration.to_a)
|
99
|
+
.to eq([['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']])
|
97
100
|
end
|
98
101
|
|
99
|
-
it
|
102
|
+
it 'translates the available values' do
|
100
103
|
I18n.locale = :en
|
101
104
|
expect(TestEnumerationWithoutArray.to_a).to eq([['First Value', '1'], ['Value Two', '2']])
|
102
105
|
I18n.locale = :pt
|
103
106
|
expect(TestEnumerationWithoutArray.to_a).to eq([['Primeiro Valor', '1'], ['Value Two', '2']])
|
104
107
|
end
|
105
108
|
|
106
|
-
it
|
107
|
-
expect(TestEnumerationWithExtendedBehaviour.to_a).to eq([
|
109
|
+
it 'can be extended from the enumeration class' do
|
110
|
+
expect(TestEnumerationWithExtendedBehaviour.to_a).to eq([%w(Second 2), %w(First 1)])
|
108
111
|
end
|
109
112
|
end
|
110
113
|
|
111
|
-
describe
|
112
|
-
it
|
114
|
+
describe '.to_json' do
|
115
|
+
it 'gives a valid json back' do
|
113
116
|
I18n.locale = :inexsistent
|
114
|
-
expect(TestEnumerationWithoutArray.to_json)
|
117
|
+
expect(TestEnumerationWithoutArray.to_json)
|
118
|
+
.to eq('[{"value":"1","label":"Value One"},{"value":"2","label":"Value Two"}]')
|
115
119
|
end
|
116
120
|
|
117
|
-
it
|
121
|
+
it 'give translated values when available' do
|
118
122
|
I18n.locale = :pt
|
119
|
-
expect(TestEnumerationWithoutArray.to_json)
|
123
|
+
expect(TestEnumerationWithoutArray.to_json)
|
124
|
+
.to eq('[{"value":"1","label":"Primeiro Valor"},{"value":"2","label":"Value Two"}]')
|
120
125
|
end
|
121
126
|
end
|
122
127
|
|
123
|
-
describe
|
124
|
-
it
|
128
|
+
describe '.t' do
|
129
|
+
it 'translates a given value' do
|
125
130
|
I18n.locale = :pt
|
126
131
|
expect(TestEnumerationWithoutArray.t('1')).to eq('Primeiro Valor')
|
127
132
|
end
|
128
133
|
end
|
129
134
|
|
130
|
-
describe
|
135
|
+
describe '.to_range' do
|
131
136
|
it "returns a Range object containing the enumeration's value interval" do
|
132
|
-
expect(TestEnumeration.to_range).to eq(
|
137
|
+
expect(TestEnumeration.to_range).to eq('1'..'3')
|
133
138
|
end
|
134
139
|
end
|
135
140
|
|
136
|
-
describe
|
137
|
-
it "returns an array
|
138
|
-
expect(TestEnumeration.values_for(%w(VALUE_1 VALUE_2)))
|
141
|
+
describe '.values_for' do
|
142
|
+
it "returns an array representing some of the enumeration's values" do
|
143
|
+
expect(TestEnumeration.values_for(%w(VALUE_1 VALUE_2)))
|
144
|
+
.to eq([TestEnumeration::VALUE_1, TestEnumeration::VALUE_2])
|
139
145
|
end
|
140
146
|
|
141
|
-
it
|
142
|
-
expect(TestEnumeration.values_for(%w(VALUE_1 THIS_IS_WRONG)))
|
147
|
+
it 'returns nil if the a constant named after one of the given strings cannot be found' do
|
148
|
+
expect(TestEnumeration.values_for(%w(VALUE_1 THIS_IS_WRONG)))
|
149
|
+
.to eq([TestEnumeration::VALUE_1, nil])
|
143
150
|
end
|
144
151
|
end
|
145
152
|
|
146
|
-
describe
|
153
|
+
describe '.value_for' do
|
147
154
|
it "returns the enumeration's value" do
|
148
|
-
expect(TestEnumeration.value_for(
|
155
|
+
expect(TestEnumeration.value_for('VALUE_1')).to eq(TestEnumeration::VALUE_1)
|
149
156
|
end
|
150
157
|
|
151
|
-
context
|
152
|
-
it
|
153
|
-
expect(TestEnumeration.value_for(
|
158
|
+
context 'when a constant named after the received value cannot be found' do
|
159
|
+
it 'returns nil' do
|
160
|
+
expect(TestEnumeration.value_for('THIS_IS_WRONG')).to be_nil
|
154
161
|
end
|
155
162
|
end
|
156
163
|
end
|
157
164
|
|
158
|
-
describe
|
159
|
-
it
|
160
|
-
expect(TestEnumeration.value_from_key(
|
165
|
+
describe '.value_from_key' do
|
166
|
+
it 'returns the correct value when the key is a string' do
|
167
|
+
expect(TestEnumeration.value_from_key('value_1')).to eq(TestEnumeration::VALUE_1)
|
161
168
|
end
|
162
169
|
|
163
|
-
it
|
170
|
+
it 'returns the correct value when the key is a symbol' do
|
164
171
|
expect(TestEnumeration.value_from_key(:value_1)).to eq(TestEnumeration::VALUE_1)
|
165
172
|
end
|
166
173
|
|
167
|
-
it
|
168
|
-
expect(TestEnumeration.value_from_key(
|
174
|
+
it 'returns nil when the key does not exist in the enumeration' do
|
175
|
+
expect(TestEnumeration.value_from_key('wrong')).to be_nil
|
169
176
|
end
|
170
177
|
|
171
|
-
it
|
178
|
+
it 'returns nil when the given value is nil' do
|
172
179
|
expect(TestEnumeration.value_from_key(nil)).to be_nil
|
173
180
|
end
|
174
181
|
end
|
175
182
|
|
176
|
-
describe
|
177
|
-
it
|
183
|
+
describe '.keys' do
|
184
|
+
it 'returns a list with the keys used to define the enumeration' do
|
178
185
|
expect(TestEnumeration.keys).to eq([:value_1, :value_2, :value_3])
|
179
186
|
end
|
180
187
|
end
|
181
188
|
|
182
|
-
describe
|
183
|
-
it
|
189
|
+
describe '.key_for' do
|
190
|
+
it 'returns the key for the given value inside the enumeration' do
|
184
191
|
expect(TestEnumeration.key_for(TestEnumeration::VALUE_1)).to eq(:value_1)
|
185
192
|
end
|
186
193
|
|
187
|
-
it
|
188
|
-
expect(TestEnumeration.key_for(
|
194
|
+
it 'returns nil if the enumeration does not have the given value' do
|
195
|
+
expect(TestEnumeration.key_for('foo')).to be_nil
|
189
196
|
end
|
190
197
|
end
|
191
198
|
|
192
199
|
context 'associate values with a list' do
|
193
|
-
it
|
194
|
-
expect(TestEnumerationWithList::FIRST).to eq(
|
195
|
-
expect(TestEnumerationWithList::SECOND).to eq(
|
200
|
+
it 'creates constants for each enumeration value' do
|
201
|
+
expect(TestEnumerationWithList::FIRST).to eq('first')
|
202
|
+
expect(TestEnumerationWithList::SECOND).to eq('second')
|
196
203
|
end
|
197
204
|
|
198
|
-
it
|
199
|
-
expect(TestEnumerationWithList.to_a).to eq([
|
205
|
+
it 'returns an array with the values and human representations' do
|
206
|
+
expect(TestEnumerationWithList.to_a).to eq([%w(First first), %w(Second second)])
|
200
207
|
end
|
201
208
|
end
|
202
209
|
|
203
|
-
context
|
210
|
+
context 'specifying a default sort mode' do
|
204
211
|
subject { create_enumeration_class_with_sort_mode(sort_mode).to_a }
|
205
212
|
|
206
|
-
context
|
213
|
+
context 'by value' do
|
207
214
|
let(:sort_mode) { :value }
|
208
215
|
|
209
|
-
it { is_expected.to eq([
|
216
|
+
it { is_expected.to eq([%w(jkl 0), %w(xyz 1), %w(fgh 2), %w(abc 3)]) }
|
210
217
|
end
|
211
218
|
|
212
|
-
context
|
219
|
+
context 'by name' do
|
213
220
|
let(:sort_mode) { :name }
|
214
221
|
|
215
|
-
it { is_expected.to eq([
|
222
|
+
it { is_expected.to eq([%w(fgh 2), %w(xyz 1), %w(abc 3), %w(jkl 0)]) }
|
216
223
|
end
|
217
224
|
|
218
|
-
context
|
225
|
+
context 'by translation' do
|
219
226
|
let(:sort_mode) { :translation }
|
220
227
|
|
221
|
-
it { is_expected.to eq([
|
228
|
+
it { is_expected.to eq([%w(abc 3), %w(fgh 2), %w(jkl 0), %w(xyz 1)]) }
|
222
229
|
end
|
223
230
|
|
224
|
-
context
|
231
|
+
context 'by nothing' do
|
225
232
|
let(:sort_mode) { :none }
|
226
233
|
|
227
|
-
it { is_expected.to eq([
|
234
|
+
it { is_expected.to eq([%w(xyz 1), %w(fgh 2), %w(abc 3), %w(jkl 0)]) }
|
228
235
|
end
|
229
236
|
end
|
230
237
|
|
231
|
-
context
|
238
|
+
context 'when included in ActiveRecord::Base' do
|
232
239
|
before do
|
233
240
|
class ActiveRecordStub
|
234
241
|
attr_accessor :bla
|
235
242
|
|
236
243
|
class << self
|
237
|
-
def validates_inclusion_of(
|
238
|
-
|
244
|
+
def validates_inclusion_of(_)
|
245
|
+
true
|
246
|
+
end
|
247
|
+
|
248
|
+
def validates_presence_of
|
249
|
+
true
|
250
|
+
end
|
239
251
|
end
|
240
252
|
end
|
241
253
|
|
@@ -243,50 +255,52 @@ describe EnumerateIt::Base do
|
|
243
255
|
ActiveRecordStub.extend EnumerateIt
|
244
256
|
end
|
245
257
|
|
246
|
-
it
|
247
|
-
expect(ActiveRecordStub)
|
258
|
+
it 'creates a validation for inclusion' do
|
259
|
+
expect(ActiveRecordStub)
|
260
|
+
.to receive(:validates_inclusion_of).with(:bla, in: TestEnumeration.list, allow_blank: true)
|
261
|
+
|
248
262
|
class ActiveRecordStub
|
249
263
|
has_enumeration_for :bla, with: TestEnumeration
|
250
264
|
end
|
251
265
|
end
|
252
266
|
|
253
|
-
context
|
267
|
+
context 'using the :required option' do
|
254
268
|
before do
|
255
269
|
allow(ActiveRecordStub).to receive(:validates_presence_of).and_return(true)
|
256
270
|
end
|
257
271
|
|
258
|
-
it
|
272
|
+
it 'creates a validation for presence' do
|
259
273
|
expect(ActiveRecordStub).to receive(:validates_presence_of)
|
260
274
|
class ActiveRecordStub
|
261
275
|
has_enumeration_for :bla, with: TestEnumeration, required: true
|
262
276
|
end
|
263
277
|
end
|
264
278
|
|
265
|
-
it
|
279
|
+
it 'passes the given options to the validation method' do
|
266
280
|
expect(ActiveRecordStub).to receive(:validates_presence_of).with(:bla, if: :some_method)
|
267
281
|
class ActiveRecordStub
|
268
282
|
has_enumeration_for :bla, with: TestEnumeration, required: { if: :some_method }
|
269
283
|
end
|
270
284
|
end
|
271
285
|
|
272
|
-
it
|
273
|
-
expect(ActiveRecordStub).
|
286
|
+
it 'do not require the attribute by default' do
|
287
|
+
expect(ActiveRecordStub).not_to receive(:validates_presence_of)
|
274
288
|
class ActiveRecordStub
|
275
289
|
has_enumeration_for :bla, with: TestEnumeration
|
276
290
|
end
|
277
291
|
end
|
278
292
|
end
|
279
293
|
|
280
|
-
context
|
294
|
+
context 'using :skip_validation option' do
|
281
295
|
it "doesn't create a validation for inclusion" do
|
282
|
-
expect(ActiveRecordStub).
|
296
|
+
expect(ActiveRecordStub).not_to receive(:validates_inclusion_of)
|
283
297
|
class ActiveRecordStub
|
284
298
|
has_enumeration_for :bla, with: TestEnumeration, skip_validation: true
|
285
299
|
end
|
286
300
|
end
|
287
301
|
|
288
302
|
it "doesn't create a validation for presence" do
|
289
|
-
expect(ActiveRecordStub).
|
303
|
+
expect(ActiveRecordStub).not_to receive(:validates_presence_of)
|
290
304
|
class ActiveRecordStub
|
291
305
|
has_enumeration_for :bla, with: TestEnumeration, require: true, skip_validation: true
|
292
306
|
end
|