grimen-validatious-on-rails 0.1.0 → 0.1.1
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.
@@ -5,7 +5,7 @@ class ValidatiousGenerator < Rails::Generator::Base
|
|
5
5
|
def manifest
|
6
6
|
record do |m|
|
7
7
|
m.template 'v2.standalone.full.min.js', File.join('public', 'javascripts', 'v2.standalone.full.min.js')
|
8
|
-
m.template '
|
8
|
+
m.template 'validatious.config.js', File.join('public', 'javascripts', 'validatious.config.js')
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -11,7 +11,7 @@ module ActionView
|
|
11
11
|
# Add validation class names to: text-fields.
|
12
12
|
#
|
13
13
|
def text_field_with_validation(object_name, method, options = {})
|
14
|
-
options = Validatious::RailsValidation.options_for(object_name, method, options)
|
14
|
+
options = ::Validatious::RailsValidation.options_for(object_name, method, options)
|
15
15
|
text_field_without_validation(object_name, method, options)
|
16
16
|
end
|
17
17
|
alias_method_chain :text_field, :validation
|
@@ -20,7 +20,7 @@ module ActionView
|
|
20
20
|
# Add validation class names to: password-fields.
|
21
21
|
#
|
22
22
|
def password_field_with_validation(object_name, method, options = {})
|
23
|
-
options = Validatious::RailsValidation.options_for(object_name, method, options)
|
23
|
+
options = ::Validatious::RailsValidation.options_for(object_name, method, options)
|
24
24
|
password_field_without_validation(object_name, method, options)
|
25
25
|
end
|
26
26
|
alias_method_chain :password_field, :validation
|
@@ -29,7 +29,7 @@ module ActionView
|
|
29
29
|
# Add validation class names to: text-areas.
|
30
30
|
#
|
31
31
|
def text_area_with_validation(object_name, method, options = {})
|
32
|
-
options = Validatious::RailsValidation.options_for(object_name, method, options)
|
32
|
+
options = ::Validatious::RailsValidation.options_for(object_name, method, options)
|
33
33
|
text_area_without_validation(object_name, method, options)
|
34
34
|
end
|
35
35
|
alias_method_chain :text_area, :validation
|
@@ -38,7 +38,7 @@ module ActionView
|
|
38
38
|
# Add validation class names to: check-boxes.
|
39
39
|
#
|
40
40
|
def check_box_with_validation(object_name, method, options = {}, checked_value = '1', unchecked_value = '0')
|
41
|
-
options = Validatious::RailsValidation.options_for(object_name, method, options)
|
41
|
+
options = ::Validatious::RailsValidation.options_for(object_name, method, options)
|
42
42
|
check_box_without_validation(object_name, method, options)
|
43
43
|
end
|
44
44
|
alias_method_chain :check_box, :validation
|
@@ -47,7 +47,7 @@ module ActionView
|
|
47
47
|
# Add validation class names to: radio-buttons.
|
48
48
|
#
|
49
49
|
def radio_button_with_validation(object_name, method, tag_value, options = {})
|
50
|
-
options = Validatious::RailsValidation.options_for(object_name, method, options)
|
50
|
+
options = ::Validatious::RailsValidation.options_for(object_name, method, options)
|
51
51
|
radio_button_without_validation(object_name, method, tag_value, options)
|
52
52
|
end
|
53
53
|
alias_method_chain :radio_button, :validation
|
@@ -31,16 +31,15 @@ module Validatious
|
|
31
31
|
# Input may be an ActiveRecord class, a class name (string), or an object
|
32
32
|
# name along with a method/field.
|
33
33
|
#
|
34
|
-
def options_for(object_name,
|
35
|
-
validation = self.from_active_record(object_name,
|
34
|
+
def options_for(object_name, attribute_method, options = {})
|
35
|
+
validation = self.from_active_record(object_name, attribute_method)
|
36
36
|
|
37
37
|
# Loop validation and add/append pairs to options.
|
38
|
-
validation.each_pair do |
|
39
|
-
options[
|
40
|
-
options[
|
41
|
-
|
38
|
+
validation.each_pair do |attribute, value|
|
39
|
+
options[attribute] ||= ''
|
40
|
+
options[attribute] << value
|
42
41
|
# Shake out duplicates.
|
43
|
-
options[
|
42
|
+
options[attribute] = options[attribute].split.uniq.join(' ').strip
|
44
43
|
end
|
45
44
|
options
|
46
45
|
end
|
@@ -55,17 +54,39 @@ module Validatious
|
|
55
54
|
# Returns a string that will be recognized by Validatious as a class name in
|
56
55
|
# form markup.
|
57
56
|
#
|
58
|
-
def from_active_record(object_or_class,
|
57
|
+
def from_active_record(object_or_class, attribute_method)
|
59
58
|
klass = object_or_class.to_s.classify.constantize
|
60
59
|
options = {:class => ''}
|
61
60
|
validation_classes = []
|
62
|
-
|
61
|
+
|
63
62
|
# Iterate thorugh the validations for the current class,
|
64
63
|
# and collect validation options.
|
65
|
-
klass.reflect_on_validations_for(
|
66
|
-
|
67
|
-
|
64
|
+
klass.reflect_on_validations_for(attribute_method.to_sym).each do |validation|
|
65
|
+
validates_type = validation.macro.to_s.sub(/^validates_/, '')
|
66
|
+
|
67
|
+
# Skip "confirmation_of"-validation info for the attribute that
|
68
|
+
# needs to be confirmed. Validatious expects this validation rule
|
69
|
+
# on the confirmation field. *
|
70
|
+
unless validates_type =~ /^confirmation_of$/
|
71
|
+
validation_options = self.send(validates_type, validation)
|
72
|
+
validation_classes << validation_options[:class]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Special case for "confirmation_of"-validation (see * above).
|
77
|
+
if attribute_method.to_s =~ /(.+)_confirmation$/
|
78
|
+
confirm_attribute_method = $1
|
79
|
+
# Check if validates_confirmation_of(:hello) actually exists,
|
80
|
+
# if :hello_confirmation field exists - just to be safe.
|
81
|
+
klass.reflect_on_validations_for(confirm_attribute_method.to_sym).each do |validation|
|
82
|
+
if validation.macro.to_s =~ /^validates_confirmation_of$/
|
83
|
+
validation_options = self.confirmation_of(validation)
|
84
|
+
validation_classes << validation_options[:class]
|
85
|
+
break
|
86
|
+
end
|
87
|
+
end
|
68
88
|
end
|
89
|
+
|
69
90
|
options[:class] = [options[:class], validation_classes].flatten.compact.join(' ')
|
70
91
|
options
|
71
92
|
end
|
@@ -82,17 +103,25 @@ module Validatious
|
|
82
103
|
#
|
83
104
|
# Resolve validation from validates_associated.
|
84
105
|
#
|
106
|
+
# Note: Most probably too hard to implement.
|
107
|
+
#
|
85
108
|
def associated(validation)
|
86
109
|
{:class => ''}
|
87
110
|
end
|
88
111
|
|
89
112
|
#
|
90
|
-
#
|
113
|
+
# Resolve validation from validates_confirmation_of.
|
91
114
|
#
|
92
|
-
# Note:
|
115
|
+
# Note: This validation needed to be treated a bit differently in compare
|
116
|
+
# to the other validations. See "from_active_record".
|
93
117
|
#
|
94
118
|
def confirmation_of(validation)
|
95
|
-
|
119
|
+
field_id_to_confirm = unless validation.active_record.present?
|
120
|
+
"#{validation.active_record.name.tableize.singularize.gsub('/', '_')}_#{validation.name}"
|
121
|
+
else
|
122
|
+
"#{validation.name}"
|
123
|
+
end
|
124
|
+
{:class => "confirmation-of_#{field_id_to_confirm}"}
|
96
125
|
end
|
97
126
|
|
98
127
|
#
|
data/test/test_helper.rb
CHANGED
@@ -42,6 +42,7 @@ build_model :bogus_items do
|
|
42
42
|
boolean :signed
|
43
43
|
|
44
44
|
validates_presence_of :name, :body
|
45
|
+
validates_confirmation_of :name
|
45
46
|
validates_acceptance_of :signed
|
46
47
|
validates_format_of :url,
|
47
48
|
:with => /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i,
|
@@ -45,6 +45,12 @@ class FormHelperTest < Test::Unit::TestCase
|
|
45
45
|
# TODO
|
46
46
|
end
|
47
47
|
|
48
|
+
def test_confirmation_field
|
49
|
+
# TODO: Add form-builder-test as well
|
50
|
+
assert_has_class "confirmation-of_name", text_field(:bogus_item, :name_confirmation)
|
51
|
+
assert_has_class "confirmation-of_name text", text_field(:bogus_item, :name_confirmation, :class => "text")
|
52
|
+
end
|
53
|
+
|
48
54
|
def test_normal_label
|
49
55
|
assert_equal "<label for=\"bogus_item_name\">Name</label>", label(:bogus_item, :name)
|
50
56
|
end
|
@@ -5,8 +5,15 @@ class RailsValidationTest < Test::Unit::TestCase
|
|
5
5
|
|
6
6
|
include ActionView::Helpers::FormHelper
|
7
7
|
|
8
|
+
#
|
9
|
+
# Simulate a validation
|
10
|
+
#
|
11
|
+
def validation(macro, options = {})
|
12
|
+
ActiveRecord::Reflection::MacroReflection.new(macro, :name, options, BogusItem.new)
|
13
|
+
end
|
14
|
+
|
8
15
|
def test_acceptance_of
|
9
|
-
validation = Validatious::RailsValidation.
|
16
|
+
validation = Validatious::RailsValidation.acceptance_of(validation(:validates_acceptance_of))
|
10
17
|
assert_equal 'required', validation[:class]
|
11
18
|
end
|
12
19
|
|
@@ -15,7 +22,8 @@ class RailsValidationTest < Test::Unit::TestCase
|
|
15
22
|
end
|
16
23
|
|
17
24
|
def test_confirmation_of
|
18
|
-
|
25
|
+
validation = Validatious::RailsValidation.confirmation_of(validation(:validates_confirmation_of))
|
26
|
+
assert_equal 'confirmation-of_name', validation[:class]
|
19
27
|
end
|
20
28
|
|
21
29
|
def test_exclusion_of
|
@@ -78,11 +86,4 @@ class RailsValidationTest < Test::Unit::TestCase
|
|
78
86
|
# TODO: not implemented
|
79
87
|
end
|
80
88
|
|
81
|
-
#
|
82
|
-
# Simulate a validation
|
83
|
-
#
|
84
|
-
def validation(macro, options = {})
|
85
|
-
ActiveRecord::Reflection::MacroReflection.new(macro, :name, options, nil)
|
86
|
-
end
|
87
|
-
|
88
89
|
end
|