validation-scopes 0.0.1
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/.gitignore +3 -0
- data/.rspec +1 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +32 -0
- data/LICENSE +20 -0
- data/README.md +23 -0
- data/Rakefile +7 -0
- data/lib/validation-scopes.rb +1 -0
- data/lib/validation_scopes.rb +40 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/validation_scopes_spec.rb +85 -0
- data/specs.watchr +64 -0
- data/validation-scopes.gemspec +22 -0
- metadata +90 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
validation-scopes (0.0.1)
|
5
|
+
activemodel
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.0.4)
|
11
|
+
activesupport (= 3.0.4)
|
12
|
+
builder (~> 2.1.2)
|
13
|
+
i18n (~> 0.4)
|
14
|
+
activesupport (3.0.4)
|
15
|
+
builder (2.1.2)
|
16
|
+
diff-lcs (1.1.2)
|
17
|
+
i18n (0.5.0)
|
18
|
+
rspec (2.5.0)
|
19
|
+
rspec-core (~> 2.5.0)
|
20
|
+
rspec-expectations (~> 2.5.0)
|
21
|
+
rspec-mocks (~> 2.5.0)
|
22
|
+
rspec-core (2.5.1)
|
23
|
+
rspec-expectations (2.5.0)
|
24
|
+
diff-lcs (~> 1.1.2)
|
25
|
+
rspec-mocks (2.5.0)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
rspec
|
32
|
+
validation-scopes!
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Steve Hodgkiss
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Validation Scopes
|
2
|
+
|
3
|
+
Validation Scopes allows you to group validations together that share the same conditions. It depends on ActiveModel. Example:
|
4
|
+
|
5
|
+
class Car < ActiveRecord::Base
|
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
|
+
validates_presence_of :variant
|
9
|
+
validates_presence_of :body
|
10
|
+
end
|
11
|
+
|
12
|
+
validation_scope :if => Proc.new { |u| i.step == 3 } do
|
13
|
+
validates_inclusion_of :outstanding_finance, :in => [true, false], :if => Proc.new { |u| u.finance == true }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Installation
|
18
|
+
|
19
|
+
Add the gem to your Gemfile
|
20
|
+
|
21
|
+
gem "validation-scopes"
|
22
|
+
|
23
|
+
It will be included into ActiveRecord::Base if it is defined, if not use `include ValidationScopes` on any ActiveModel object.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validation_scopes'
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
module ValidationScopes
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
def validation_scope(options, &block)
|
10
|
+
@_in_validation_scope = true
|
11
|
+
@_validation_scope_options = options
|
12
|
+
block.call
|
13
|
+
@_validation_scope_options = nil
|
14
|
+
@_in_validation_scope = false
|
15
|
+
end
|
16
|
+
|
17
|
+
def validate(*args, &block)
|
18
|
+
if @_in_validation_scope
|
19
|
+
if args.empty?
|
20
|
+
args = [@_validation_scope_options.dup]
|
21
|
+
elsif args.last.is_a?(Hash) && args.last.extractable_options?
|
22
|
+
options = args.extract_options!
|
23
|
+
options = options.dup
|
24
|
+
@_validation_scope_options.each_key do |key|
|
25
|
+
if options[key].nil?
|
26
|
+
options[key] = @_validation_scope_options[key]
|
27
|
+
else
|
28
|
+
options[key] = Array.wrap(options[key])
|
29
|
+
options[key] << @_validation_scope_options[key]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
args << options
|
33
|
+
end
|
34
|
+
end
|
35
|
+
super(*args, &block)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
ActiveRecord::Base.send(:include, ValidationScopes) if defined?(ActiveRecord)
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestUser
|
4
|
+
include ActiveModel::Validations
|
5
|
+
include ValidationScopes
|
6
|
+
|
7
|
+
attr_accessor :step
|
8
|
+
|
9
|
+
attr_accessor :name, :email, :address
|
10
|
+
attr_accessor :height, :weight
|
11
|
+
attr_accessor :eye_colour, :age
|
12
|
+
|
13
|
+
def step_2?
|
14
|
+
step == 2
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".validation_scope" do
|
19
|
+
|
20
|
+
context "validates_with" do
|
21
|
+
before do
|
22
|
+
User = Class.new(TestUser) do
|
23
|
+
validates_presence_of :address
|
24
|
+
validation_scope :if => :step_2? do
|
25
|
+
validates_presence_of :name
|
26
|
+
end
|
27
|
+
validation_scope :if => Proc.new { |u| u.step == 3 } do
|
28
|
+
validates_inclusion_of :eye_colour, :in => ["blue", "brown"], :if => Proc.new { |u| !u.age.nil? && u.age > 20 }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
@user = User.new
|
32
|
+
@user.errors[:address].should be
|
33
|
+
@user.address = "123 High St"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should only validate name when on step 2" do
|
37
|
+
@user.name = nil
|
38
|
+
@user.should be_valid
|
39
|
+
@user.step = 2
|
40
|
+
@user.should be_invalid
|
41
|
+
@user.name = "Steve"
|
42
|
+
@user.should be_valid
|
43
|
+
end
|
44
|
+
|
45
|
+
it "only validates eye colour when on step 3 and age is above 20" do
|
46
|
+
@user.eye_colour = nil
|
47
|
+
@user.should be_valid
|
48
|
+
@user.step = 3
|
49
|
+
@user.eye_colour = "red"
|
50
|
+
@user.should be_valid
|
51
|
+
@user.age = 21
|
52
|
+
@user.should be_invalid
|
53
|
+
@user.eye_colour = "blue"
|
54
|
+
@user.should be_valid
|
55
|
+
end
|
56
|
+
|
57
|
+
after { Object.send(:remove_const, :User) }
|
58
|
+
end
|
59
|
+
|
60
|
+
context "validate method" do
|
61
|
+
before do
|
62
|
+
User = Class.new(TestUser) do
|
63
|
+
validation_scope :unless => :step_2? do
|
64
|
+
validate do
|
65
|
+
errors.add(:weight, "Must be greater than 0") unless !@weight.nil? && @weight > 0
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should only validate weight on step 2" do
|
72
|
+
user = User.new
|
73
|
+
user.weight = 0
|
74
|
+
user.should be_invalid
|
75
|
+
user.weight = 1
|
76
|
+
user.should be_valid
|
77
|
+
|
78
|
+
user.step = 2
|
79
|
+
user.weight = 0
|
80
|
+
user.should be_valid
|
81
|
+
end
|
82
|
+
|
83
|
+
after { Object.send(:remove_const, :User) }
|
84
|
+
end
|
85
|
+
end
|
data/specs.watchr
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
ENV["WATCHR"] = "1"
|
2
|
+
system 'clear'
|
3
|
+
|
4
|
+
def growl(message)
|
5
|
+
growlnotify = `which growlnotify`.chomp
|
6
|
+
title = "Watchr Test Results"
|
7
|
+
image = message.include?('0 failures') ? "~/Dropbox/Scripts/watchr_example/rails_ok.png" : "~/Dropbox/Scripts/watchr_example/rails_fail.png"
|
8
|
+
options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
|
9
|
+
system %(#{growlnotify} #{options} &)
|
10
|
+
end
|
11
|
+
|
12
|
+
def run(cmd)
|
13
|
+
puts(cmd)
|
14
|
+
`#{cmd}`
|
15
|
+
end
|
16
|
+
|
17
|
+
def run_spec_file(file)
|
18
|
+
system('clear')
|
19
|
+
result = run(%Q(rspec #{file}))
|
20
|
+
growl result.split("\n").last rescue nil
|
21
|
+
puts result
|
22
|
+
end
|
23
|
+
|
24
|
+
def run_all_specs
|
25
|
+
system('clear')
|
26
|
+
result = run "rspec spec"
|
27
|
+
growl result.split("\n").last rescue nil
|
28
|
+
puts result
|
29
|
+
end
|
30
|
+
|
31
|
+
def related_spec_files(path)
|
32
|
+
puts path
|
33
|
+
Dir['spec/*.rb'].select { |file| file =~ /#{File.basename(path).split(".").first}_spec.rb/ }
|
34
|
+
end
|
35
|
+
|
36
|
+
def run_suite
|
37
|
+
run_all_specs
|
38
|
+
end
|
39
|
+
|
40
|
+
watch('spec/spec_helper\.rb') { run_all_specs }
|
41
|
+
watch('spec/.*_spec\.rb') { |m| run_spec_file(m[0]) }
|
42
|
+
watch('lib/.*\.rb') { |m| related_spec_files(m[0]).map {|tf| run_spec_file(tf) } }
|
43
|
+
|
44
|
+
# Ctrl-\
|
45
|
+
Signal.trap 'QUIT' do
|
46
|
+
puts " --- Running all tests ---\n\n"
|
47
|
+
run_all_tests
|
48
|
+
end
|
49
|
+
|
50
|
+
@interrupted = false
|
51
|
+
|
52
|
+
# Ctrl-C
|
53
|
+
Signal.trap 'INT' do
|
54
|
+
if @interrupted then
|
55
|
+
@wants_to_quit = true
|
56
|
+
abort("\n")
|
57
|
+
else
|
58
|
+
puts "Interrupt a second time to quit"
|
59
|
+
@interrupted = true
|
60
|
+
Kernel.sleep 1.5
|
61
|
+
# raise Interrupt, nil # let the run loop catch it
|
62
|
+
run_suite
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "validation-scopes"
|
6
|
+
s.version = "0.0.1"# Validation::Scopes::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Steve Hodgkiss"]
|
9
|
+
s.email = ["steve@hodgkiss.me.uk"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Scope ActiveModel validations}
|
12
|
+
s.description = %q{Scope ActiveModel validations}
|
13
|
+
|
14
|
+
s.rubyforge_project = "validation-scopes"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency("activemodel")
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validation-scopes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Steve Hodgkiss
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-02-14 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activemodel
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
description: Scope ActiveModel validations
|
34
|
+
email:
|
35
|
+
- steve@hodgkiss.me.uk
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- .rspec
|
45
|
+
- Gemfile
|
46
|
+
- Gemfile.lock
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/validation-scopes.rb
|
51
|
+
- lib/validation_scopes.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
- spec/validation_scopes_spec.rb
|
54
|
+
- specs.watchr
|
55
|
+
- validation-scopes.gemspec
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: ""
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: validation-scopes
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Scope ActiveModel validations
|
88
|
+
test_files:
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/validation_scopes_spec.rb
|