jnunemaker-validatable 1.1.10 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/README.rdoc +6 -4
  2. data/Rakefile +42 -17
  3. data/VERSION.yml +2 -4
  4. data/lib/{validatable/child_validation.rb → child_validation.rb} +0 -0
  5. data/lib/{validatable/errors.rb → errors.rb} +0 -2
  6. data/lib/{validatable/included_validation.rb → included_validation.rb} +0 -0
  7. data/lib/{validatable/macros.rb → macros.rb} +1 -32
  8. data/lib/{validatable/object_extension.rb → object_extension.rb} +0 -0
  9. data/lib/{validatable/requireable.rb → requireable.rb} +0 -0
  10. data/lib/{validatable/understandable.rb → understandable.rb} +0 -0
  11. data/lib/validatable.rb +20 -26
  12. data/lib/{validatable/validatable_class_methods.rb → validatable_class_methods.rb} +11 -13
  13. data/lib/{validatable/validatable_instance_methods.rb → validatable_instance_methods.rb} +3 -3
  14. data/lib/{validatable/validations → validations}/validates_acceptance_of.rb +0 -0
  15. data/lib/{validatable/validations → validations}/validates_confirmation_of.rb +0 -0
  16. data/lib/{validatable/validations → validations}/validates_each.rb +0 -0
  17. data/lib/{validatable/validations → validations}/validates_format_of.rb +0 -0
  18. data/lib/{validatable/validations → validations}/validates_length_of.rb +0 -0
  19. data/lib/{validatable/validations → validations}/validates_numericality_of.rb +0 -0
  20. data/lib/{validatable/validations → validations}/validates_presence_of.rb +0 -0
  21. data/lib/{validatable/validations → validations}/validates_true_for.rb +0 -0
  22. data/lib/{validatable/validations → validations}/validation_base.rb +1 -6
  23. data/test/all_tests.rb +1 -0
  24. data/test/functional/test_validatable.rb +0 -82
  25. data/test/test_helper.rb +2 -1
  26. data/test/unit/test_errors.rb +1 -7
  27. metadata +25 -40
  28. data/lib/validatable/validations/validates_associated.rb +0 -13
  29. data/lib/validatable/validations/validates_exclusion_of.rb +0 -17
  30. data/lib/validatable/validations/validates_inclusion_of.rb +0 -17
  31. data/test/functional/test_validates_associated.rb +0 -41
  32. data/test/functional/test_validates_exclusion_of.rb +0 -29
  33. data/test/functional/test_validates_inclusion_of.rb +0 -29
  34. data/test/unit/test_validates_associated.rb +0 -29
  35. data/test/unit/test_validates_exclusion_of.rb +0 -23
  36. data/test/unit/test_validates_inclusion_of.rb +0 -23
data/README.rdoc CHANGED
@@ -2,11 +2,13 @@
2
2
 
3
3
  Validatable is a library for adding validations.
4
4
 
5
- Created by Jay Fields, updated by John Nunemaker
5
+ by Jay[http://jayfields.blogspot.com] Fields[http://jayfields.blogspot.com]
6
6
 
7
- == Installation
8
-
9
- $ gem install jnunemaker-validatable -s http://gemcutter.org
7
+ == Download and Installation
8
+
9
+ You can download Validatable from here[http://rubyforge.org/projects/validatable] or install it with the following command.
10
+
11
+ $ gem install validatable
10
12
 
11
13
  == License
12
14
 
data/Rakefile CHANGED
@@ -1,29 +1,54 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
3
 
4
- require File.dirname(__FILE__) + '/lib/validatable'
5
-
6
- require 'jeweler'
7
- Jeweler::Tasks.new do |gem|
8
- gem.name = "jnunemaker-validatable"
9
- gem.summary = %Q{Validatable is a library for adding validations.}
10
- gem.description = %Q{Validatable is a library for adding validations.}
11
- gem.email = "nunemaker@gmail.com"
12
- gem.homepage = "http://github.com/jnunemaker/validatable"
13
- gem.authors = ['Jay Fields', 'John Nunemaker']
14
- gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
15
- gem.version = Validatable::Version
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "validatable"
8
+ gem.summary = %Q{Validatable is a library for adding validations.}
9
+ gem.email = "nunemaker@gmail.com"
10
+ gem.homepage = "http://github.com/jnunemaker/validatable"
11
+ gem.authors = ['Jay Fields', 'John Nunemaker']
12
+ gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
16
17
  end
17
18
 
18
- Jeweler::GemcutterTasks.new
19
-
20
19
  require 'rake/testtask'
21
20
  Rake::TestTask.new(:test) do |test|
22
- test.libs << 'test'
23
- test.ruby_opts << '-rubygems'
21
+ test.libs << 'lib' << 'test'
24
22
  test.pattern = 'test/**/test_*.rb'
25
23
  test.verbose = true
26
24
  end
27
25
 
28
- task :test => :check_dependencies
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
29
39
  task :default => :test
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ if File.exist?('VERSION.yml')
44
+ config = YAML.load(File.read('VERSION.yml'))
45
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
46
+ else
47
+ version = ""
48
+ end
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "validatable #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION.yml CHANGED
@@ -1,5 +1,3 @@
1
- ---
2
- :minor: 8
3
- :patch: 1
4
1
  :major: 1
5
- :build:
2
+ :minor: 7
3
+ :patch: 0
@@ -28,8 +28,6 @@ module Validatable
28
28
  return nil if errors[attribute.to_sym].nil?
29
29
  errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
30
30
  end
31
-
32
- alias [] on
33
31
 
34
32
  def add(attribute, message) #:nodoc:
35
33
  errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
@@ -194,37 +194,6 @@ module Validatable
194
194
  add_validations(args, ValidatesTrueFor)
195
195
  end
196
196
 
197
- def validates_exclusion_of(*args)
198
- add_validations(args, ValidatesExclusionOf)
199
- end
200
-
201
- def validates_inclusion_of(*args)
202
- add_validations(args, ValidatesInclusionOf)
203
- end
204
-
205
- # call-seq: validates_associated(*args)
206
- #
207
- # Checks the validity of an associated object or objects and adds a single
208
- # error if validation fails.
209
- #
210
- # class Person
211
- # include Validatable
212
- # attr_accessor :addresses
213
- # validates_associated :addresses
214
- # end
215
- #
216
- # Configuration options:
217
- #
218
- # * after_validate - A block that executes following the run of a validation
219
- # * message - The message to add to the errors collection when the validation fails
220
- # * times - The number of times the validation applies
221
- # * level - The level at which the validation should occur
222
- # * if - A block that when executed must return true of the validation will not occur
223
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
224
- def validates_associated(*args)
225
- add_validations(args, ValidatesAssociated)
226
- end
227
-
228
197
  # call-seq: include_validations_from(attribute)
229
198
  #
230
199
  # Includes all the validations that are defined on the attribute.
@@ -311,4 +280,4 @@ module Validatable
311
280
  before_validations << block
312
281
  end
313
282
  end
314
- end
283
+ end
File without changes
data/lib/validatable.rb CHANGED
@@ -1,28 +1,22 @@
1
1
  require 'forwardable'
2
- require 'active_support/all'
2
+ require 'rubygems'
3
+ require 'activesupport'
3
4
 
4
- require 'validatable/object_extension'
5
- require 'validatable/errors'
6
- require 'validatable/validatable_class_methods'
7
- require 'validatable/macros'
8
- require 'validatable/validatable_instance_methods'
9
- require 'validatable/included_validation'
10
- require 'validatable/child_validation'
11
- require 'validatable/understandable'
12
- require 'validatable/requireable'
13
- require 'validatable/validations/validation_base'
14
- require 'validatable/validations/validates_format_of'
15
- require 'validatable/validations/validates_presence_of'
16
- require 'validatable/validations/validates_acceptance_of'
17
- require 'validatable/validations/validates_confirmation_of'
18
- require 'validatable/validations/validates_length_of'
19
- require 'validatable/validations/validates_true_for'
20
- require 'validatable/validations/validates_numericality_of'
21
- require 'validatable/validations/validates_exclusion_of'
22
- require 'validatable/validations/validates_inclusion_of'
23
- require 'validatable/validations/validates_each'
24
- require 'validatable/validations/validates_associated'
25
-
26
- module Validatable
27
- Version = '1.1.10'
28
- end
5
+ require File.expand_path(File.dirname(__FILE__) + '/object_extension')
6
+ require File.expand_path(File.dirname(__FILE__) + '/errors')
7
+ require File.expand_path(File.dirname(__FILE__) + '/validatable_class_methods')
8
+ require File.expand_path(File.dirname(__FILE__) + '/macros')
9
+ require File.expand_path(File.dirname(__FILE__) + '/validatable_instance_methods')
10
+ require File.expand_path(File.dirname(__FILE__) + '/included_validation')
11
+ require File.expand_path(File.dirname(__FILE__) + '/child_validation')
12
+ require File.expand_path(File.dirname(__FILE__) + '/understandable')
13
+ require File.expand_path(File.dirname(__FILE__) + '/requireable')
14
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validation_base')
15
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_format_of')
16
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_presence_of')
17
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_acceptance_of')
18
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_confirmation_of')
19
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_length_of')
20
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_true_for')
21
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_numericality_of')
22
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_each')
@@ -4,20 +4,18 @@ module Validatable
4
4
  def validate_children(instance, group)
5
5
  self.children_to_validate.each do |child_validation|
6
6
  next unless child_validation.should_validate?(instance)
7
- child_or_children = instance.send child_validation.attribute
8
- [child_or_children].flatten.each do |child|
9
- if (child.respond_to?(:valid_for_group?))
10
- child.valid_for_group?(group)
7
+ child = instance.send child_validation.attribute
8
+ if (child.respond_to?(:valid_for_group?))
9
+ child.valid_for_group?(group)
10
+ else
11
+ child.valid?
12
+ end
13
+ child.errors.each do |attribute, messages|
14
+ if messages.is_a?(String)
15
+ add_error(instance, child_validation.map[attribute.to_sym] || attribute, messages)
11
16
  else
12
- child.valid?
13
- end
14
- child.errors.each do |attribute, messages|
15
- if messages.is_a?(String)
16
- add_error(instance, child_validation.map[attribute.to_sym] || attribute, messages)
17
- else
18
- messages.each do |message|
19
- add_error(instance, child_validation.map[attribute.to_sym] || attribute, message)
20
- end
17
+ messages.each do |message|
18
+ add_error(instance, child_validation.map[attribute.to_sym] || attribute, message)
21
19
  end
22
20
  end
23
21
  end
@@ -19,10 +19,10 @@ module Validatable
19
19
  end
20
20
 
21
21
  def valid_for_group?(group) #:nodoc:
22
- errors.clear
23
22
  run_before_validations
23
+ errors.clear
24
24
  self.class.validate_children(self, group)
25
- self.validate_group(group)
25
+ self.validate(group)
26
26
  errors.empty?
27
27
  end
28
28
 
@@ -61,7 +61,7 @@ module Validatable
61
61
  @times_validated_hash ||= {}
62
62
  end
63
63
 
64
- def validate_group(group) #:nodoc:
64
+ def validate(group) #:nodoc:
65
65
  validation_levels.each do |level|
66
66
  validations_for_level_and_group(level, group).each do |validation|
67
67
  run_validation(validation) if validation.should_validate?(self)
@@ -63,12 +63,7 @@ module Validatable
63
63
 
64
64
  def should_validate?(instance)
65
65
  result = validate_this_time?(instance)
66
- case self.if
67
- when Proc
68
- result &&= instance.instance_eval(&self.if)
69
- when Symbol, String
70
- result &&= instance.instance_eval(self.if.to_s)
71
- end
66
+ result &&= instance.instance_eval(&self.if) unless self.if.nil?
72
67
  result
73
68
  end
74
69
 
data/test/all_tests.rb ADDED
@@ -0,0 +1 @@
1
+ Dir['test/**/test_*.rb'].each { |test_case| require test_case }
@@ -138,53 +138,7 @@ functional_tests do
138
138
  instance.valid?
139
139
  instance.errors.on(:address)
140
140
  end
141
-
142
- expect "is invalid" do
143
- child_class = Class.new do
144
- include Validatable
145
- attr_accessor :name, :address
146
- validates_presence_of :name
147
- validates_format_of :address, :with => /.+/
148
- end
149
- klass = Class.new do
150
- include Validatable
151
- include_errors_from :child
152
- define_method :child do
153
- valid_child = child_class.new
154
- valid_child.name = "nom de plume"
155
- valid_child.address = "nowhere"
156
- [valid_child, child_class.new]
157
- end
158
- end
159
- instance = klass.new
160
- instance.valid?
161
- instance.errors.on(:address)
162
- end
163
141
 
164
- expect true do
165
- child_class = Class.new do
166
- include Validatable
167
- attr_accessor :name, :address
168
- validates_presence_of :name
169
- validates_format_of :address, :with => /.+/
170
- end
171
- klass = Class.new do
172
- include Validatable
173
- include_errors_from :child
174
- define_method :child do
175
- valid_child = child_class.new
176
- valid_child.name = "nom de plume"
177
- valid_child.address = "nowhere"
178
- also_valid = child_class.new
179
- also_valid.name = "nom de guerre"
180
- also_valid.address = "somewhere else"
181
- [valid_child, valid_child]
182
- end
183
- end
184
- instance = klass.new
185
- instance.valid?
186
- end
187
-
188
142
  expect "can't be empty" do
189
143
  child_class = Class.new do
190
144
  include Validatable
@@ -301,42 +255,6 @@ functional_tests do
301
255
  instance = klass.new
302
256
  instance.valid?
303
257
  end
304
-
305
- test ':if with symbol should work' do
306
- klass = Class.new do
307
- include Validatable
308
- attr_accessor :name, :name_required
309
- validates_presence_of :name, :if => :name_required?
310
-
311
- def name_required?
312
- name_required
313
- end
314
- end
315
- instance = klass.new
316
- instance.name_required = false
317
- assert instance.valid?
318
- instance.name_required = true
319
- assert !instance.valid?
320
- assert_equal "can't be empty", instance.errors.on(:name)
321
- end
322
-
323
- test ':if with string should work' do
324
- klass = Class.new do
325
- include Validatable
326
- attr_accessor :name, :name_required
327
- validates_presence_of :name, :if => 'name_required?'
328
-
329
- def name_required?
330
- name_required
331
- end
332
- end
333
- instance = klass.new
334
- instance.name_required = false
335
- assert instance.valid?
336
- instance.name_required = true
337
- assert !instance.valid?
338
- assert_equal "can't be empty", instance.errors.on(:name)
339
- end
340
258
 
341
259
  test "classes only have valid_for_* methods for groups that appear in their validations" do
342
260
  class_with_group_one = Class.new do
data/test/test_helper.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  require 'test/unit'
2
+ require 'rubygems'
2
3
  require 'mocha'
3
4
  require 'dust'
4
5
  require 'set'
5
6
  require 'expectations'
6
7
 
7
- require File.expand_path(File.dirname(__FILE__) + '/../lib/validatable')
8
+ require File.dirname(__FILE__) + '/../lib/validatable'
8
9
 
9
10
  class << Test::Unit::TestCase
10
11
  def expect(expected_value, &block)
@@ -60,11 +60,5 @@ Expectations do
60
60
  errors.add(:attribute1, "message1")
61
61
  errors.add(:attribute2, "message2")
62
62
  errors.count
63
- end
64
-
65
- expect "message" do
66
- errors = Validatable::Errors.new
67
- errors.add(:attribute, "message")
68
- errors[:attribute]
69
- end
63
+ end
70
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jnunemaker-validatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.10
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay Fields
@@ -10,11 +10,11 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-02-28 00:00:00 -05:00
13
+ date: 2009-05-28 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
17
- description: Validatable is a library for adding validations.
17
+ description:
18
18
  email: nunemaker@gmail.com
19
19
  executables: []
20
20
 
@@ -26,36 +26,31 @@ files:
26
26
  - README.rdoc
27
27
  - Rakefile
28
28
  - VERSION.yml
29
+ - lib/child_validation.rb
30
+ - lib/errors.rb
31
+ - lib/included_validation.rb
32
+ - lib/macros.rb
33
+ - lib/object_extension.rb
34
+ - lib/requireable.rb
35
+ - lib/understandable.rb
29
36
  - lib/validatable.rb
30
- - lib/validatable/child_validation.rb
31
- - lib/validatable/errors.rb
32
- - lib/validatable/included_validation.rb
33
- - lib/validatable/macros.rb
34
- - lib/validatable/object_extension.rb
35
- - lib/validatable/requireable.rb
36
- - lib/validatable/understandable.rb
37
- - lib/validatable/validatable_class_methods.rb
38
- - lib/validatable/validatable_instance_methods.rb
39
- - lib/validatable/validations/validates_acceptance_of.rb
40
- - lib/validatable/validations/validates_associated.rb
41
- - lib/validatable/validations/validates_confirmation_of.rb
42
- - lib/validatable/validations/validates_each.rb
43
- - lib/validatable/validations/validates_exclusion_of.rb
44
- - lib/validatable/validations/validates_format_of.rb
45
- - lib/validatable/validations/validates_inclusion_of.rb
46
- - lib/validatable/validations/validates_length_of.rb
47
- - lib/validatable/validations/validates_numericality_of.rb
48
- - lib/validatable/validations/validates_presence_of.rb
49
- - lib/validatable/validations/validates_true_for.rb
50
- - lib/validatable/validations/validation_base.rb
37
+ - lib/validatable_class_methods.rb
38
+ - lib/validatable_instance_methods.rb
39
+ - lib/validations/validates_acceptance_of.rb
40
+ - lib/validations/validates_confirmation_of.rb
41
+ - lib/validations/validates_each.rb
42
+ - lib/validations/validates_format_of.rb
43
+ - lib/validations/validates_length_of.rb
44
+ - lib/validations/validates_numericality_of.rb
45
+ - lib/validations/validates_presence_of.rb
46
+ - lib/validations/validates_true_for.rb
47
+ - lib/validations/validation_base.rb
48
+ - test/all_tests.rb
51
49
  - test/functional/test_validatable.rb
52
50
  - test/functional/test_validates_acceptance_of.rb
53
- - test/functional/test_validates_associated.rb
54
51
  - test/functional/test_validates_confirmation_of.rb
55
52
  - test/functional/test_validates_each.rb
56
- - test/functional/test_validates_exclusion_of.rb
57
53
  - test/functional/test_validates_format_of.rb
58
- - test/functional/test_validates_inclusion_of.rb
59
54
  - test/functional/test_validates_length_of.rb
60
55
  - test/functional/test_validates_numericality_of.rb
61
56
  - test/functional/test_validates_presence_of.rb
@@ -65,11 +60,8 @@ files:
65
60
  - test/unit/test_understandable.rb
66
61
  - test/unit/test_validatable.rb
67
62
  - test/unit/test_validates_acceptance_of.rb
68
- - test/unit/test_validates_associated.rb
69
63
  - test/unit/test_validates_confirmation_of.rb
70
- - test/unit/test_validates_exclusion_of.rb
71
64
  - test/unit/test_validates_format_of.rb
72
- - test/unit/test_validates_inclusion_of.rb
73
65
  - test/unit/test_validates_length_of.rb
74
66
  - test/unit/test_validates_numericality_of.rb
75
67
  - test/unit/test_validates_presence_of.rb
@@ -77,8 +69,6 @@ files:
77
69
  - test/unit/test_validation_base.rb
78
70
  has_rdoc: true
79
71
  homepage: http://github.com/jnunemaker/validatable
80
- licenses: []
81
-
82
72
  post_install_message:
83
73
  rdoc_options:
84
74
  - --charset=UTF-8
@@ -99,19 +89,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
89
  requirements: []
100
90
 
101
91
  rubyforge_project:
102
- rubygems_version: 1.3.5
92
+ rubygems_version: 1.2.0
103
93
  signing_key:
104
- specification_version: 3
94
+ specification_version: 2
105
95
  summary: Validatable is a library for adding validations.
106
96
  test_files:
97
+ - test/all_tests.rb
107
98
  - test/functional/test_validatable.rb
108
99
  - test/functional/test_validates_acceptance_of.rb
109
- - test/functional/test_validates_associated.rb
110
100
  - test/functional/test_validates_confirmation_of.rb
111
101
  - test/functional/test_validates_each.rb
112
- - test/functional/test_validates_exclusion_of.rb
113
102
  - test/functional/test_validates_format_of.rb
114
- - test/functional/test_validates_inclusion_of.rb
115
103
  - test/functional/test_validates_length_of.rb
116
104
  - test/functional/test_validates_numericality_of.rb
117
105
  - test/functional/test_validates_presence_of.rb
@@ -121,11 +109,8 @@ test_files:
121
109
  - test/unit/test_understandable.rb
122
110
  - test/unit/test_validatable.rb
123
111
  - test/unit/test_validates_acceptance_of.rb
124
- - test/unit/test_validates_associated.rb
125
112
  - test/unit/test_validates_confirmation_of.rb
126
- - test/unit/test_validates_exclusion_of.rb
127
113
  - test/unit/test_validates_format_of.rb
128
- - test/unit/test_validates_inclusion_of.rb
129
114
  - test/unit/test_validates_length_of.rb
130
115
  - test/unit/test_validates_numericality_of.rb
131
116
  - test/unit/test_validates_presence_of.rb
@@ -1,13 +0,0 @@
1
- module Validatable
2
- class ValidatesAssociated < ValidationBase #:nodoc:
3
- def valid?(instance)
4
- Array(instance.send(attribute)).compact.map do |child|
5
- child.valid?
6
- end.all?
7
- end
8
-
9
- def message(instance)
10
- super || "is invalid"
11
- end
12
- end
13
- end
@@ -1,17 +0,0 @@
1
- module Validatable
2
- class ValidatesExclusionOf < ValidationBase #:nodoc:
3
- required_option :within
4
-
5
- def valid?(instance)
6
- value = instance.send(attribute)
7
- return true if allow_nil && value.nil?
8
- return true if allow_blank && value.blank?
9
-
10
- !within.include?(value)
11
- end
12
-
13
- def message(instance)
14
- super || "is reserved"
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- module Validatable
2
- class ValidatesInclusionOf < ValidationBase
3
- required_option :within
4
-
5
- def valid?(instance)
6
- value = instance.send(attribute)
7
- return true if allow_nil && value.nil?
8
- return true if allow_blank && value.blank?
9
-
10
- within.include?(value)
11
- end
12
-
13
- def message(instance)
14
- super || "is not in the list"
15
- end
16
- end
17
- end
@@ -1,41 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
-
3
- module Functional
4
- class ValidatesAssociatedTest < Test::Unit::TestCase
5
- def setup
6
- @parent = Class.new do
7
- include Validatable
8
- attr_accessor :children
9
- validates_associated :children
10
- end
11
- @child = Class.new do
12
- include Validatable
13
- attr_accessor :name
14
- validates_presence_of :name
15
- end
16
- end
17
-
18
- test "given invalid child, when validated, then error is in the parent's error collection" do
19
- instance = @parent.new
20
- instance.children = [@child.new]
21
- instance.valid?
22
- assert_equal "is invalid", instance.errors.on(:children)
23
- end
24
-
25
- test "given two invalid children, when validated, then both children have errors" do
26
- instance = @parent.new
27
- instance.children = [@child.new, @child.new]
28
- instance.valid?
29
- instance.children.each do |child|
30
- assert_not_nil child.errors.on(:name)
31
- end
32
- end
33
-
34
- test "given valid child, when validated, the parent is valid" do
35
- instance = @parent.new
36
- instance.children = [@child.new]
37
- instance.children.first.name = 'x'
38
- assert instance.valid?
39
- end
40
- end
41
- end
@@ -1,29 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
-
3
- module Functional
4
- class ValidatesExclusionOfTest < Test::Unit::TestCase
5
- test "given excluded state, when validated, then error is in the objects error collection" do
6
- klass = Class.new do
7
- include Validatable
8
- attr_accessor :state
9
- validates_exclusion_of :state, :within => %w(in out)
10
- end
11
- instance = klass.new
12
- instance.state = 'in'
13
- instance.valid?
14
- assert_equal "is reserved", instance.errors.on(:state)
15
- end
16
-
17
- test "given non-excluded state, when validated, then no error is in the objects error collection" do
18
- klass = Class.new do
19
- include Validatable
20
- attr_accessor :state
21
- validates_exclusion_of :state, :within => %w(in out)
22
- end
23
- instance = klass.new
24
- instance.state = 'foo'
25
- instance.valid?
26
- assert_nil instance.errors.on(:state)
27
- end
28
- end
29
- end
@@ -1,29 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
-
3
- module Functional
4
- class ValidatesInclusionOfTest < Test::Unit::TestCase
5
- test "given state not in list, when validated, then error is in the objects error collection" do
6
- klass = Class.new do
7
- include Validatable
8
- attr_accessor :state
9
- validates_inclusion_of :state, :within => %w(in out)
10
- end
11
- instance = klass.new
12
- instance.state = 'foo'
13
- instance.valid?
14
- assert_equal "is not in the list", instance.errors.on(:state)
15
- end
16
-
17
- test "given state in list, when validated, then no error is in the objects error collection" do
18
- klass = Class.new do
19
- include Validatable
20
- attr_accessor :state
21
- validates_inclusion_of :state, :within => %w(in out)
22
- end
23
- instance = klass.new
24
- instance.state = 'in'
25
- instance.valid?
26
- assert_nil instance.errors.on(:state)
27
- end
28
- end
29
- end
@@ -1,29 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
-
3
- Expectations do
4
-
5
- expect false do
6
- parent = stub('parent', :child => stub(:valid? => false))
7
- validation = Validatable::ValidatesAssociated.new stub_everything, :child
8
- validation.valid?(parent)
9
- end
10
-
11
- expect true do
12
- parent = stub('parent', :child => stub(:valid? => true))
13
- validation = Validatable::ValidatesAssociated.new stub_everything, :child
14
- validation.valid?(parent)
15
- end
16
-
17
- expect false do
18
- parent = stub('parent', :children => [stub(:valid? => false)])
19
- validation = Validatable::ValidatesAssociated.new stub_everything, :children
20
- validation.valid?(parent)
21
- end
22
-
23
- expect true do
24
- parent = stub('parent', :children => [stub(:valid? => true)])
25
- validation = Validatable::ValidatesAssociated.new stub_everything, :children
26
- validation.valid?(parent)
27
- end
28
-
29
- end
@@ -1,23 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
-
3
- Expectations do
4
- expect false do
5
- validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out)
6
- validation.valid?(stub(:state => 'in'))
7
- end
8
-
9
- expect true do
10
- validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out)
11
- validation.valid?(stub(:state => 'foo'))
12
- end
13
-
14
- expect true do
15
- validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out), :allow_blank => true
16
- validation.valid?(stub(:state => ''))
17
- end
18
-
19
- expect true do
20
- validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out), :allow_nil => true
21
- validation.valid?(stub(:state => nil))
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
-
3
- Expectations do
4
- expect true do
5
- validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out)
6
- validation.valid?(stub(:state => 'in'))
7
- end
8
-
9
- expect false do
10
- validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out)
11
- validation.valid?(stub(:state => 'foo'))
12
- end
13
-
14
- expect true do
15
- validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out), :allow_blank => true
16
- validation.valid?(stub(:state => ''))
17
- end
18
-
19
- expect true do
20
- validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out), :allow_nil => true
21
- validation.valid?(stub(:state => nil))
22
- end
23
- end