jeffp-wizardly 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc
CHANGED
data/lib/validation_group.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
# Validation group code from Alex Kira's repository of validationgroup on Github
|
2
|
-
# modifications made for wizardly integration
|
3
|
-
# included herein until such time the code can be managed and released independantly with modifications
|
4
1
|
module ValidationGroup
|
5
2
|
module ActiveRecord
|
6
3
|
module ActsMethods # extends ActiveRecord::Base
|
@@ -11,29 +8,29 @@ module ValidationGroup
|
|
11
8
|
cattr_accessor :validation_group_classes
|
12
9
|
self.validation_group_classes = {}
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
11
|
+
def self.validation_group_order; @validation_group_order; end
|
12
|
+
def self.validation_groups(all_classes = false)
|
13
|
+
return (self.validation_group_classes[self] || {}) unless all_classes
|
14
|
+
klasses = ValidationGroup::Util.current_and_ancestors(self).reverse
|
15
|
+
returning Hash.new do |hash|
|
16
|
+
klasses.each do |klass|
|
17
|
+
hash.merge! self.validation_group_classes[klass]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
24
21
|
end
|
25
22
|
end
|
26
23
|
|
27
24
|
def validation_group(name, options={})
|
28
25
|
self_groups = (self.validation_group_classes[self] ||= {})
|
29
26
|
self_groups[name.to_sym] = options[:fields] || []
|
30
|
-
|
31
|
-
|
27
|
+
@validation_group_order ||= []
|
28
|
+
@validation_group_order << name.to_sym
|
32
29
|
|
33
30
|
unless included_modules.include?(InstanceMethods)
|
34
31
|
attr_reader :current_validation_group, :current_validation_fields
|
35
32
|
include InstanceMethods
|
36
|
-
|
33
|
+
alias_method_chain :valid?, :validation_group
|
37
34
|
end
|
38
35
|
end
|
39
36
|
end
|
@@ -41,16 +38,16 @@ module ValidationGroup
|
|
41
38
|
module InstanceMethods # included in every model which calls validation_group
|
42
39
|
|
43
40
|
def enable_validation_group(group)
|
44
|
-
# Check if given validation group is defined for current class
|
45
|
-
#
|
41
|
+
# Check if given validation group is defined for current class or one of
|
42
|
+
# its ancestors
|
46
43
|
group_classes = self.class.validation_group_classes
|
47
44
|
found = ValidationGroup::Util.current_and_ancestors(self.class).
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
find do |klass|
|
46
|
+
group_classes[klass] && group_classes[klass].include?(group)
|
47
|
+
end
|
51
48
|
if found
|
52
49
|
@current_validation_group = group
|
53
|
-
|
50
|
+
@current_validation_fields = group_classes[found][group]
|
54
51
|
else
|
55
52
|
raise ArgumentError, "No validation group of name :#{group}"
|
56
53
|
end
|
@@ -58,35 +55,32 @@ module ValidationGroup
|
|
58
55
|
|
59
56
|
def disable_validation_group
|
60
57
|
@current_validation_group = nil
|
61
|
-
|
58
|
+
@current_validation_fields = nil
|
62
59
|
end
|
63
60
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
61
|
+
def should_validate?(attribute)
|
62
|
+
!self.validation_group_enabled? || (@current_validation_fields && @current_validation_fields.include?(attribute.to_sym))
|
63
|
+
end
|
68
64
|
|
69
65
|
def validation_group_enabled?
|
70
66
|
respond_to?(:current_validation_group) && !current_validation_group.nil?
|
71
67
|
end
|
72
68
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
69
|
+
# eliminates need to use :enable_validation_group before :valid? call --
|
70
|
+
# nice
|
71
|
+
def valid_with_validation_group?(group=nil)
|
72
|
+
self.enable_validation_group(group) if group
|
73
|
+
valid_without_validation_group?
|
74
|
+
end
|
78
75
|
end
|
79
76
|
|
80
77
|
module Errors # included in ActiveRecord::Errors
|
81
78
|
def add_with_validation_group(attribute,
|
82
|
-
|
83
|
-
|
84
|
-
add_error = true
|
85
|
-
if
|
86
|
-
|
87
|
-
end
|
88
|
-
add_without_validation_group(attribute, msg, *args, &block) if add_error
|
89
|
-
end
|
79
|
+
msg = @@default_error_messages[:invalid], *args,
|
80
|
+
&block)
|
81
|
+
add_error = @base.respond_to?(:should_validate?) ? @base.should_validate?(attribute.to_sym) : true
|
82
|
+
add_without_validation_group(attribute, msg, *args, &block) if add_error
|
83
|
+
end
|
90
84
|
|
91
85
|
def self.included(base) #:nodoc:
|
92
86
|
base.class_eval do
|
@@ -97,8 +91,8 @@ module ValidationGroup
|
|
97
91
|
end
|
98
92
|
|
99
93
|
module Util
|
100
|
-
# Return array consisting of current and its superclasses
|
101
|
-
#
|
94
|
+
# Return array consisting of current and its superclasses down to and
|
95
|
+
# including base_class.
|
102
96
|
def self.current_and_ancestors(current)
|
103
97
|
returning [] do |klasses|
|
104
98
|
klasses << current
|
@@ -5,8 +5,8 @@ class WizardlyControllerGenerator < Rails::Generator::Base
|
|
5
5
|
|
6
6
|
def initialize(runtime_args, runtime_options = {})
|
7
7
|
super
|
8
|
-
@controller_name = @args[0].underscore
|
9
|
-
@model_name = @args[1].underscore
|
8
|
+
@controller_name = @args[0].sub(/^:/, '').underscore
|
9
|
+
@model_name = @args[1].sub(/^:/, '').underscore
|
10
10
|
@completed_redirect = @args[2]
|
11
11
|
@canceled_redirect = @args[3]
|
12
12
|
opts = {}
|
@@ -15,7 +15,7 @@ class WizardlyScaffoldGenerator < Rails::Generator::Base
|
|
15
15
|
|
16
16
|
def initialize(runtime_args, runtime_options = {})
|
17
17
|
super
|
18
|
-
name = @args[0].underscore.sub(/_controller$/, '').camelize + 'Controller'
|
18
|
+
name = @args[0].sub(/^:/, '').underscore.sub(/_controller$/, '').camelize + 'Controller'
|
19
19
|
|
20
20
|
base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(name)
|
21
21
|
@controller_class_name_without_nesting = base_name.camelize
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jeffp-wizardly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Patmon
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-04 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|