grouped_validations 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,7 +4,7 @@ Allows you to define validation groups in ActiveRecord for more control over wha
4
4
 
5
5
  This can be useful for multi-page forms or wizard style data entry.
6
6
 
7
- Works with Rails 2.3.5 and probably earlier. Haven't tried it with Rails 3 yet.
7
+ Works with Rails 2.3 and Rails 3 (ActiveRecord only).
8
8
 
9
9
  == Installation
10
10
 
data/Rakefile CHANGED
@@ -3,9 +3,10 @@ require 'rake/rdoctask'
3
3
  require 'rake/gempackagetask'
4
4
  require 'rubygems/specification'
5
5
  require 'spec/rake/spectask'
6
+ require 'lib/grouped_validations/version'
6
7
 
7
8
  GEM_NAME = "grouped_validations"
8
- GEM_VERSION = '0.1.0'
9
+ GEM_VERSION = GroupedValidations::VERSION
9
10
 
10
11
  spec = Gem::Specification.new do |s|
11
12
  s.name = GEM_NAME
@@ -0,0 +1,33 @@
1
+ module GroupedValidations
2
+
3
+ module ClassMethods
4
+
5
+ def define_group_validation_callbacks(group)
6
+ define_callbacks :"validate_#{group}", :scope => 'validate'
7
+ end
8
+
9
+ def validate_with_groups(*args, &block)
10
+ if @current_validation_group
11
+ options = args.last
12
+ if options.is_a?(Hash) && options.key?(:on)
13
+ options[:if] = Array(options[:if])
14
+ options[:if] << "@_on_validate == :#{options[:on]}"
15
+ end
16
+ set_callback(:"validate_#{@current_validation_group}", *args, &block)
17
+ else
18
+ validate_without_groups(*args, &block)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ module InstanceMethods
25
+
26
+ def run_group_validation_callbacks(group)
27
+ @_on_validate = new_record? ? :create : :update
28
+ send(:"_run_validate_#{group}_callbacks")
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,39 @@
1
+ module GroupedValidations
2
+
3
+ module ClassMethods
4
+
5
+ def define_group_validation_callbacks(group)
6
+ base_name = :"validate_#{group}"
7
+ define_callbacks base_name, :"#{base_name}_on_create", :"#{base_name}_on_update"
8
+ end
9
+
10
+ def validation_method_with_groups(on)
11
+ if @current_validation_group
12
+ base_name = :"validate_#{@current_validation_group}"
13
+ case on
14
+ when :save then base_name
15
+ when :create then :"#{base_name}_on_create"
16
+ when :update then :"#{base_name}_on_update"
17
+ end
18
+ else
19
+ validation_method_without_groups on
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ module InstanceMethods
26
+
27
+ def run_group_validation_callbacks(group)
28
+ base_name = :"validate_#{group}"
29
+ run_callbacks(base_name)
30
+ if new_record?
31
+ run_callbacks(:"#{base_name}_on_create")
32
+ else
33
+ run_callbacks(:"#{base_name}_on_update")
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,3 @@
1
+ module GroupedValidations
2
+ VERSION = '0.2.0'
3
+ end
@@ -1,63 +1,37 @@
1
1
  module GroupedValidations
2
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
3
  module ClassMethods
16
4
 
17
- def validation_group(name, &block)
5
+ def validation_group(group, &block)
18
6
  raise "The validation_group method requires a block" unless block_given?
19
7
 
20
8
  self.validation_groups ||= []
21
- self.validation_groups << name
9
+ self.validation_groups << group
22
10
 
23
- base_name = :"validate_#{name}"
24
- define_callbacks base_name, :"#{base_name}_on_create", :"#{base_name}_on_update"
11
+ define_group_validation_callbacks group
25
12
 
26
- @current_validation_group = name
13
+ @current_validation_group = group
27
14
  class_eval &block
28
15
  @current_validation_group = nil
29
16
  end
30
17
 
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
18
  end
45
19
 
46
20
  module InstanceMethods
47
21
 
48
- def group_valid?(name)
49
- raise "Validation group '#{name}' not defined" unless validation_groups.include?(name)
22
+ def group_valid?(group)
23
+ raise "Validation group '#{group}' not defined" unless validation_groups.include?(group)
50
24
 
51
25
  errors.clear
52
- run_group_validation_callbacks name
26
+ run_group_validation_callbacks group
53
27
  errors.empty?
54
28
  end
55
29
 
56
30
  def groups_valid?(*groups)
57
31
  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
32
+ groups.each do |group|
33
+ raise "Validation group '#{group}' not defined" unless validation_groups.include?(group)
34
+ run_group_validation_callbacks group
61
35
  end
62
36
  errors.empty?
63
37
  end
@@ -70,18 +44,29 @@ module GroupedValidations
70
44
  errors.empty?
71
45
  end
72
46
 
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
47
  end
84
48
 
85
49
  end
86
50
 
87
- ActiveRecord::Base.send :include, GroupedValidations
51
+ ActiveRecord::Base.class_eval do
52
+ extend GroupedValidations::ClassMethods
53
+ include GroupedValidations::InstanceMethods
54
+ class_inheritable_accessor :validation_groups
55
+ alias_method_chain :valid?, :groups
56
+ end
57
+
58
+ if ActiveRecord::VERSION::MAJOR == 3
59
+ require 'grouped_validations/active_model'
60
+ ActiveRecord::Base.class_eval do
61
+ class << self
62
+ alias_method_chain :validate, :groups
63
+ end
64
+ end
65
+ else
66
+ require 'grouped_validations/active_record'
67
+ ActiveRecord::Base.class_eval do
68
+ class << self
69
+ alias_method_chain :validation_method, :groups
70
+ end
71
+ end
72
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ $:.unshift File.expand_path(File.dirname(__FILE__) + '/spec')
3
3
 
4
4
  require 'rubygems'
5
5
  require 'active_record'
6
+ require 'active_support'
6
7
 
7
8
  require 'grouped_validations'
8
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grouped_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
@@ -9,7 +9,7 @@ autorequire: grouped_validations
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-22 00:00:00 +11:00
12
+ date: 2010-02-23 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,9 @@ files:
25
25
  - MIT-LICENSE
26
26
  - README.rdoc
27
27
  - Rakefile
28
+ - lib/grouped_validations/active_model.rb
29
+ - lib/grouped_validations/active_record.rb
30
+ - lib/grouped_validations/version.rb
28
31
  - lib/grouped_validations.rb
29
32
  - spec/grouped_validations_spec.rb
30
33
  - spec/spec_helper.rb