validation-sets 1.0.0 → 1.1.0
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 +2 -0
- data/README.rdoc +59 -0
- data/VERSION +1 -1
- data/lib/validation_sets/validation_set.rb +6 -0
- data/lib/validation_sets.rb +37 -5
- data/test/validation_sets_test.rb +7 -0
- data/validation-sets.gemspec +6 -5
- metadata +18 -5
data/.gitignore
ADDED
data/README.rdoc
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
= Validation Sets
|
2
|
+
|
3
|
+
A Rails plugin to bundle validations in sets. You can turn entire sets of validations on or off on
|
4
|
+
an instance. This allows you to use different sets of validations for various user roles in the
|
5
|
+
application, or for different stages in the lifetime of the model.
|
6
|
+
|
7
|
+
validation_set_for(:activation) do |set|
|
8
|
+
set.validates_presence_of :fullname, :username, :email
|
9
|
+
set.validate :password_should_fit_requirements
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
== The Case of the Organization
|
14
|
+
|
15
|
+
Let's assume we have an organization in our application. The organization represents a company
|
16
|
+
using the application. Administrators add these organizations, but often they don't have all the
|
17
|
+
information about the company yet. It's up to the contact at the organization to complete it.
|
18
|
+
|
19
|
+
class Organization < ActiveRecord::Base
|
20
|
+
validates_presence_of :name
|
21
|
+
|
22
|
+
validation_set_for(:contact) do |set|
|
23
|
+
set.validates_presence_of :address, :zipcode, :city
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
Now we can have two controllers, one of the administrator and one for the contact. For the
|
28
|
+
administrator we don't run any validations except on name so she can choose to fill out any
|
29
|
+
of the field.
|
30
|
+
|
31
|
+
class Administrator::OrganizationsController < ActionController::Base
|
32
|
+
allow_access :administrator
|
33
|
+
|
34
|
+
def create
|
35
|
+
@organization = Organization.new(params[:organization])
|
36
|
+
if @organization.save
|
37
|
+
redirect_to [:administrator, @organization]
|
38
|
+
else
|
39
|
+
render :new
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
For the contact we turn on the extra set of validations for the contact so all the fields
|
45
|
+
need to be filled out.
|
46
|
+
|
47
|
+
class Contact::OrganizationsController < ActionController::Base
|
48
|
+
allow_access(:contact) { @organization = @authenticated.organization }
|
49
|
+
|
50
|
+
def update
|
51
|
+
@organization.attributes = params[:organization].slice(:address, :zipcode, :city)
|
52
|
+
@organization.use_validation_set(:contact)
|
53
|
+
if @organization.save
|
54
|
+
redirect_to [:contact, @organization]
|
55
|
+
else
|
56
|
+
render :edit
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.1.0
|
@@ -1,22 +1,28 @@
|
|
1
1
|
module ValidationSets
|
2
|
+
# A ValidationSet instance is used to redirect the original validation methods (validate,
|
3
|
+
# validate_on_create, and validate_on_update) to the callback chain for the set.
|
2
4
|
class ValidationSet
|
3
5
|
def initialize(model, label)
|
4
6
|
@model = model
|
5
7
|
@label = label
|
6
8
|
end
|
7
9
|
|
10
|
+
# Adds a validation method or proc that always runs
|
8
11
|
def validate(*params, &block)
|
9
12
|
send(validation_set_method(:save, @label), *params, &block)
|
10
13
|
end
|
11
14
|
|
15
|
+
# Adds a validation method or proc that runs on create
|
12
16
|
def validate_on_create(*params, &block)
|
13
17
|
send(validation_set_method(:create, @label), *params, &block)
|
14
18
|
end
|
15
19
|
|
20
|
+
# Adds a validation method or proc that runs on update
|
16
21
|
def validate_on_update(*params, &block)
|
17
22
|
send(validation_set_method(:update, @label), *params, &block)
|
18
23
|
end
|
19
24
|
|
25
|
+
# Forwards all other methods (ie. validates_presence_of) to the model class.
|
20
26
|
def method_missing(method, *attributes, &block)
|
21
27
|
@model.send(method, *attributes, &block)
|
22
28
|
end
|
data/lib/validation_sets.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
module ValidationSets
|
2
2
|
autoload :ValidationSet, 'validation_sets/validation_set'
|
3
3
|
|
4
|
+
# Used to set the current validation set during definition of the model
|
4
5
|
attr_accessor :current_validation_set
|
5
6
|
|
7
|
+
# Returns the name of the validation callback method for a save method and the label of a
|
8
|
+
# validation set.
|
6
9
|
def validation_set_method(on, label)
|
7
10
|
case on
|
8
11
|
when :save then "validate_#{label}_set".to_sym
|
@@ -11,6 +14,7 @@ module ValidationSets
|
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
17
|
+
# Returns the name of the validation callback method for a save method.
|
14
18
|
def validation_method(on)
|
15
19
|
if current_validation_set
|
16
20
|
validation_set_method(on, current_validation_set)
|
@@ -19,6 +23,20 @@ module ValidationSets
|
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
26
|
+
# Returns true if the validation set is defined on this class
|
27
|
+
def validation_set_defined?(set)
|
28
|
+
[:save, :create, :update].any? do |on|
|
29
|
+
respond_to?(validation_set_method(on, set))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Add a validation set to the model.
|
34
|
+
#
|
35
|
+
# class Account < ActiveRecord::Base
|
36
|
+
# validation_set_for(:activation) do |set|
|
37
|
+
# set.validates_presence_of :username
|
38
|
+
# end
|
39
|
+
# end
|
22
40
|
def validation_set_for(label, &block)
|
23
41
|
[:save, :create, :update].each do |save_method|
|
24
42
|
callback_chain = validation_method(save_method)
|
@@ -41,15 +59,29 @@ module ValidationSets
|
|
41
59
|
module InstanceMethods
|
42
60
|
attr_reader :_validation_set
|
43
61
|
|
62
|
+
# Set the active validation set on the model. After calling this both the global as well as
|
63
|
+
# the validations defined in the set will run.
|
64
|
+
#
|
65
|
+
# Raises an exception when asked to use an unknown validation set.
|
66
|
+
#
|
67
|
+
# Example:
|
68
|
+
#
|
69
|
+
# account = Account.new
|
70
|
+
# account.use_validation_set(:admin) # Turns on the :admin validation set
|
71
|
+
# account.use_validation_set(nil) # Turns off all validation sets
|
44
72
|
def use_validation_set(set)
|
45
|
-
|
73
|
+
if self.class.validation_set_defined?(set)
|
74
|
+
@_validation_set = set
|
75
|
+
else
|
76
|
+
raise ArgumentError, "There is no validation set `#{set}'"
|
77
|
+
end
|
46
78
|
end
|
47
79
|
end
|
48
80
|
end
|
49
81
|
|
50
|
-
|
51
|
-
class
|
52
|
-
|
82
|
+
module ActiveRecord #:nodoc:
|
83
|
+
class Base #:nodoc:
|
84
|
+
extend ValidationSets
|
85
|
+
include ValidationSets::InstanceMethods
|
53
86
|
end
|
54
|
-
include ValidationSets::InstanceMethods
|
55
87
|
end
|
@@ -88,6 +88,13 @@ class ValidationSetsTest < ActiveSupport::TestCase
|
|
88
88
|
assert account.save
|
89
89
|
end
|
90
90
|
|
91
|
+
test "an exception is raised when trying to activate an unknown validation set" do
|
92
|
+
account = Account.new
|
93
|
+
assert_raises(ArgumentError) do
|
94
|
+
account.use_validation_set(:unknown)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
91
98
|
def setup
|
92
99
|
ValidationSetsTests::Initializer.setup_database
|
93
100
|
end
|
data/validation-sets.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{validation-sets}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Manfred Stienstra"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-08-05}
|
13
13
|
s.description = %q{A Rails plugin that adds validation sets to Active Record.}
|
14
14
|
s.email = %q{manfred@fngtps.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,7 +17,8 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
".
|
20
|
+
".gitignore",
|
21
|
+
".kick",
|
21
22
|
"LICENSE",
|
22
23
|
"README.rdoc",
|
23
24
|
"Rakefile",
|
@@ -33,7 +34,7 @@ Gem::Specification.new do |s|
|
|
33
34
|
s.homepage = %q{http://fingertips.github.com}
|
34
35
|
s.rdoc_options = ["--charset=UTF-8"]
|
35
36
|
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version = %q{1.3.
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
37
38
|
s.summary = %q{A Rails plugin that adds validation sets to Active Record.}
|
38
39
|
s.test_files = [
|
39
40
|
"test/test_helper.rb",
|
@@ -45,7 +46,7 @@ Gem::Specification.new do |s|
|
|
45
46
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
47
|
s.specification_version = 3
|
47
48
|
|
48
|
-
if Gem::Version.new(Gem::
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
50
|
else
|
50
51
|
end
|
51
52
|
else
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validation-sets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Manfred Stienstra
|
@@ -9,7 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-
|
18
|
+
date: 2010-08-05 00:00:00 +02:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -23,6 +29,7 @@ extra_rdoc_files:
|
|
23
29
|
- LICENSE
|
24
30
|
- README.rdoc
|
25
31
|
files:
|
32
|
+
- .gitignore
|
26
33
|
- .kick
|
27
34
|
- LICENSE
|
28
35
|
- README.rdoc
|
@@ -45,21 +52,27 @@ rdoc_options:
|
|
45
52
|
require_paths:
|
46
53
|
- lib
|
47
54
|
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
48
56
|
requirements:
|
49
57
|
- - ">="
|
50
58
|
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
51
62
|
version: "0"
|
52
|
-
version:
|
53
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
54
65
|
requirements:
|
55
66
|
- - ">="
|
56
67
|
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
57
71
|
version: "0"
|
58
|
-
version:
|
59
72
|
requirements: []
|
60
73
|
|
61
74
|
rubyforge_project:
|
62
|
-
rubygems_version: 1.3.
|
75
|
+
rubygems_version: 1.3.7
|
63
76
|
signing_key:
|
64
77
|
specification_version: 3
|
65
78
|
summary: A Rails plugin that adds validation sets to Active Record.
|