has_enum 0.7.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/has_enum/class_methods.rb +32 -8
- data/lib/has_enum/version.rb +1 -1
- data/spec/has_enum_spec.rb +34 -1
- data/spec/ru.yml +4 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/test_model.rb +2 -0
- metadata +4 -4
@@ -15,7 +15,7 @@ module HasEnum::ClassMethods
|
|
15
15
|
|
16
16
|
def has_enum(*params)
|
17
17
|
options = params.extract_options!
|
18
|
-
options.assert_valid_keys(:query_methods, :scopes, :presence)
|
18
|
+
options.assert_valid_keys(:query_methods, :scopes, :presence, :multiple)
|
19
19
|
|
20
20
|
raise ArgumentError, "Empty arguments list for has_enum call" if params.empty?
|
21
21
|
|
@@ -35,7 +35,12 @@ module HasEnum::ClassMethods
|
|
35
35
|
|
36
36
|
enums[attribute] = values
|
37
37
|
|
38
|
-
|
38
|
+
if options[:multiple]
|
39
|
+
serialize attribute, Array
|
40
|
+
else
|
41
|
+
validates attribute, :inclusion => { :in => values + [nil]}
|
42
|
+
end
|
43
|
+
|
39
44
|
validates attribute, :presence => options[:presence] if options[:presence]
|
40
45
|
|
41
46
|
values.each do |val|
|
@@ -48,14 +53,33 @@ module HasEnum::ClassMethods
|
|
48
53
|
end
|
49
54
|
end if options[:query_methods] != false
|
50
55
|
|
51
|
-
|
52
|
-
|
56
|
+
if options[:multiple]
|
57
|
+
define_method "human_#{attribute}" do
|
58
|
+
return nil if self.send(attribute).empty?
|
59
|
+
self.send(attribute).map{|v| self.class.human_enums[attribute][v]}
|
60
|
+
end
|
61
|
+
else
|
62
|
+
define_method "human_#{attribute}" do
|
63
|
+
self.class.human_enums[attribute][self[attribute]]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
if options[:multiple]
|
68
|
+
define_method "#{attribute}=" do | values |
|
69
|
+
self[attribute] = [*values].compact.delete_if{|v| v.blank?}
|
70
|
+
end
|
71
|
+
else
|
72
|
+
define_method "#{attribute}=" do | value |
|
73
|
+
value = value.to_s
|
74
|
+
value = nil if value == ''
|
75
|
+
self[attribute] = value
|
76
|
+
end
|
53
77
|
end
|
54
78
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
79
|
+
if options[:multiple]
|
80
|
+
define_method attribute do
|
81
|
+
self[attribute] ||= []
|
82
|
+
end
|
59
83
|
end
|
60
84
|
end
|
61
85
|
end
|
data/lib/has_enum/version.rb
CHANGED
data/spec/has_enum_spec.rb
CHANGED
@@ -4,7 +4,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
4
4
|
|
5
5
|
describe HasEnum do
|
6
6
|
let :model do
|
7
|
-
TestModel.new(:category => :stuff, :status => :pending, :state => :done)
|
7
|
+
TestModel.new(:category => :stuff, :status => :pending, :state => :done, :speed => :slow)
|
8
8
|
end
|
9
9
|
|
10
10
|
let :human_enums do
|
@@ -34,6 +34,11 @@ describe HasEnum do
|
|
34
34
|
:failed => 'Failed',
|
35
35
|
:done => 'Done'
|
36
36
|
},
|
37
|
+
:speed => {
|
38
|
+
:slow => 'Медленный',
|
39
|
+
:normal => 'Нормальный',
|
40
|
+
:fast => 'Быстрый'
|
41
|
+
}
|
37
42
|
}.with_indifferent_access
|
38
43
|
end
|
39
44
|
|
@@ -195,4 +200,32 @@ describe HasEnum do
|
|
195
200
|
model.should_not be_valid
|
196
201
|
end
|
197
202
|
end
|
203
|
+
|
204
|
+
describe "speed enum" do
|
205
|
+
it "should return empty array for nil speed" do
|
206
|
+
model.speed = nil
|
207
|
+
model.speed.should eql []
|
208
|
+
model.should_not be_valid
|
209
|
+
end
|
210
|
+
|
211
|
+
it "empty array is not valid value for speed" do
|
212
|
+
model.speed = []
|
213
|
+
model.should_not be_valid
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should support multiple values for attribute" do
|
217
|
+
model.speed = [:normal, :fast]
|
218
|
+
model.should be_valid
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should return nil for human_speed if speed not initialized" do
|
222
|
+
model.speed = []
|
223
|
+
model.human_speed.should be_nil
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should return array of translated values for human_speed" do
|
227
|
+
model.speed = [:normal, :fast]
|
228
|
+
model.human_speed.should eql %w[Нормальный Быстрый]
|
229
|
+
end
|
230
|
+
end
|
198
231
|
end
|
data/spec/ru.yml
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/test_model.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 8
|
8
|
+
- 0
|
9
|
+
version: 0.8.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Andreas Korth
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-04-11 00:00:00 +07:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|