grouped_validations 0.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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Adam Meehan
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
@@ -0,0 +1,55 @@
1
+ = Grouped Validations
2
+
3
+ Allows you to define validation groups in ActiveRecord for more control over what validations you want to run.
4
+
5
+ This can be useful for multi-page forms or wizard style data entry.
6
+
7
+ Works with Rails 2.3.5 and probably earlier. Haven't tried it with Rails 3 yet.
8
+
9
+ == Installation
10
+
11
+ Just install the gem
12
+
13
+ gem install grouped_validations
14
+
15
+ Add it to your Rails environment gems
16
+
17
+ config.gem 'grouped_validations'
18
+
19
+ == Usage
20
+
21
+ Define validations as you would normally but inside a validation_group block which you pass a group
22
+ name to.
23
+
24
+ class Person < ActiveRecord::Base
25
+ validation_group :name do
26
+ validates_presence_of :first_name
27
+ validates_presence_of :last_name
28
+ end
29
+
30
+ validates_presence_of :sex
31
+ end
32
+
33
+ You can define validations outside the group as normal.
34
+
35
+ To check for errors for only a certain group of validations
36
+
37
+ p = Person.new
38
+ p.group_valid?(:name) # => false
39
+ p.first_name = 'John'
40
+ p.last_name = 'Smith'
41
+ p.group_valid?(:name) # => true
42
+
43
+ If you run the normal valid? method all validations, inside and outside validation groups, will be run.
44
+
45
+ p.valid? # => false because sex is not present
46
+
47
+ You can also check validation for multiple groups
48
+
49
+ p.groups_valid?(:group1, :group2)
50
+
51
+ == Credits
52
+
53
+ * Adam Meehan (http://github.com/adzap)
54
+
55
+ Copyright (c) 2010 Adam Meehan, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,69 @@
1
+ require 'rubygems'
2
+ require 'rake/rdoctask'
3
+ require 'rake/gempackagetask'
4
+ require 'rubygems/specification'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM_NAME = "grouped_validations"
8
+ GEM_VERSION = '0.1.0'
9
+
10
+ spec = Gem::Specification.new do |s|
11
+ s.name = GEM_NAME
12
+ s.version = GEM_VERSION
13
+ s.platform = Gem::Platform::RUBY
14
+ s.rubyforge_project = GEM_NAME
15
+ s.has_rdoc = true
16
+ s.extra_rdoc_files = ["README.rdoc"]
17
+ s.summary = "Define validation groups in ActiveRecord for greater control over which validations to run."
18
+ s.description = s.summary
19
+ s.author = "Adam Meehan"
20
+ s.email = "adam.meehan@gmail.com"
21
+ s.homepage = "http://github.com/adzap/grouped_validations"
22
+
23
+ s.require_path = 'lib'
24
+ s.autorequire = GEM_NAME
25
+ s.files = %w(MIT-LICENSE README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
26
+ end
27
+
28
+ desc 'Default: run specs.'
29
+ task :default => :spec
30
+
31
+ spec_files = Rake::FileList["spec/**/*_spec.rb"]
32
+
33
+ desc "Run specs"
34
+ Spec::Rake::SpecTask.new do |t|
35
+ t.spec_files = spec_files
36
+ t.spec_opts = ["-c"]
37
+ end
38
+
39
+ desc "Generate code coverage"
40
+ Spec::Rake::SpecTask.new(:coverage) do |t|
41
+ t.spec_files = spec_files
42
+ t.rcov = true
43
+ t.rcov_opts = ['--exclude', 'spec,/var/lib/gems']
44
+ end
45
+
46
+ desc 'Generate documentation for plugin.'
47
+ Rake::RDocTask.new(:rdoc) do |rdoc|
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = 'GroupedValidations'
50
+ rdoc.options << '--line-numbers' << '--inline-source'
51
+ rdoc.rdoc_files.include('README')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
54
+
55
+ Rake::GemPackageTask.new(spec) do |pkg|
56
+ pkg.gem_spec = spec
57
+ end
58
+
59
+ desc "install the gem locally"
60
+ task :install => [:package] do
61
+ sh %{gem install pkg/#{GEM_NAME}-#{GEM_VERSION}}
62
+ end
63
+
64
+ desc "create a gemspec file"
65
+ task :make_spec do
66
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
67
+ file.puts spec.to_ruby
68
+ end
69
+ end
@@ -0,0 +1,87 @@
1
+ module GroupedValidations
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.class_eval do
6
+ include InstanceMethods
7
+ class_inheritable_accessor :validation_groups
8
+ alias_method_chain :valid?, :groups
9
+ class << self
10
+ alias_method_chain :validation_method, :groups
11
+ end
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+
17
+ def validation_group(name, &block)
18
+ raise "The validation_group method requires a block" unless block_given?
19
+
20
+ self.validation_groups ||= []
21
+ self.validation_groups << name
22
+
23
+ base_name = :"validate_#{name}"
24
+ define_callbacks base_name, :"#{base_name}_on_create", :"#{base_name}_on_update"
25
+
26
+ @current_validation_group = name
27
+ class_eval &block
28
+ @current_validation_group = nil
29
+ end
30
+
31
+ def validation_method_with_groups(on)
32
+ if @current_validation_group
33
+ base_name = :"validate_#{@current_validation_group}"
34
+ case on
35
+ when :save then base_name
36
+ when :create then :"#{base_name}_on_create"
37
+ when :update then :"#{base_name}_on_update"
38
+ end
39
+ else
40
+ validation_method_without_groups on
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ module InstanceMethods
47
+
48
+ def group_valid?(name)
49
+ raise "Validation group '#{name}' not defined" unless validation_groups.include?(name)
50
+
51
+ errors.clear
52
+ run_group_validation_callbacks name
53
+ errors.empty?
54
+ end
55
+
56
+ def groups_valid?(*groups)
57
+ errors.clear
58
+ groups.each do |name|
59
+ raise "Validation group '#{name}' not defined" unless validation_groups.include?(name)
60
+ run_group_validation_callbacks name
61
+ end
62
+ errors.empty?
63
+ end
64
+
65
+ def valid_with_groups?
66
+ valid_without_groups?
67
+ (validation_groups || []).each do |group|
68
+ run_group_validation_callbacks group
69
+ end
70
+ errors.empty?
71
+ end
72
+
73
+ def run_group_validation_callbacks(name)
74
+ base_name = :"validate_#{name}"
75
+ run_callbacks(base_name)
76
+ if new_record?
77
+ run_callbacks(:"#{base_name}_on_create")
78
+ else
79
+ run_callbacks(:"#{base_name}_on_update")
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ end
86
+
87
+ ActiveRecord::Base.send :include, GroupedValidations
@@ -0,0 +1,96 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe GroupedValidations do
4
+ before do
5
+ reset_class Person
6
+ end
7
+
8
+ it "should add validation_group class method" do
9
+ Person.should respond_to(:validation_group)
10
+ end
11
+
12
+ it "should store defined validation group names" do
13
+ Person.validation_group(:dummy) { }
14
+ Person.validation_groups.should == [:dummy]
15
+ end
16
+
17
+ it "it should add group_valid? method which takes a group name param" do
18
+ Person.validation_group(:dummy) { }
19
+ p = Person.new
20
+ p.group_valid?(:dummy)
21
+ end
22
+
23
+ it "should raise exception if valiation group not defined on group_valid check" do
24
+ p = Person.new
25
+ lambda { p.group_valid?(:dummy) }.should raise_exception
26
+ end
27
+
28
+ it "should run the validations defined inside the validation group" do
29
+ Person.validation_group :name do
30
+ validates_presence_of :first_name
31
+ validates_presence_of :last_name
32
+ end
33
+
34
+ p = Person.new
35
+ p.group_valid?(:name)
36
+ p.should have(2).errors
37
+
38
+ p.first_name = 'Dave'
39
+ p.last_name = 'Smith'
40
+ p.group_valid?(:name)
41
+ p.should have(0).errors
42
+ end
43
+
44
+ it "should run all validation groups passed to groups_valid?" do
45
+ Person.class_eval do
46
+ validation_group :first_name_group do
47
+ validates_presence_of :first_name
48
+ end
49
+ validation_group :last_name_group do
50
+ validates_presence_of :last_name
51
+ end
52
+ end
53
+
54
+ p = Person.new
55
+ p.groups_valid?(:first_name_group, :last_name_group)
56
+ p.should have(2).errors
57
+ end
58
+
59
+ it "should run all validation including groups when valid? method called" do
60
+ Person.class_eval do
61
+ validation_group :first_name_group do
62
+ validates_presence_of :first_name
63
+ end
64
+ validation_group :last_name_group do
65
+ validates_presence_of :last_name
66
+ end
67
+
68
+ validates_presence_of :sex
69
+ end
70
+
71
+ p = Person.new
72
+ p.valid?
73
+ p.should have(3).errors
74
+ end
75
+
76
+ it "should respect :on validation option" do
77
+ Person.validation_group :name do
78
+ validates_presence_of :first_name, :on => :create
79
+ validates_presence_of :last_name, :on => :update
80
+ end
81
+
82
+ p = Person.new
83
+ p.group_valid?(:name)
84
+ p.should have(1).errors
85
+ p.first_name = 'Dave'
86
+ p.group_valid?(:name)
87
+ p.should have(0).errors
88
+
89
+ p.save.should be_true
90
+ p.group_valid?(:name)
91
+ p.should have(1).errors
92
+ p.last_name = 'Smith'
93
+ p.group_valid?(:name)
94
+ p.should have(0).errors
95
+ end
96
+ end
@@ -0,0 +1,35 @@
1
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/lib')
2
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/spec')
3
+
4
+ require 'rubygems'
5
+ require 'active_record'
6
+
7
+ require 'grouped_validations'
8
+
9
+ ActiveRecord::Migration.verbose = false
10
+ ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
11
+
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :people, :force => true do |t|
14
+ t.string :first_name
15
+ t.string :last_name
16
+ t.integer :sex
17
+ end
18
+ end
19
+
20
+ class Person < ActiveRecord::Base
21
+ end
22
+
23
+ module SpecHelper
24
+ def reset_class(klass, &block)
25
+ name = klass.name.to_sym
26
+ Object.send(:remove_const, name)
27
+ Object.const_set(name, Class.new(ActiveRecord::Base))
28
+ new_klass = Object.const_get(name)
29
+ new_klass.class_eval &block if block_given?
30
+ end
31
+ end
32
+
33
+ Spec::Runner.configure do |config|
34
+ config.include SpecHelper
35
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grouped_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Meehan
8
+ autorequire: grouped_validations
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-22 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Define validation groups in ActiveRecord for greater control over which validations to run.
17
+ email: adam.meehan@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.rdoc
27
+ - Rakefile
28
+ - lib/grouped_validations.rb
29
+ - spec/grouped_validations_spec.rb
30
+ - spec/spec_helper.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/adzap/grouped_validations
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project: grouped_validations
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Define validation groups in ActiveRecord for greater control over which validations to run.
59
+ test_files: []
60
+