validation-scopes 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +6 -1
- data/lib/validation_scopes.rb +30 -15
- data/spec/spec_helper.rb +1 -4
- data/spec/validation_scopes_spec.rb +39 -2
- data/validation-scopes.gemspec +2 -2
- metadata +3 -3
data/README.md
CHANGED
@@ -4,13 +4,18 @@ Validation Scopes allows you to group validations together that share the same c
|
|
4
4
|
|
5
5
|
class Car < ActiveRecord::Base
|
6
6
|
validation_scope :if => Proc.new { |u| u.step == 2 } do
|
7
|
-
# All validations here get their options merged with the options passed in above
|
8
7
|
validates_presence_of :variant
|
9
8
|
validates_presence_of :body
|
10
9
|
end
|
11
10
|
|
12
11
|
validation_scope :if => Proc.new { |u| i.step == 3 } do
|
13
12
|
validates_inclusion_of :outstanding_finance, :in => [true, false], :if => Proc.new { |u| u.finance == true }
|
13
|
+
# Duplicate keys are turned into arrays
|
14
|
+
# In this case InclusionValidator would get an array containing both Procs in the :if attribute
|
15
|
+
|
16
|
+
validate do
|
17
|
+
errors.add(:weight, "Must be greater than 0") unless !@weight.nil? && @weight > 0
|
18
|
+
end
|
14
19
|
end
|
15
20
|
end
|
16
21
|
|
data/lib/validation_scopes.rb
CHANGED
@@ -12,27 +12,42 @@ module ValidationScopes
|
|
12
12
|
block.call
|
13
13
|
@_validation_scope_options = nil
|
14
14
|
@_in_validation_scope = false
|
15
|
+
@_handled_by_with = false
|
15
16
|
end
|
16
17
|
|
17
|
-
def
|
18
|
+
def validates_with(*args, &block)
|
18
19
|
if @_in_validation_scope
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
20
|
+
merge_args(args)
|
21
|
+
@_handled_by_with = true
|
22
|
+
end
|
23
|
+
super(*args, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate(*args, &block)
|
27
|
+
if @_in_validation_scope & !@_handled_by_with
|
28
|
+
merge_args(args)
|
29
|
+
end
|
30
|
+
super(*args, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def merge_args(args)
|
36
|
+
if args.empty?
|
37
|
+
args << @_validation_scope_options.dup
|
38
|
+
elsif args.last.is_a?(Hash) && args.last.extractable_options?
|
39
|
+
options = args.extract_options!
|
40
|
+
options = options.dup
|
41
|
+
@_validation_scope_options.each_key do |key|
|
42
|
+
if options[key].nil?
|
43
|
+
options[key] = @_validation_scope_options[key]
|
44
|
+
else
|
45
|
+
options[key] = Array.wrap(options[key])
|
46
|
+
options[key] << @_validation_scope_options[key]
|
31
47
|
end
|
32
|
-
args << options
|
33
48
|
end
|
49
|
+
args << options
|
34
50
|
end
|
35
|
-
super(*args, &block)
|
36
51
|
end
|
37
52
|
end
|
38
53
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -15,6 +15,19 @@ class TestUser
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
class TestValidator < ActiveModel::EachValidator
|
19
|
+
attr_accessor :options
|
20
|
+
|
21
|
+
def initialize(options)
|
22
|
+
@options = options
|
23
|
+
super(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate(record)
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
18
31
|
describe ".validation_scope" do
|
19
32
|
|
20
33
|
context "validates_with" do
|
@@ -45,6 +58,7 @@ describe ".validation_scope" do
|
|
45
58
|
it "only validates eye colour when on step 3 and age is above 20" do
|
46
59
|
@user.eye_colour = nil
|
47
60
|
@user.should be_valid
|
61
|
+
@user.age = 20
|
48
62
|
@user.step = 3
|
49
63
|
@user.eye_colour = "red"
|
50
64
|
@user.should be_valid
|
@@ -54,7 +68,10 @@ describe ".validation_scope" do
|
|
54
68
|
@user.should be_valid
|
55
69
|
end
|
56
70
|
|
57
|
-
|
71
|
+
it "calls validates_with with merged scope options" do
|
72
|
+
presence_validator = User.validators_on(:name).first
|
73
|
+
presence_validator.options.should eq({:if => :step_2?})
|
74
|
+
end
|
58
75
|
end
|
59
76
|
|
60
77
|
context "validate method" do
|
@@ -79,7 +96,27 @@ describe ".validation_scope" do
|
|
79
96
|
user.weight = 0
|
80
97
|
user.should be_valid
|
81
98
|
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "custom validators" do
|
102
|
+
before do
|
103
|
+
User = Class.new(TestUser) do
|
104
|
+
validation_scope :unless => :step_2?, :some_config_var => 5 do
|
105
|
+
validates_with TestValidator, {:attributes => [:name], :some_config_var => 6}
|
106
|
+
end
|
107
|
+
end
|
108
|
+
@validator = User.validators_on(:name).first
|
109
|
+
end
|
82
110
|
|
83
|
-
|
111
|
+
it "passes the options in" do
|
112
|
+
@validator.options[:unless].should eq(:step_2?)
|
113
|
+
@validator.options[:some_config_var].should be
|
114
|
+
end
|
115
|
+
|
116
|
+
it "turns the duplicate options into an array" do
|
117
|
+
@validator.options[:some_config_var].should eq([6, 5])
|
118
|
+
end
|
84
119
|
end
|
120
|
+
|
121
|
+
after { Object.send(:remove_const, :User) if defined?(User) }
|
85
122
|
end
|
data/validation-scopes.gemspec
CHANGED
@@ -3,11 +3,11 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "validation-scopes"
|
6
|
-
s.version = "0.0.
|
6
|
+
s.version = "0.0.3"
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["Steve Hodgkiss"]
|
9
9
|
s.email = ["steve@hodgkiss.me.uk"]
|
10
|
-
s.homepage = ""
|
10
|
+
s.homepage = "https://github.com/stevehodgkiss/validation-scopes"
|
11
11
|
s.summary = %q{Scope ActiveModel validations}
|
12
12
|
s.description = %q{Scope ActiveModel validations}
|
13
13
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Steve Hodgkiss
|
@@ -54,7 +54,7 @@ files:
|
|
54
54
|
- specs.watchr
|
55
55
|
- validation-scopes.gemspec
|
56
56
|
has_rdoc: true
|
57
|
-
homepage:
|
57
|
+
homepage: https://github.com/stevehodgkiss/validation-scopes
|
58
58
|
licenses: []
|
59
59
|
|
60
60
|
post_install_message:
|