validation-sets 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,14 +5,24 @@ require 'rake/rdoctask'
5
5
  desc 'Default: run unit tests.'
6
6
  task :default => :test
7
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
8
+ namespace :test do
9
+ Rake::TestTask.new(:rails2) do |t|
10
+ t.libs += %w(test test/test_helper/rails2)
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Test the plugin with Rails 3.'
16
+ Rake::TestTask.new(:rails3) do |t|
17
+ t.libs += %w(test test/test_helper/rails3)
18
+ t.pattern = 'test/**/*_test.rb'
19
+ t.verbose = true
20
+ end
14
21
  end
15
22
 
23
+ desc 'Run all tests'
24
+ task :test => ['test:rails2', 'test:rails3']
25
+
16
26
  desc 'Generate documentation'
17
27
  Rake::RDocTask.new(:rdoc) do |rdoc|
18
28
  rdoc.rdoc_dir = 'rdoc'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -1,61 +1,4 @@
1
1
  module ValidationSets
2
- autoload :ValidationSet, 'validation_sets/validation_set'
3
-
4
- # Used to set the current validation set during definition of the model
5
- attr_accessor :current_validation_set
6
-
7
- # Returns the name of the validation callback method for a save method and the label of a
8
- # validation set.
9
- def validation_set_method(on, label)
10
- case on
11
- when :save then "validate_#{label}_set".to_sym
12
- when :create then "validate_on_create_#{label}_set".to_sym
13
- when :update then "validate_on_update_#{label}_set".to_sym
14
- end
15
- end
16
-
17
- # Returns the name of the validation callback method for a save method.
18
- def validation_method(on)
19
- if current_validation_set
20
- validation_set_method(on, current_validation_set)
21
- else
22
- super
23
- end
24
- end
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
40
- def validation_set_for(label, &block)
41
- [:save, :create, :update].each do |save_method|
42
- callback_chain = validation_method(save_method)
43
- callback_chain_for_set = validation_set_method(save_method, label)
44
- unless respond_to?(callback_chain_for_set)
45
- define_callbacks(callback_chain_for_set)
46
- define_method(callback_chain_for_set) { run_callbacks(callback_chain_for_set) }
47
- send(callback_chain, callback_chain_for_set, :if => Proc.new { |r| label.to_sym == r._validation_set })
48
- end
49
- end
50
-
51
- validation_set = ValidationSet.new(self, label)
52
-
53
- before = current_validation_set
54
- self.current_validation_set = label.to_sym
55
- block.call(validation_set)
56
- self.current_validation_set = before
57
- end
58
-
59
2
  module InstanceMethods
60
3
  attr_reader :_validation_set
61
4
 
@@ -71,7 +14,7 @@ module ValidationSets
71
14
  # account.use_validation_set(nil) # Turns off all validation sets
72
15
  def use_validation_set(set)
73
16
  if self.class.validation_set_defined?(set)
74
- @_validation_set = set
17
+ @_validation_set = set.to_sym
75
18
  else
76
19
  raise ArgumentError, "There is no validation set `#{set}'"
77
20
  end
@@ -79,6 +22,89 @@ module ValidationSets
79
22
  end
80
23
  end
81
24
 
25
+ if ActiveRecord::Base.respond_to?(:set_callback)
26
+ module ValidationSets
27
+ autoload :ValidationProxy, 'validation_sets/validation_proxy'
28
+
29
+ # Add a validation set to the model.
30
+ #
31
+ # class Account < ActiveRecord::Base
32
+ # validation_set_for(:activation) do |set|
33
+ # set.validates_presence_of :username
34
+ # end
35
+ # end
36
+ def validation_set_for(set, &block)
37
+ @_validation_sets ||= Set.new
38
+ @_validation_sets << set
39
+ yield ValidationProxy.new(self, set)
40
+ end
41
+
42
+ # Returns true if the validation set is defined on this class
43
+ def validation_set_defined?(set)
44
+ @_validation_sets.include?(set)
45
+ end
46
+ end
47
+ else
48
+ module ValidationSets
49
+ autoload :ValidationSet, 'validation_sets/validation_set'
50
+
51
+ # Used to set the current validation set during definition of the model
52
+ attr_accessor :current_validation_set
53
+
54
+ # Returns the name of the validation callback method for a save method and the label of a
55
+ # validation set.
56
+ def validation_set_method(on, label)
57
+ case on
58
+ when :save then "validate_#{label}_set".to_sym
59
+ when :create then "validate_on_create_#{label}_set".to_sym
60
+ when :update then "validate_on_update_#{label}_set".to_sym
61
+ end
62
+ end
63
+
64
+ # Returns the name of the validation callback method for a save method.
65
+ def validation_method(on)
66
+ if current_validation_set
67
+ validation_set_method(on, current_validation_set)
68
+ else
69
+ super
70
+ end
71
+ end
72
+
73
+ # Returns true if the validation set is defined on this class
74
+ def validation_set_defined?(set)
75
+ [:save, :create, :update].any? do |on|
76
+ respond_to?(validation_set_method(on, set))
77
+ end
78
+ end
79
+
80
+ # Add a validation set to the model.
81
+ #
82
+ # class Account < ActiveRecord::Base
83
+ # validation_set_for(:activation) do |set|
84
+ # set.validates_presence_of :username
85
+ # end
86
+ # end
87
+ def validation_set_for(label, &block)
88
+ [:save, :create, :update].each do |save_method|
89
+ callback_chain = validation_method(save_method)
90
+ callback_chain_for_set = validation_set_method(save_method, label)
91
+ unless respond_to?(callback_chain_for_set)
92
+ define_callbacks(callback_chain_for_set)
93
+ define_method(callback_chain_for_set) { run_callbacks(callback_chain_for_set) }
94
+ send(callback_chain, callback_chain_for_set, :if => Proc.new { |r| label.to_sym == r._validation_set })
95
+ end
96
+ end
97
+
98
+ validation_set = ValidationSet.new(self, label)
99
+
100
+ before = current_validation_set
101
+ self.current_validation_set = label.to_sym
102
+ block.call(validation_set)
103
+ self.current_validation_set = before
104
+ end
105
+ end
106
+ end
107
+
82
108
  module ActiveRecord #:nodoc:
83
109
  class Base #:nodoc:
84
110
  extend ValidationSets
@@ -0,0 +1,26 @@
1
+ module ValidationSets
2
+ # A ValidationSet instance is used to add extra options to validations to make
3
+ # sure the only run when a certain validation set is active.
4
+ class ValidationProxy
5
+ def initialize(model, set)
6
+ @model = model
7
+ @set = set
8
+ end
9
+
10
+ # Forwards all other methods (ie. validates_presence_of) to the model class
11
+ # with extra options
12
+ def method_missing(method, *args, &block)
13
+ options = args.extract_options!
14
+ options[:if] = Array.wrap(options[:if])
15
+ options[:if] << "_validation_set == :#{@set}"
16
+ # BUG: Callbacks get deleted from their chain when they're the same as a previous callback
17
+ if (method == :validate) and !block and args[0].kind_of?(Symbol)
18
+ callback = args.shift
19
+ block = lambda { send(callback) }
20
+ end
21
+ args << options
22
+ @model.send(method, *args, &block)
23
+ end
24
+ end
25
+ end
26
+
@@ -2,24 +2,24 @@ module ValidationSets
2
2
  # A ValidationSet instance is used to redirect the original validation methods (validate,
3
3
  # validate_on_create, and validate_on_update) to the callback chain for the set.
4
4
  class ValidationSet
5
- def initialize(model, label)
5
+ def initialize(model, set)
6
6
  @model = model
7
- @label = label
7
+ @set = set
8
8
  end
9
9
 
10
10
  # Adds a validation method or proc that always runs
11
11
  def validate(*params, &block)
12
- send(validation_set_method(:save, @label), *params, &block)
12
+ send(validation_set_method(:save, @set), *params, &block)
13
13
  end
14
14
 
15
15
  # Adds a validation method or proc that runs on create
16
16
  def validate_on_create(*params, &block)
17
- send(validation_set_method(:create, @label), *params, &block)
17
+ send(validation_set_method(:create, @set), *params, &block)
18
18
  end
19
19
 
20
20
  # Adds a validation method or proc that runs on update
21
21
  def validate_on_update(*params, &block)
22
- send(validation_set_method(:update, @label), *params, &block)
22
+ send(validation_set_method(:update, @set), *params, &block)
23
23
  end
24
24
 
25
25
  # Forwards all other methods (ie. validates_presence_of) to the model class.
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../../shared', __FILE__)
2
+
3
+ module ValidationSetsTests
4
+ module Initializer
5
+ def self.load_dependencies
6
+ if rails_directory
7
+ $:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
8
+ $:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
9
+ else
10
+ require 'rubygems'
11
+ gem 'rails', '< 3.0'
12
+ end
13
+
14
+ require 'test/unit'
15
+
16
+ require 'active_support'
17
+ require 'active_support/test_case'
18
+ require 'active_record'
19
+ require 'active_record/test_case'
20
+ require 'active_record/base' # this is needed because of dependency hell
21
+
22
+ $:.unshift File.expand_path('../../lib', __FILE__)
23
+ require File.join(PLUGIN_ROOT, 'rails', 'init')
24
+ end
25
+ end
26
+ end
27
+
28
+ ValidationSetsTests::Initializer.start
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../../shared', __FILE__)
2
+
3
+ module ValidationSetsTests
4
+ module Initializer
5
+ def self.load_dependencies
6
+ if rails_directory
7
+ $:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
8
+ $:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
9
+ else
10
+ require 'rubygems'
11
+ gem 'rails', '> 3.0'
12
+ end
13
+
14
+ require 'test/unit'
15
+
16
+ require 'active_support'
17
+ require 'active_support/test_case'
18
+ require 'active_record'
19
+ require 'active_record/test_case'
20
+ require 'active_record/base' # this is needed because of dependency hell
21
+
22
+ $:.unshift File.expand_path('../../lib', __FILE__)
23
+ require File.join(PLUGIN_ROOT, 'rails', 'init')
24
+ end
25
+ end
26
+ end
27
+
28
+ ValidationSetsTests::Initializer.start
@@ -0,0 +1,39 @@
1
+ module ValidationSetsTests
2
+ module Initializer
3
+ VENDOR_RAILS = File.expand_path('../../../../../../vendor/rails', __FILE__)
4
+ PLUGIN_ROOT = File.expand_path('../../../', __FILE__)
5
+
6
+ def self.rails_directory
7
+ if File.exist?(VENDOR_RAILS)
8
+ VENDOR_RAILS
9
+ end
10
+ end
11
+
12
+ def self.configure_database
13
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
14
+ ActiveRecord::Migration.verbose = false
15
+ end
16
+
17
+ def self.setup_database
18
+ ActiveRecord::Schema.define(:version => 1) do
19
+ create_table :accounts do |t|
20
+ t.column :fullname, :string
21
+ t.column :email, :string
22
+ t.column :username, :string
23
+ t.column :password, :string
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.teardown_database
29
+ ActiveRecord::Base.connection.tables.each do |table|
30
+ ActiveRecord::Base.connection.drop_table(table)
31
+ end
32
+ end
33
+
34
+ def self.start
35
+ load_dependencies
36
+ configure_database
37
+ end
38
+ end
39
+ end
@@ -1,9 +1,11 @@
1
- require File.expand_path('../../test_helper', __FILE__)
1
+ require 'test_helper'
2
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
3
+ if !ActiveRecord::Base.respond_to?(:set_callback) # Rails 2
4
+ class ValidationSetTest < ActiveSupport::TestCase
5
+ test "forwards missing methods to the model" do
6
+ model = 'Model'
7
+ validation_set = ValidationSets::ValidationSet.new(model, :admin)
8
+ assert_equal 5, validation_set.length
9
+ end
8
10
  end
9
11
  end
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ if ActiveRecord::Base.respond_to?(:set_callback) # Rails 3
4
+ class Person
5
+ def self.arguments(*args)
6
+ args
7
+ end
8
+ end
9
+
10
+ class ValidationProxyTest < ActiveSupport::TestCase
11
+ test "forwards missing methods to the model" do
12
+ validation_proxy = ValidationSets::ValidationProxy.new(Person, :admin)
13
+ args = validation_proxy.arguments
14
+ assert args.last.has_key?(:if)
15
+ end
16
+ end
17
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require 'test_helper'
2
2
 
3
3
  class Account < ActiveRecord::Base
4
4
  attr_accessor :current_password
@@ -47,7 +47,7 @@ class ValidationSetsTest < ActiveSupport::TestCase
47
47
  test "global validation should always run" do
48
48
  account = Account.new
49
49
  assert !account.valid?
50
- assert account.errors.on(:fullname)
50
+ assert_error(account, :fullname)
51
51
 
52
52
  account.fullname = "Patricia Herder"
53
53
  assert account.valid?
@@ -58,9 +58,9 @@ class ValidationSetsTest < ActiveSupport::TestCase
58
58
  account = Account.new
59
59
  account.use_validation_set(:activation)
60
60
  assert !account.valid?
61
- assert account.errors.on(:email)
62
- assert account.errors.on(:username)
63
- assert account.errors.on(:password)
61
+ assert_error(account, :email)
62
+ assert_error(account, :username)
63
+ assert_error(account, :password)
64
64
 
65
65
  account.fullname = "Patricia Herder"
66
66
  account.email = 'patricia@example.com'
@@ -74,8 +74,8 @@ class ValidationSetsTest < ActiveSupport::TestCase
74
74
  account = Account.new
75
75
  account.use_validation_set(:admin)
76
76
  assert !account.valid?
77
- assert account.errors.on(:fullname)
78
- assert account.errors.on(:email)
77
+ assert_error(account, :fullname)
78
+ assert_error(account, :email)
79
79
 
80
80
  account.fullname = "Patricia Herder"
81
81
  account.email = 'patricia@example.com'
@@ -95,6 +95,15 @@ class ValidationSetsTest < ActiveSupport::TestCase
95
95
  end
96
96
  end
97
97
 
98
+ def assert_error(object, attribute)
99
+ message = "Expected #{attribute} to have a validation error"
100
+ if object.errors[:base].kind_of?(Array)
101
+ assert !object.errors[attribute].empty?, message
102
+ else
103
+ assert object.errors.on(attribute), message
104
+ end
105
+ end
106
+
98
107
  def setup
99
108
  ValidationSetsTests::Initializer.setup_database
100
109
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validation-sets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
4
+ hash: 31
5
+ prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 1.1.0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Manfred Stienstra
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-05 00:00:00 +02:00
18
+ date: 2011-02-24 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,17 +29,20 @@ extra_rdoc_files:
29
29
  - LICENSE
30
30
  - README.rdoc
31
31
  files:
32
- - .gitignore
33
32
  - .kick
34
33
  - LICENSE
35
34
  - README.rdoc
36
35
  - Rakefile
37
36
  - VERSION
38
37
  - lib/validation_sets.rb
38
+ - lib/validation_sets/validation_proxy.rb
39
39
  - lib/validation_sets/validation_set.rb
40
40
  - rails/init.rb
41
- - test/test_helper.rb
41
+ - test/test_helper/rails2/test_helper.rb
42
+ - test/test_helper/rails3/test_helper.rb
43
+ - test/test_helper/shared.rb
42
44
  - test/validation_sets/validation_set_test.rb
45
+ - test/validation_sets/validaton_proxy_test.rb
43
46
  - test/validation_sets_test.rb
44
47
  - validation-sets.gemspec
45
48
  has_rdoc: true
@@ -47,8 +50,8 @@ homepage: http://fingertips.github.com
47
50
  licenses: []
48
51
 
49
52
  post_install_message:
50
- rdoc_options:
51
- - --charset=UTF-8
53
+ rdoc_options: []
54
+
52
55
  require_paths:
53
56
  - lib
54
57
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -72,11 +75,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
75
  requirements: []
73
76
 
74
77
  rubyforge_project:
75
- rubygems_version: 1.3.7
78
+ rubygems_version: 1.5.2
76
79
  signing_key:
77
80
  specification_version: 3
78
81
  summary: A Rails plugin that adds validation sets to Active Record.
79
82
  test_files:
80
- - test/test_helper.rb
83
+ - test/test_helper/rails2/test_helper.rb
84
+ - test/test_helper/rails3/test_helper.rb
85
+ - test/test_helper/shared.rb
81
86
  - test/validation_sets/validation_set_test.rb
87
+ - test/validation_sets/validaton_proxy_test.rb
82
88
  - test/validation_sets_test.rb
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- pkg
2
- rdoc
@@ -1,64 +0,0 @@
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