simple_enum 1.6.4 → 1.6.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA512:
3
+ metadata.gz: 891cf333ea0c52d94a7a8d40a2a861f3197ffa23c1f74cc0b127ef136e6df86f1f66d7db8fa94ce88669954a5eb6c551957a2c1c69c17aadf2b608f728104120
4
+ data.tar.gz: 6ac9b3af113c20669f840f748303a36182e25f94bd41565b13dda5fe3d70aeecd86b215393ad0ca36c6500963ed95e08e32a80453907501f33b4166549db6a66
5
+ SHA1:
6
+ metadata.gz: bd0a0e232a1fc335dff5f5f94c6ec8bf91c9d993
7
+ data.tar.gz: c6b7b1bdeb9bfee01cef4fc85f34172465528178
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_enum (1.6.4)
4
+ simple_enum (1.6.5)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,38 +1,56 @@
1
- module SimpleEnum
2
- module Validation
3
- # Validates an +as_enum+ field based on the value of it's column.
4
- #
5
- # Model:
6
- # class User < ActiveRecord::Base
7
- # as_enum :gender, [ :male, :female ]
8
- # validates_as_enum :gender
9
- # end
10
- #
11
- # View:
12
- # <%= select(:user, :gender, User.genders.keys) %>
13
- #
14
- # Configuration options:
15
- # * <tt>:message</tt> - A custom error message (default: is <tt>[:activerecord, :errors, :messages, :invalid_enum]</tt>).
16
- # * <tt>:on</tt> - Specifies when this validation is active (default is always, other options <tt>:create</tt>, <tt>:update</tt>).
17
- # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
18
- # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
19
- # method, proc or string should return or evaluate to a true or false value.
20
- # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
21
- # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
22
- # method, proc or string should return or evaluate to a true or false value.
23
- def validates_as_enum(*attr_names)
24
- configuration = attr_names.extract_options!
1
+ module ActiveModel
2
+ module Validations
3
+ class AsEnumValidator < ActiveModel::Validator
4
+ attr_reader :attributes
25
5
 
26
- attr_names.map! { |e| enum_definitions[e][:column] } # map to column name
6
+ def initialize(options)
7
+ @attributes = Array.wrap(options.delete(:attributes))
8
+ raise ":attributes cannot be blank" if @attributes.empty?
9
+ super
10
+ end
11
+
12
+ def setup(klass)
13
+ @klass = klass
14
+ end
15
+
16
+ def validate(record)
17
+ attributes.each do |attribute|
18
+ enum_def = @klass.enum_definitions[attribute]
19
+ raw_value = record.send(enum_def[:column])
27
20
 
28
- validates_each(attr_names, configuration) do |record, attr_name, value|
29
- enum_def = enum_definitions[attr_name]
30
- unless send(enum_def[:name].to_s.pluralize).values.include?(value)
31
- params = { :value => value}
32
- params[:message] = configuration[:message] if configuration[:message].present?
33
- record.errors.add(enum_def[:name], :invalid_enum, params)
21
+ next if (raw_value.nil? && options[:allow_nil]) || (raw_value.blank? && options[:allow_blank])
22
+
23
+ unless @klass.send(enum_def[:name].to_s.pluralize).values.include?(raw_value)
24
+ record.errors.add(attribute, :invalid_enum, options)
25
+ end
34
26
  end
35
27
  end
36
28
  end
29
+
30
+ module HelperMethods
31
+ # Validates an +as_enum+ field based on the value of it's column.
32
+ #
33
+ # Model:
34
+ # class User < ActiveRecord::Base
35
+ # as_enum :gender, [ :male, :female ]
36
+ # validates_as_enum :gender
37
+ # end
38
+ #
39
+ # View:
40
+ # <%= select(:user, :gender, User.genders.keys) %>
41
+ #
42
+ # Configuration options:
43
+ # * <tt>:message</tt> - A custom error message (default: is <tt>[:activerecord, :errors, :messages, :invalid_enum]</tt>).
44
+ # * <tt>:on</tt> - Specifies when this validation is active (default is always, other options <tt>:create</tt>, <tt>:update</tt>).
45
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
46
+ # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
47
+ # method, proc or string should return or evaluate to a true or false value.
48
+ # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
49
+ # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
50
+ # method, proc or string should return or evaluate to a true or false value.
51
+ def validates_as_enum(*attr_names)
52
+ validates_with AsEnumValidator, _merge_attributes(attr_names)
53
+ end
54
+ end
37
55
  end
38
56
  end
@@ -1,5 +1,5 @@
1
1
  module SimpleEnum
2
2
 
3
3
  # +SimpleEnum+ version string.
4
- VERSION = "1.6.4"
4
+ VERSION = "1.6.5"
5
5
  end
data/lib/simple_enum.rb CHANGED
@@ -176,7 +176,7 @@ module SimpleEnum
176
176
 
177
177
  # generate getter
178
178
  define_method("#{enum_cd}") do
179
- id = respond_to?(:read_attribute) ? read_attribute(options[:column]) : send(options[:column])
179
+ id = send(options[:column])
180
180
  values_inverted[id]
181
181
  end
182
182
 
@@ -185,7 +185,7 @@ module SimpleEnum
185
185
  real = new_value.blank? ? nil : values[EnumHash.symbolize(new_value)]
186
186
  real = new_value if real.nil? && values_inverted[new_value].present?
187
187
  raise(ArgumentError, "Invalid enumeration value: #{new_value}") if (options[:whiny] and real.nil? and !new_value.blank?)
188
- respond_to?(:write_attribute) ? write_attribute(options[:column], real) : send("#{options[:column]}=", real)
188
+ send("#{options[:column]}=", real)
189
189
  end
190
190
 
191
191
  # generate checker
@@ -244,11 +244,11 @@ module SimpleEnum
244
244
  sym = EnumHash.symbolize(k)
245
245
 
246
246
  define_method("#{prefix}#{sym}?") do
247
- current = respond_to?(:read_attribute) ? read_attribute(options[:column]) : send(options[:column])
247
+ current = send(options[:column])
248
248
  code == current
249
249
  end
250
250
  define_method("#{prefix}#{sym}!") do
251
- respond_to?(:write_attribute) ? write_attribute(options[:column], code) : send("#{options[:column]}=", code)
251
+ send("#{options[:column]}=", code)
252
252
  sym
253
253
  end
254
254
 
@@ -260,8 +260,6 @@ module SimpleEnum
260
260
  end
261
261
  end
262
262
 
263
- include Validation
264
-
265
263
  def human_enum_name(enum, key, options = {})
266
264
  defaults = lookup_ancestors.map do |klass|
267
265
  :"#{self.i18n_scope}.enums.#{klass.model_name.i18n_key}.#{enum}.#{key}"
@@ -202,6 +202,25 @@ class SimpleEnumTest < MiniTest::Unit::TestCase
202
202
  assert_equal "invalid manufacturer", computer.errors[:manufacturer].first
203
203
  end
204
204
 
205
+ def test_validator
206
+ validator_comp = extend_computer do
207
+ validates :manufacturer, :operating_system, :as_enum => true
208
+ end
209
+
210
+ computer = validator_comp.new
211
+ assert !computer.save, "save should return false"
212
+ assert_equal(1, computer.errors[:manufacturer].size)
213
+ assert_equal(1, computer.errors[:operating_system].size)
214
+
215
+ computer.manufacturer_cd = 84321483219
216
+ assert !computer.save, "save should return false"
217
+ assert_equal(1, computer.errors[:manufacturer].size)
218
+
219
+ computer.manufacturer_cd = 0
220
+ computer.operating_system_cd = 0
221
+ assert_equal(true, computer.save)
222
+ end
223
+
205
224
  def test_that_argumenterror_is_raised_if_invalid_symbol_is_passed
206
225
  assert_raises ArgumentError do
207
226
  Dummy.new :gender => :foo
metadata CHANGED
@@ -1,13 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_enum
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
5
- prerelease:
6
- segments:
7
- - 1
8
- - 6
9
- - 4
10
- version: 1.6.4
4
+ version: 1.6.5
11
5
  platform: ruby
12
6
  authors:
13
7
  - Lukas Westermann
@@ -15,87 +9,58 @@ autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
11
 
18
- date: 2012-10-21 00:00:00 Z
12
+ date: 2013-05-27 00:00:00 Z
19
13
  dependencies:
20
14
  - !ruby/object:Gem::Dependency
21
- requirement: &id001 !ruby/object:Gem::Requirement
22
- none: false
15
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
16
  requirements:
24
17
  - - ">="
25
18
  - !ruby/object:Gem::Version
26
- hash: 7
27
- segments:
28
- - 3
29
- - 0
30
- - 0
31
19
  version: 3.0.0
32
- prerelease: false
33
- type: :runtime
34
20
  name: activesupport
35
- version_requirements: *id001
21
+ type: :runtime
22
+ requirement: *id001
23
+ prerelease: false
36
24
  - !ruby/object:Gem::Dependency
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- none: false
25
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
26
  requirements:
40
27
  - - ">="
41
28
  - !ruby/object:Gem::Version
42
- hash: 63
43
- segments:
44
- - 0
45
- - 9
46
- - 2
47
29
  version: 0.9.2
48
- prerelease: false
49
- type: :development
50
30
  name: rake
51
- version_requirements: *id002
31
+ type: :development
32
+ requirement: *id002
33
+ prerelease: false
52
34
  - !ruby/object:Gem::Dependency
53
- requirement: &id003 !ruby/object:Gem::Requirement
54
- none: false
35
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
36
  requirements:
56
37
  - - ">="
57
38
  - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 2
61
- - 3
62
- - 0
63
39
  version: 2.3.0
64
- prerelease: false
65
- type: :development
66
40
  name: minitest
67
- version_requirements: *id003
41
+ type: :development
42
+ requirement: *id003
43
+ prerelease: false
68
44
  - !ruby/object:Gem::Dependency
69
- requirement: &id004 !ruby/object:Gem::Requirement
70
- none: false
45
+ version_requirements: &id004 !ruby/object:Gem::Requirement
71
46
  requirements:
72
47
  - - ">="
73
48
  - !ruby/object:Gem::Version
74
- hash: 7
75
- segments:
76
- - 3
77
- - 0
78
- - 0
79
49
  version: 3.0.0
80
- prerelease: false
81
- type: :development
82
50
  name: activerecord
83
- version_requirements: *id004
51
+ type: :development
52
+ requirement: *id004
53
+ prerelease: false
84
54
  - !ruby/object:Gem::Dependency
85
- requirement: &id005 !ruby/object:Gem::Requirement
86
- none: false
55
+ version_requirements: &id005 !ruby/object:Gem::Requirement
87
56
  requirements:
88
57
  - - ~>
89
58
  - !ruby/object:Gem::Version
90
- hash: 3
91
- segments:
92
- - 2
93
- - 0
94
59
  version: "2.0"
95
- prerelease: false
96
- type: :development
97
60
  name: mongoid
98
- version_requirements: *id005
61
+ type: :development
62
+ requirement: *id005
63
+ prerelease: false
99
64
  description: Provides enum-like fields for ActiveRecord, ActiveModel and Mongoid models.
100
65
  email:
101
66
  - lukas.westermann@gmail.com
@@ -139,39 +104,29 @@ files:
139
104
  homepage: http://lwe.github.com/simple_enum/
140
105
  licenses:
141
106
  - MIT
107
+ metadata: {}
108
+
142
109
  post_install_message:
143
110
  rdoc_options: []
144
111
 
145
112
  require_paths:
146
113
  - lib
147
114
  required_ruby_version: !ruby/object:Gem::Requirement
148
- none: false
149
115
  requirements:
150
116
  - - ">="
151
117
  - !ruby/object:Gem::Version
152
- hash: 57
153
- segments:
154
- - 1
155
- - 8
156
- - 7
157
118
  version: 1.8.7
158
119
  required_rubygems_version: !ruby/object:Gem::Requirement
159
- none: false
160
120
  requirements:
161
121
  - - ">="
162
122
  - !ruby/object:Gem::Version
163
- hash: 23
164
- segments:
165
- - 1
166
- - 3
167
- - 6
168
123
  version: 1.3.6
169
124
  requirements: []
170
125
 
171
126
  rubyforge_project:
172
- rubygems_version: 1.8.24
127
+ rubygems_version: 2.0.2
173
128
  signing_key:
174
- specification_version: 3
129
+ specification_version: 4
175
130
  summary: Simple enum-like field support for models.
176
131
  test_files:
177
132
  - test/array_conversions_test.rb