validation-sets 1.0.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/.kick +1 -0
- data/LICENSE +20 -0
- data/README.rdoc +0 -0
- data/Rakefile +35 -0
- data/VERSION +1 -0
- data/lib/validation_sets.rb +55 -0
- data/lib/validation_sets/validation_set.rb +24 -0
- data/rails/init.rb +1 -0
- data/test/test_helper.rb +64 -0
- data/test/validation_sets/validation_set_test.rb +9 -0
- data/test/validation_sets_test.rb +98 -0
- data/validation-sets.gemspec +54 -0
- metadata +69 -0
data/.kick
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
recipe :ruby
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Fingertips, Manfred Stienstra <manfred@fngtps.com>
|
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.rdoc
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Validation Sets'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README.rdoc')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'jeweler'
|
27
|
+
Jeweler::Tasks.new do |s|
|
28
|
+
s.name = "validation-sets"
|
29
|
+
s.summary = s.description = "A Rails plugin that adds validation sets to Active Record."
|
30
|
+
s.homepage = "http://fingertips.github.com"
|
31
|
+
s.email = "manfred@fngtps.com"
|
32
|
+
s.authors = ["Manfred Stienstra"]
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module ValidationSets
|
2
|
+
autoload :ValidationSet, 'validation_sets/validation_set'
|
3
|
+
|
4
|
+
attr_accessor :current_validation_set
|
5
|
+
|
6
|
+
def validation_set_method(on, label)
|
7
|
+
case on
|
8
|
+
when :save then "validate_#{label}_set".to_sym
|
9
|
+
when :create then "validate_on_create_#{label}_set".to_sym
|
10
|
+
when :update then "validate_on_update_#{label}_set".to_sym
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def validation_method(on)
|
15
|
+
if current_validation_set
|
16
|
+
validation_set_method(on, current_validation_set)
|
17
|
+
else
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def validation_set_for(label, &block)
|
23
|
+
[:save, :create, :update].each do |save_method|
|
24
|
+
callback_chain = validation_method(save_method)
|
25
|
+
callback_chain_for_set = validation_set_method(save_method, label)
|
26
|
+
unless respond_to?(callback_chain_for_set)
|
27
|
+
define_callbacks(callback_chain_for_set)
|
28
|
+
define_method(callback_chain_for_set) { run_callbacks(callback_chain_for_set) }
|
29
|
+
send(callback_chain, callback_chain_for_set, :if => Proc.new { |r| label.to_sym == r._validation_set })
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
validation_set = ValidationSet.new(self, label)
|
34
|
+
|
35
|
+
before = current_validation_set
|
36
|
+
self.current_validation_set = label.to_sym
|
37
|
+
block.call(validation_set)
|
38
|
+
self.current_validation_set = before
|
39
|
+
end
|
40
|
+
|
41
|
+
module InstanceMethods
|
42
|
+
attr_reader :_validation_set
|
43
|
+
|
44
|
+
def use_validation_set(set)
|
45
|
+
@_validation_set = set
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class ActiveRecord::Base
|
51
|
+
class << self
|
52
|
+
include ValidationSets
|
53
|
+
end
|
54
|
+
include ValidationSets::InstanceMethods
|
55
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ValidationSets
|
2
|
+
class ValidationSet
|
3
|
+
def initialize(model, label)
|
4
|
+
@model = model
|
5
|
+
@label = label
|
6
|
+
end
|
7
|
+
|
8
|
+
def validate(*params, &block)
|
9
|
+
send(validation_set_method(:save, @label), *params, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def validate_on_create(*params, &block)
|
13
|
+
send(validation_set_method(:create, @label), *params, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate_on_update(*params, &block)
|
17
|
+
send(validation_set_method(:update, @label), *params, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing(method, *attributes, &block)
|
21
|
+
@model.send(method, *attributes, &block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validation_sets'
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module ValidationSetsTests
|
2
|
+
module Initializer
|
3
|
+
VENDOR_RAILS = File.expand_path('../../../../../vendor/rails', __FILE__)
|
4
|
+
OTHER_RAILS = File.expand_path('../../../rails', __FILE__)
|
5
|
+
PLUGIN_ROOT = File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
def self.rails_directory
|
8
|
+
if File.exist?(VENDOR_RAILS)
|
9
|
+
VENDOR_RAILS
|
10
|
+
elsif File.exist?(OTHER_RAILS)
|
11
|
+
OTHER_RAILS
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.load_dependencies
|
16
|
+
if rails_directory
|
17
|
+
$:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
|
18
|
+
$:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
|
19
|
+
else
|
20
|
+
require 'rubygems' rescue LoadError
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'test/unit'
|
24
|
+
|
25
|
+
require 'active_support'
|
26
|
+
require 'active_support/test_case'
|
27
|
+
require 'active_record'
|
28
|
+
require 'active_record/test_case'
|
29
|
+
require 'active_record/base' # this is needed because of dependency hell
|
30
|
+
|
31
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
32
|
+
require File.join(PLUGIN_ROOT, 'rails', 'init')
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.configure_database
|
36
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
37
|
+
ActiveRecord::Migration.verbose = false
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.setup_database
|
41
|
+
ActiveRecord::Schema.define(:version => 1) do
|
42
|
+
create_table :accounts do |t|
|
43
|
+
t.column :fullname, :string
|
44
|
+
t.column :email, :string
|
45
|
+
t.column :username, :string
|
46
|
+
t.column :password, :string
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.teardown_database
|
52
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
53
|
+
ActiveRecord::Base.connection.drop_table(table)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.start
|
58
|
+
load_dependencies
|
59
|
+
configure_database
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
ValidationSetsTests::Initializer.start
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class ValidationSetTest < ActiveSupport::TestCase
|
4
|
+
test "forwards missing methods to the model" do
|
5
|
+
model = 'Model'
|
6
|
+
validation_set = ValidationSets::ValidationSet.new(model, :admin)
|
7
|
+
assert_equal 5, validation_set.length
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class Account < ActiveRecord::Base
|
4
|
+
attr_accessor :current_password
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def password_should_match_management_requirements
|
9
|
+
if password.blank?
|
10
|
+
errors.add(:password, "can't be blank")
|
11
|
+
elsif password !~ /\d/
|
12
|
+
errors.add(:password, "should contain at least one number")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def password_should_match_current
|
17
|
+
if send(:attribute_was, 'password') != current_password
|
18
|
+
errors.add(:current_password, "is wrong")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
validates_presence_of :fullname
|
23
|
+
|
24
|
+
# Admins create accounts with a blank username and password
|
25
|
+
validation_set_for(:admin) do |set|
|
26
|
+
set.validates_presence_of :email, :on => :create
|
27
|
+
end
|
28
|
+
|
29
|
+
# Members are force to choose a username and password during activation
|
30
|
+
validation_set_for(:activation) do |set|
|
31
|
+
set.validates_presence_of :email
|
32
|
+
set.validates_presence_of :username
|
33
|
+
set.validate :password_should_match_management_requirements
|
34
|
+
end
|
35
|
+
|
36
|
+
# Members need to set the current password when updating their account after activation
|
37
|
+
validation_set_for(:member) do |set|
|
38
|
+
set.validates_presence_of :email
|
39
|
+
set.validates_presence_of :username
|
40
|
+
set.validate :password_should_match_management_requirements
|
41
|
+
set.validate :password_should_match_current
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class ValidationSetsTest < ActiveSupport::TestCase
|
46
|
+
|
47
|
+
test "global validation should always run" do
|
48
|
+
account = Account.new
|
49
|
+
assert !account.valid?
|
50
|
+
assert account.errors.on(:fullname)
|
51
|
+
|
52
|
+
account.fullname = "Patricia Herder"
|
53
|
+
assert account.valid?
|
54
|
+
assert account.errors.empty?
|
55
|
+
end
|
56
|
+
|
57
|
+
test "a validation set runs when it's active" do
|
58
|
+
account = Account.new
|
59
|
+
account.use_validation_set(:activation)
|
60
|
+
assert !account.valid?
|
61
|
+
assert account.errors.on(:email)
|
62
|
+
assert account.errors.on(:username)
|
63
|
+
assert account.errors.on(:password)
|
64
|
+
|
65
|
+
account.fullname = "Patricia Herder"
|
66
|
+
account.email = 'patricia@example.com'
|
67
|
+
account.username = 'patricia'
|
68
|
+
account.password = 'secret1'
|
69
|
+
assert account.valid?
|
70
|
+
assert account.errors.empty?
|
71
|
+
end
|
72
|
+
|
73
|
+
test "a validation in a validation set runs a the correct time" do
|
74
|
+
account = Account.new
|
75
|
+
account.use_validation_set(:admin)
|
76
|
+
assert !account.valid?
|
77
|
+
assert account.errors.on(:fullname)
|
78
|
+
assert account.errors.on(:email)
|
79
|
+
|
80
|
+
account.fullname = "Patricia Herder"
|
81
|
+
account.email = 'patricia@example.com'
|
82
|
+
assert account.valid?
|
83
|
+
assert account.errors.empty?
|
84
|
+
|
85
|
+
assert account.save
|
86
|
+
|
87
|
+
account.email = nil
|
88
|
+
assert account.save
|
89
|
+
end
|
90
|
+
|
91
|
+
def setup
|
92
|
+
ValidationSetsTests::Initializer.setup_database
|
93
|
+
end
|
94
|
+
|
95
|
+
def teardown
|
96
|
+
ValidationSetsTests::Initializer.teardown_database
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{validation-sets}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Manfred Stienstra"]
|
12
|
+
s.date = %q{2010-03-26}
|
13
|
+
s.description = %q{A Rails plugin that adds validation sets to Active Record.}
|
14
|
+
s.email = %q{manfred@fngtps.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".kick",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"lib/validation_sets.rb",
|
26
|
+
"lib/validation_sets/validation_set.rb",
|
27
|
+
"rails/init.rb",
|
28
|
+
"test/test_helper.rb",
|
29
|
+
"test/validation_sets/validation_set_test.rb",
|
30
|
+
"test/validation_sets_test.rb",
|
31
|
+
"validation-sets.gemspec"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://fingertips.github.com}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.5}
|
37
|
+
s.summary = %q{A Rails plugin that adds validation sets to Active Record.}
|
38
|
+
s.test_files = [
|
39
|
+
"test/test_helper.rb",
|
40
|
+
"test/validation_sets/validation_set_test.rb",
|
41
|
+
"test/validation_sets_test.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
49
|
+
else
|
50
|
+
end
|
51
|
+
else
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validation-sets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Manfred Stienstra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-26 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A Rails plugin that adds validation sets to Active Record.
|
17
|
+
email: manfred@fngtps.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .kick
|
27
|
+
- LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- lib/validation_sets.rb
|
32
|
+
- lib/validation_sets/validation_set.rb
|
33
|
+
- rails/init.rb
|
34
|
+
- test/test_helper.rb
|
35
|
+
- test/validation_sets/validation_set_test.rb
|
36
|
+
- test/validation_sets_test.rb
|
37
|
+
- validation-sets.gemspec
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://fingertips.github.com
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: A Rails plugin that adds validation sets to Active Record.
|
66
|
+
test_files:
|
67
|
+
- test/test_helper.rb
|
68
|
+
- test/validation_sets/validation_set_test.rb
|
69
|
+
- test/validation_sets_test.rb
|