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.
@@ -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
- validates attribute, :inclusion => { :in => values + [nil]}
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
- define_method "human_#{attribute}" do
52
- self.class.human_enums[attribute][self[attribute]]
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
- define_method "#{attribute}=" do | value |
56
- value = value.to_s
57
- value = nil if value == ''
58
- self[attribute] = value
79
+ if options[:multiple]
80
+ define_method attribute do
81
+ self[attribute] ||= []
82
+ end
59
83
  end
60
84
  end
61
85
  end
@@ -1,3 +1,3 @@
1
1
  module HasEnum
2
- VERSION = "0.7.3"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -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
@@ -4,6 +4,10 @@ ru:
4
4
  small: "Маленький"
5
5
  medium: "Средний"
6
6
  large: "Большой"
7
+ speed_enum:
8
+ slow: Медленный
9
+ normal: Нормальный
10
+ fast: Быстрый
7
11
 
8
12
  activerecord:
9
13
  attributes:
data/spec/spec_helper.rb CHANGED
@@ -16,6 +16,7 @@ ActiveRecord::Schema.define(:version => 0) do
16
16
  t.string "size"
17
17
  t.string "status"
18
18
  t.string "state"
19
+ t.string "speed"
19
20
  end
20
21
 
21
22
  create_table "schema_info", :id => false, :force => true do |t|
data/spec/test_model.rb CHANGED
@@ -8,5 +8,7 @@ class TestModel < ActiveRecord::Base
8
8
 
9
9
  has_enum ["status", :state], [:pending, :failed, :done], :presence => true
10
10
 
11
+ has_enum :speed, %w[slow normal fast], :multiple => true, :presence => true
12
+
11
13
  end
12
14
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 7
8
- - 3
9
- version: 0.7.3
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-03-29 00:00:00 +07:00
19
+ date: 2011-04-11 00:00:00 +07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency