validatious-on-rails 0.3.1 → 0.3.2

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.
@@ -1,17 +1,33 @@
1
1
  module ValidatiousOnRails
2
2
  module Helpers
3
-
4
- extend self
5
-
3
+
4
+ def attach_validator_for(object_name, method, options = {})
5
+ options = ::ValidatiousOnRails::ModelValidations.options_for(object_name, method, options, @content_for_validatious)
6
+ custom_js = options.delete(:js)
7
+ content_for :validatious, custom_js if custom_js.present?
8
+ options
9
+ end
10
+
6
11
  # Helper for the layout to host the custom Validatious-validators for forms rendered.
7
12
  # This will only show up if the current view contains one - or more - forms that
8
13
  # are triggered to be validated with Validatous (i.e. ValidatiousOnRails that is).
9
14
  #
10
- def custom_validatious_validators
15
+ def attached_validators
11
16
  if @content_for_validatious.present?
12
17
  content_tag(:script, @content_for_validatious,
13
- :type => 'text/javascript', :id => 'custom_validatious_validators')
18
+ :type => 'text/javascript', :id => 'custom_validators')
19
+ end
20
+ end
21
+ alias :javascript_for_validatious :attached_validators
22
+
23
+ def self.extract_args!(*args)
24
+ tail = []
25
+ tail.unshift(args.pop) until args.blank? || args.last.is_a?(::Hash)
26
+ unless args.last.is_a?(::Hash)
27
+ args = tail
28
+ tail = []
14
29
  end
30
+ return args, tail
15
31
  end
16
32
 
17
33
  end
@@ -47,12 +47,18 @@ module ValidatiousOnRails
47
47
  # Input may be an ActiveRecord class, a class name (string), or an object
48
48
  # name along with a method/field.
49
49
  #
50
- def options_for(object_name, attribute_method, options = {})
51
- validation = self.from_active_record(object_name, attribute_method)
52
- options.merge!(
53
- :class => [options[:class], validation[:classes]].flatten.compact.uniq.join(' '),
54
- :validators => validation[:validators].flatten.compact.uniq
55
- )
50
+ def options_for(object_name, attribute_method, options = {}, existing_validators = nil)
51
+ validators = self.from_active_record(object_name, attribute_method)
52
+ validator_classes, validator_js = [options[:class]], []
53
+
54
+ # Only attach validators that are not already attached.
55
+ validators.flatten.compact.uniq.each do |v|
56
+ validator_js << v.to_js unless existing_validators.present? && /#{v.name}/ =~ existing_validators
57
+ validator_classes << v.to_class
58
+ end
59
+ classes = validator_classes.compact.join(' ').strip
60
+ js = validator_js.compact.join(' ').strip
61
+ options.merge!(:class => (classes unless classes.blank?), :js => (js unless js.blank?))
56
62
  end
57
63
 
58
64
  # Groks Rails validations, and is able to convert a rails validation to
@@ -68,7 +74,7 @@ module ValidatiousOnRails
68
74
  #
69
75
  def from_active_record(object_or_class, attribute_method)
70
76
  klass = object_or_class.to_s.classify.constantize
71
- attribute_validation = {:classes => [], :validators => []}
77
+ validators = []
72
78
 
73
79
  # Iterate thorugh the validations for the current class,
74
80
  # and collect validation options.
@@ -79,10 +85,7 @@ module ValidatiousOnRails
79
85
  # needs to be confirmed. Validatious expects this validation rule
80
86
  # on the confirmation field. *
81
87
  unless validates_type =~ /^confirmation_of$/
82
- validation_options = self.send(validates_type.to_sym, validation)
83
- attribute_validation[:classes] << validation_options[:class]
84
- # One or multiple validators for each validation (possible to combine validators).
85
- attribute_validation[:validators] << validation_options[:validator] || validation_options[:validators]
88
+ validators << self.send(validates_type.to_sym, validation)
86
89
  end
87
90
  end
88
91
 
@@ -93,31 +96,27 @@ module ValidatiousOnRails
93
96
  # if :hello_confirmation field exists - just to be safe.
94
97
  klass.reflect_on_validations_for(confirm_attribute_method.to_sym).each do |validation|
95
98
  if validation.macro.to_s =~ /^validates_confirmation_of$/
96
- validation_options = self.confirmation_of(validation)
97
- attribute_validation[:classes] << validation_options[:class]
99
+ validators << self.confirmation_of(validation)
98
100
  break
99
101
  end
100
102
  end
101
103
  end
102
- attribute_validation
104
+ validators
103
105
  end
104
106
 
105
107
  # Resolve validation from validates_acceptance_of.
106
108
  #
107
109
  # Alias, but might change: acceptance_of <=> presence_of
108
110
  #
109
- # TODO: Make this a custom validator - handle :accept.
110
- #
111
111
  # NOTE: Not supported:
112
- # * :accept - TODO: not taken into consideration
113
- # right now (but must have value "true" for db column values)
114
- # * :allow_nil - TODO.
115
112
  # * :on - TODO.
116
113
  # * :if/:unless - hard to port all to client-side JavaScript
117
114
  # (impossible: procs, unaccessible valiables, etc.).
118
115
  #
119
116
  def acceptance_of(validation)
120
- {:class => 'required', :validator => nil}
117
+ validators = []
118
+ validation.options[:accept] ||= '1' # Rails default.
119
+ validators << Validatious::AcceptanceValidator.new(validation, validation.options[:accept])
121
120
  end
122
121
 
123
122
  # Resolve validation from validates_associated.
@@ -125,7 +124,7 @@ module ValidatiousOnRails
125
124
  # NOTE: Not supported - low prio.
126
125
  #
127
126
  def associated(validation)
128
- {:class => '', :validator => nil}
127
+ nil
129
128
  end
130
129
 
131
130
  # Resolve validation from validates_confirmation_of.
@@ -135,17 +134,19 @@ module ValidatiousOnRails
135
134
  # TODO: Message should be Rails I18n message, not Validatious.
136
135
  #
137
136
  # NOTE: Not supported:
137
+ # * :message - TODO: Explicit or Rails I18n/default message.
138
138
  # * :on - TODO.
139
139
  # * :if/:unless - hard to port all to client-side JavaScript
140
140
  # (impossible: procs, unaccessible valiables, etc.).
141
141
  #
142
142
  def confirmation_of(validation)
143
- field_id_to_confirm = unless validation.active_record.present?
143
+ validators = []
144
+ arg = unless validation.active_record.present?
144
145
  "#{validation.active_record.name.tableize.singularize.gsub('/', '_')}_#{validation.name}"
145
146
  else
146
147
  "#{validation.name}"
147
148
  end
148
- {:class => "confirmation-of_#{field_id_to_confirm}", :validator => nil}
149
+ validators << Validatious::ConfirmationValidator.new(validation, arg)
149
150
  end
150
151
 
151
152
  # Resolve validation from validates_exclusion_of.
@@ -158,8 +159,8 @@ module ValidatiousOnRails
158
159
  # (impossible: procs, unaccessible valiables, etc.).
159
160
  #
160
161
  def exclusion_of(validation)
161
- validator = Validatious::ExclusionValidator.new(validation)
162
- {:class => validator.name, :validator => validator}
162
+ validators = []
163
+ validators << Validatious::ExclusionValidator.new(validation)
163
164
  end
164
165
 
165
166
  # Resolve validation from validates_format_of.
@@ -173,8 +174,8 @@ module ValidatiousOnRails
173
174
  # (impossible: procs, unaccessible valiables, etc.).
174
175
  #
175
176
  def format_of(validation)
176
- validator = Validatious::FormatValidator.new(validation)
177
- {:class => validator.name, :validator => validator}
177
+ validators = []
178
+ validators << Validatious::FormatValidator.new(validation)
178
179
  end
179
180
 
180
181
  # Resolve validation from validates_inclusion_of.
@@ -187,8 +188,8 @@ module ValidatiousOnRails
187
188
  # (impossible: procs, unaccessible valiables, etc.).
188
189
  #
189
190
  def inclusion_of(validation)
190
- validator = Validatious::InclusionValidator.new(validation)
191
- {:class => validator.name, :validator => validator}
191
+ validators = []
192
+ validators << Validatious::InclusionValidator.new(validation)
192
193
  end
193
194
 
194
195
  # Resolve validation from validates_length_of.
@@ -203,34 +204,25 @@ module ValidatiousOnRails
203
204
  # (impossible: procs, unaccessible valiables, etc.).
204
205
  #
205
206
  def length_of(validation)
206
- # TODO: DRY up this with the neat idea/combo:
207
- # Validator#new(name, validation, *args)-idea + Validator#to_class
208
- validators, min, max = case true
209
- when validation.options[:is].present?
210
- [Validatious::Length::IsValidator.new(validation),
211
- validation.options[:is], validation.options[:is]]
212
- when [:in, :within].any? { |k| validation.options[k].present? } ||
213
- [:minimum, :maximum].all? { |k| validation.options[k].present? }
214
- validation.options[:within] ||=
215
- validation.options[:in] ||
216
- (validation.options[:minimum].to_i..validation.options[:maximum].to_i)
217
- [[Validatious::Length::MinimumValidator.new(validation),
218
- Validatious::Length::MaximumValidator.new(validation)],
219
- validation.options[:within].min, validation.options[:within].max]
220
- when validation.options[:minimum].present?
221
- [Validatious::Length::MinimumValidator.new(validation),
222
- validation.options[:minimum], nil]
223
- when validation.options[:maximum].present?
224
- [Validatious::Length::MaximumValidator.new(validation),
225
- nil, validation.options[:maximum]]
207
+ validators = []
208
+
209
+ if validation.options[:is].present?
210
+ validators << Validatious::Length::IsValidator.new(validation, validation.options[:is])
211
+ elsif [:in, :within, :minimum, :maximum].any? { |k| validation.options[k].present? }
212
+ validation.options[:within] ||= validation.options[:in]
213
+ validation.options[:minimum] ||= validation.options[:within].min rescue nil
214
+ validation.options[:maximum] ||= validation.options[:within].max rescue nil
215
+
216
+ if validation.options[:minimum].present?
217
+ validators << Validatious::Length::MinimumValidator.new(validation, validation.options[:minimum])
218
+ end
219
+
220
+ if validation.options[:maximum].present?
221
+ validators << Validatious::Length::MaximumValidator.new(validation, validation.options[:maximum])
222
+ end
226
223
  end
227
- validators = [*validators]
228
- # This piece of code is a bit diffuse, but works. =)
229
- classes = [
230
- ("#{validators.first.name}_#{min}" if min),
231
- ("#{validators.last.name}_#{max}" if max)
232
- ].compact.uniq.join(' ')
233
- {:class => classes, :validator => validators}
224
+
225
+ validators
234
226
  end
235
227
  alias :size_of :length_of
236
228
 
@@ -240,36 +232,29 @@ module ValidatiousOnRails
240
232
  # numericality-odd, numericality-only-integer, numericality-equal-to_5, etc.
241
233
  #
242
234
  # NOTE: Not supported:
243
- # * :on - TODO.
235
+ # * :on - TODO.en
244
236
  # * :if/:unless - hard to port all to client-side JavaScript
245
237
  # (impossible: procs, unaccessible valiables, etc.).
246
238
  #
247
239
  def numericality_of(validation)
248
240
  validators = []
249
- values = {}
250
-
241
+
251
242
  if validation.options[:odd] && !validation.options[:even]
252
243
  validators << Validatious::Numericality::OddValidator.new(validation)
253
244
  end
245
+
254
246
  if validation.options[:even] && !validation.options[:odd]
255
247
  validators << Validatious::Numericality::EvenValidator.new(validation)
256
248
  end
257
-
249
+
258
250
  (validation.options.keys & [:only_integer, :equal_to, :less_than, :less_than_or_equal_to,
259
251
  :greater_than, :greater_than_or_equal_to]).each { |v|
260
- klass = "::ValidatiousOnRails::Validatious::Numericality::#{v.to_s.classify}Validator".constantize
261
- validators << (validator = klass.new(validation))
262
- values.merge!(validator.name.to_sym => validation.options[v]) if validation.options[v].is_a?(::Numeric)
252
+ validator_klass = "::ValidatiousOnRails::Validatious::Numericality::#{v.to_s.classify}Validator".constantize
253
+ value = validation.options[v] if validation.options[v].is_a?(::Numeric)
254
+ validators << validator_klass.new(validation, value)
263
255
  }
264
-
265
- # TODO: Needs DRYer solution,
266
- # Maybe: validator.args = [...] => validator.to_class => "#{validator.name}_params[0]_params[1]_etc..."
267
- classes = validators.collect { |validator|
268
- [validator.name,
269
- (values[validator.name.to_sym] if values[validator.name.to_sym].present?)
270
- ].compact.join('_')
271
- }.join(' ')
272
- {:class => classes, :validator => validators}
256
+
257
+ validators
273
258
  end
274
259
 
275
260
  # Resolve validation from validates_presence_of.
@@ -282,7 +267,8 @@ module ValidatiousOnRails
282
267
  # (impossible: procs, unaccessible valiables, etc.).
283
268
  #
284
269
  def presence_of(validation)
285
- {:class => 'required', :validator => nil}
270
+ validators = []
271
+ validators << Validatious::PresenceValidator.new(validation)
286
272
  end
287
273
 
288
274
  # Resolve validation from validates_uniqueness_of.
@@ -290,7 +276,7 @@ module ValidatiousOnRails
290
276
  # TODO: Implement using RemoteValidator.
291
277
  #
292
278
  def uniqueness_of(validation)
293
- {:class => '', :validator => nil}
279
+ nil
294
280
  end
295
281
 
296
282
  # Unknown validations - if no matching custom validator is found/registered.
@@ -300,7 +286,7 @@ module ValidatiousOnRails
300
286
  " No custom Validatious validator found for this validation makro. " <<
301
287
  "Maybe you forgot to register you custom validation using: " <<
302
288
  "ValidatiousOnRails::ModelValidations.add(<CustomValidationClass>)", :warn
303
- {:class => '', :validator => nil}
289
+ nil
304
290
  end
305
291
 
306
292
  # TODO: Include custom validations here...
@@ -1,4 +1,5 @@
1
1
  # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), *%w[.. helpers])
2
3
  #
3
4
  # Tap into the built-in form/input helpers to add validatious class names from
4
5
  # model validations.
@@ -7,48 +8,22 @@ module ActionView # :nodoc:
7
8
  module Helpers # :nodoc:
8
9
  module FormHelper # :nodoc:
9
10
 
10
- FIELD_TYPES = [:text_field, :password_field, :text_area, :file_field, :radio_button].freeze
11
+ include ::ValidatiousOnRails::Helpers
12
+
13
+ FIELD_TYPES = [:text_field, :password_field, :text_area, :file_field, :radio_button, :check_box].freeze
11
14
 
12
15
  # Only altering the options hash is interesting - we want to set a validator class for fields,
13
16
  # so the hooking of these helpers don't have to be very explicit.
14
17
  #
15
18
  FIELD_TYPES.each do |field_type|
16
19
  define_method :"#{field_type}_with_validation" do |*args|
17
- options = args.extract_options!
18
- # Get the validation options.
19
- options = ::ValidatiousOnRails::ModelValidations.options_for(args.first, args.second, options)
20
-
21
- # Attach custom validator - if any - to the layout (in the <head>-tag - the unobtrusive way).
22
- # Don't attach validator(s) if it/they already attached.
23
- # TODO: Refactor into helper.
24
- validators_js = options.delete(:validators).collect { |v|
25
- v.to_s unless /#{v.name}/ =~ @content_for_validatious
26
- }.join(' ')
27
- content_for :validatious, validators_js if validators_js.present?
28
-
29
- self.send :"#{field_type}_without_validation", *(args << options)
20
+ args, tail = ::ValidatiousOnRails::Helpers.extract_args!(*args)
21
+ options = self.attach_validator_for(args.first, args.second, args.extract_options!)
22
+ self.send :"#{field_type}_without_validation", *((args << options) + tail)
30
23
  end
31
24
  alias_method_chain field_type, :validation
32
25
  end
33
26
 
34
- # Special case...no hash as last argument.
35
- #
36
- def check_box_with_validation(object_name, method, options = {}, checked_value = '1', unchecked_value = '0')
37
- # Get the validation options.
38
- options = ::ValidatiousOnRails::ModelValidations.options_for(object_name, method, options)
39
-
40
- # Attach custom validator - if any - to the layout (in the <head>-tag - the unobtrusive way).
41
- # Don't attach validator(s) if it/they already attached.
42
- # TODO: Refactor into helper.
43
- validators_js = options.delete(:validators).collect { |v|
44
- v.to_s unless /#{v.name}/ =~ @content_for_validatious
45
- }.join(' ')
46
- content_for :validatious, validators_js if validators_js.present?
47
-
48
- self.check_box_without_validation object_name, method, options, checked_value, unchecked_value
49
- end
50
- alias_method_chain :check_box, :validation
51
-
52
27
  # Adds the title attribute to label tags when there is no title
53
28
  # set, and the label text is provided. The title is set to object_name.humanize
54
29
  #
@@ -62,23 +37,15 @@ module ActionView # :nodoc:
62
37
 
63
38
  module FormOptionsHelper
64
39
 
40
+ include ::ValidatiousOnRails::Helpers
41
+
65
42
  FIELD_TYPES = [:time_zone_select, :select, :collection_select, :grouped_options_for_select].freeze
66
43
 
67
44
  FIELD_TYPES.each do |field_type|
68
45
  define_method :"#{field_type}_with_validation" do |*args|
69
- options = args.extract_options!
70
- # Get the validation options.
71
- options = ::ValidatiousOnRails::ModelValidations.options_for(args.first, args.second, options)
72
-
73
- # Attach custom validator - if any - to the layout (in the <head>-tag - the unobtrusive way).
74
- # Don't attach validator(s) if it/they already attached.
75
- # TODO: Refactor into helper.
76
- validators_js = options.delete(:validators).collect { |v|
77
- v.to_s unless /#{v.name}/ =~ @content_for_validatious
78
- }.join(' ')
79
- content_for :validatious, validators_js if validators_js.present?
80
-
81
- self.send :"#{field_type}_without_validation", *(args << options)
46
+ args, tail = ::ValidatiousOnRails::Helpers.extract_args!(*args)
47
+ options = self.attach_validator_for(args.first, args.second, args.extract_options!)
48
+ self.send :"#{field_type}_without_validation", *((args << options) + tail)
82
49
  end
83
50
  alias_method_chain field_type, :validation
84
51
  end
@@ -87,23 +54,15 @@ module ActionView # :nodoc:
87
54
 
88
55
  module DateHelper
89
56
 
57
+ include ::ValidatiousOnRails::Helpers
58
+
90
59
  FIELD_TYPES = [:date_select, :datetime_select, :time_select].freeze
91
60
 
92
61
  FIELD_TYPES.each do |field_type|
93
62
  define_method :"#{field_type}_with_validation" do |*args|
94
- options = args.extract_options!
95
- # Get the validation options.
96
- options = ::ValidatiousOnRails::ModelValidations.options_for(args.first, args.second, options)
97
-
98
- # Attach custom validator - if any - to the layout (in the <head>-tag - the unobtrusive way).
99
- # Don't attach validator(s) if it/they already attached.
100
- # TODO: Refactor into helper.
101
- validators_js = options.delete(:validators).collect { |v|
102
- v.to_s unless /#{v.name}/ =~ @content_for_validatious
103
- }.join(' ')
104
- content_for :validatious, validators_js if validators_js.present?
105
-
106
- self.send :"#{field_type}_without_validation", *(args << options)
63
+ args, tail = ::ValidatiousOnRails::Helpers.extract_args!(*args)
64
+ options = self.attach_validator_for(args.first, args.second, args.extract_options!)
65
+ self.send :"#{field_type}_without_validation", *((args << options) + tail)
107
66
  end
108
67
  alias_method_chain field_type, :validation
109
68
  end
@@ -28,14 +28,17 @@ module ValidatiousOnRails
28
28
  :params,
29
29
  :aliases,
30
30
  :accept_empty,
31
- :fn
31
+ :fn,
32
+ :args
32
33
 
33
- def initialize(name, options = {})
34
+ def initialize(name, *args)
34
35
  raise ValidatorError, "Parameter :name is required for an Validatious validator" unless name.present?
35
36
  self.name = name
37
+ options = args.extract_options!
36
38
  options.each do |attr, value|
37
39
  self.send(:"#{attr}=", value) if value.present?
38
40
  end
41
+ self.args = args
39
42
  end
40
43
 
41
44
  # The primary name of the validator. This is the name you use with v2.$v('name');
@@ -125,7 +128,7 @@ module ValidatiousOnRails
125
128
  (@fn ||= "function(field, value, params) {return true;}").gsub(/[\n\t]/, '')
126
129
  end
127
130
 
128
- def to_s
131
+ def to_js
129
132
  options = {
130
133
  :name => self.name,
131
134
  :message => self.message,
@@ -142,6 +145,15 @@ module ValidatiousOnRails
142
145
  }.compact.join(', ')
143
146
  "v2.Validator.add({#{js_options}});"
144
147
  end
148
+ alias :to_s :to_js
149
+
150
+ # Generate a full Validatious-style "class function call" to generic validators, e.g.
151
+ # Length::MinimumValidator#to_class(5) => "length-minimum_5", etc.
152
+ #
153
+ def to_class(*args)
154
+ args = @args if args.blank?
155
+ [self.name, (args if args.present?)].flatten.compact.join('_')
156
+ end
145
157
 
146
158
  class << self
147
159
 
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.join(File.dirname(__FILE__), *%w[.. validator]))
3
+
4
+ module ValidatiousOnRails
5
+ module Validatious
6
+ class AcceptanceValidator < ClientSideValidator
7
+
8
+ def initialize(validation, options = {})
9
+ name = 'acceptance-accept'
10
+ super name, options
11
+ self.message = self.class.generate_message(validation)
12
+ self.accept_empty = validation.options[:allow_nil]
13
+ self.fn = %{
14
+ var accept_value = params[0] + '';
15
+ if (accept_value == 'true') {
16
+ accept_value = true
17
+ };
18
+ if (accept_value == 'false') {
19
+ accept_value = false
20
+ };
21
+ return value == accept_value;
22
+ }
23
+ self.fn.freeze
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.join(File.dirname(__FILE__), *%w[.. validator]))
3
+
4
+ module ValidatiousOnRails
5
+ module Validatious
6
+ class ConfirmationValidator < ClientSideValidator
7
+
8
+ def initialize(validation, options = {})
9
+ name = 'confirmation-of'
10
+ super name, options
11
+ self.params = ['field-id']
12
+ self.message = self.class.generate_message(validation)
13
+ self.accept_empty = true
14
+ # Identical to Validatious "confirmation-of" validator, but we want Rails I18n message support, so...
15
+ self.fn = %{
16
+ return value === v2.$f(params[0]).getValue();
17
+ }
18
+ self.fn.freeze
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -10,7 +10,6 @@ module ValidatiousOnRails
10
10
  def initialize(validation, options = {})
11
11
  name, alias_name = self.class.generate_name(validation, :in, self.class.generate_id(validation.options[:in].inspect))
12
12
  super name, options
13
- self.aliases = [alias_name] - [name]
14
13
  self.message = self.class.generate_message(validation)
15
14
  self.accept_empty = validation.options[:allow_blank] || validation.options[:allow_nil]
16
15
  self.fn = %{
@@ -19,7 +19,6 @@ module ValidatiousOnRails
19
19
  def initialize(validation, options = {})
20
20
  name, alias_name = self.class.generate_name(validation, :with, self.class.generate_id(validation.options[:with].inspect))
21
21
  super name, options
22
- self.aliases = [alias_name] - [name]
23
22
  self.message = self.class.generate_message(validation)
24
23
  self.accept_empty = validation.options[:allow_blank] || validation.options[:allow_nil]
25
24
  self.fn = %{
@@ -10,7 +10,6 @@ module ValidatiousOnRails
10
10
  def initialize(validation, options = {})
11
11
  name, alias_name = self.class.generate_name(validation, :in, self.class.generate_id(validation.options[:in].inspect))
12
12
  super name, options
13
- self.aliases = [alias_name] - [name]
14
13
  self.message = self.class.generate_message(validation)
15
14
  self.accept_empty = validation.options[:allow_blank] || validation.options[:allow_nil]
16
15
  self.fn = %{
@@ -10,7 +10,6 @@ module ValidatiousOnRails
10
10
  name, alias_name = self.class.generate_name(validation, :is, validation.options[:is])
11
11
  name = 'length-is'
12
12
  super name, options
13
- self.aliases = [alias_name] - [name]
14
13
  self.params = ['count']
15
14
  self.message = self.class.generate_message(validation, :key => :wrong_length)
16
15
  self.accept_empty = validation.options[:allow_blank] || validation.options[:allow_nil]
@@ -10,7 +10,6 @@ module ValidatiousOnRails
10
10
  name, alias_name = self.class.generate_name(validation, :maximum, validation.options[:maximum])
11
11
  name = 'length-maximum'
12
12
  super name, options
13
- self.aliases = [alias_name] - [name]
14
13
  self.params = ['count']
15
14
  self.message = self.class.generate_message(validation, :key => :too_long)
16
15
  self.accept_empty = validation.options[:allow_blank] || validation.options[:allow_nil]
@@ -10,7 +10,6 @@ module ValidatiousOnRails
10
10
  name, alias_name = self.class.generate_name(validation, :minimum, validation.options[:minimum])
11
11
  name = 'length-minimum'
12
12
  super name, options
13
- self.aliases = [alias_name] - [name]
14
13
  self.params = ['count']
15
14
  self.message = self.class.generate_message(validation, :key => :too_short)
16
15
  self.accept_empty = validation.options[:allow_blank] || validation.options[:allow_nil]
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.join(File.dirname(__FILE__), *%w[.. validator]))
3
+
4
+ module ValidatiousOnRails
5
+ module Validatious
6
+ class PresenceValidator < ClientSideValidator
7
+
8
+ def initialize(validation, options = {})
9
+ name = 'presence'
10
+ super name, options
11
+ self.message = self.class.generate_message(validation)
12
+ self.accept_empty = false
13
+ # Identical to Validatious "required" validator, but we want Rails I18n message support, so...
14
+ self.fn = %{
15
+ return !v2.empty(value) && !(typeof value.length !== 'undefined' && value.length === 0);
16
+ }
17
+ self.fn.freeze
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -5,9 +5,9 @@ require File.join(File.dirname(__FILE__), *%w[validatious-on-rails rails])
5
5
  require File.join(File.dirname(__FILE__), *%w[validatious-on-rails helpers])
6
6
 
7
7
  module ValidatiousOnRails # :nodoc:
8
-
8
+
9
9
  extend self
10
-
10
+
11
11
  # Standard error: Acts as base error class for the plugin.
12
12
  #
13
13
  class ValidatiousOnRailsError < ::StandardError
@@ -16,11 +16,11 @@ module ValidatiousOnRails # :nodoc:
16
16
  super message
17
17
  end
18
18
  end
19
-
19
+
20
20
  mattr_accessor :verbose
21
-
21
+
22
22
  @@verbose = ::Object.const_defined?(:RAILS_ENV) ? (::RAILS_ENV.to_sym == :development) : true
23
-
23
+
24
24
  # Logging helper: Internal debug-logging for the plugin.
25
25
  #
26
26
  def log(message, level = :info)
@@ -29,13 +29,5 @@ module ValidatiousOnRails # :nodoc:
29
29
  @@logger ||= ::Logger.new(::STDOUT)
30
30
  @@logger.send(level.to_sym, message)
31
31
  end
32
-
33
- # Alias method for: ValidatiousOnRails::Helpers#custom_validatious_validators
34
- #
35
- def custom_validators
36
- Helpers.custom_validatious_validators
37
- end
38
- alias :include_custom_validators :custom_validators
39
- ::ActionController::Base.helper_method :custom_validators, :include_custom_validators
40
-
32
+
41
33
  end
data/test/test_helper.rb CHANGED
@@ -4,44 +4,33 @@
4
4
  # convenience methods.
5
5
  #
6
6
 
7
+ def smart_require(lib_name, gem_name, gem_version = '>= 0.0.0')
8
+ begin
9
+ require lib_name
10
+ rescue LoadError
11
+ gem gem_name, gem_version
12
+ require lib_name
13
+ end
14
+ end
15
+
7
16
  begin
8
17
  require File.expand_path(File.join(File.dirname(__FILE__), *%w(.. .. .. .. config environment)))
9
18
  rescue LoadError
10
19
  require 'rubygems'
11
20
 
12
- gem 'test-unit', '1.2.3'
13
- gem 'activerecord', '>= 1.2.3'
14
- gem 'actionpack', '>= 1.2.3'
15
- gem 'sqlite3-ruby', '>= 1.2.0'
16
-
17
- require 'test/unit'
18
- require 'active_record'
19
- require 'action_controller'
20
- require 'sqlite3'
21
+ smart_require 'test/unit', 'test-unit', '= 1.2.3'
22
+ smart_require 'active_record', 'activerecord', '>= 1.2.3'
23
+ smart_require 'action_controller', 'actionpack', '>= 1.2.3'
24
+ smart_require 'sqlite3', 'sqlite3-ruby', '>= 1.2.0'
21
25
  end
22
26
 
23
- begin
24
- require 'context'
25
- rescue LoadError
26
- gem 'jeremymcanally-context', '>= 0.5.5'
27
- require 'context'
28
- end
27
+ smart_require 'redgreen', 'redgreen', '>= 0.10.4'
28
+ smart_require 'context', 'jeremymcanally-context', '>= 0.5.5'
29
+ smart_require 'rr', 'rr', '>= 0.10.0'
30
+ smart_require 'acts_as_fu', 'nakajima-acts_as_fu', '>= 0.0.5'
29
31
 
30
- begin
31
- require 'rr'
32
- rescue LoadError
33
- gem 'rr', '>= 0.0.0'
34
- require 'rr'
35
- end
36
32
  extend RR::Adapters::RRMethods
37
33
 
38
- begin
39
- require 'acts_as_fu'
40
- rescue LoadError
41
- gem 'nakajima-acts_as_fu', '>= 0.0.5'
42
- require 'acts_as_fu'
43
- end
44
-
45
34
  require 'validatious-on-rails'
46
35
 
47
36
  # TODO: Should extend Rails validators with this - to test respond_to.
@@ -70,7 +59,7 @@ build_model :bogus_items do
70
59
 
71
60
  validates_presence_of :name, :body, :variant
72
61
  validates_confirmation_of :name
73
- validates_acceptance_of :signed
62
+ validates_acceptance_of :signed, :accept => true
74
63
  validates_format_of :url,
75
64
  :with => /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i,
76
65
  :name => 'url', :message => 'Invalid URL.'
@@ -8,7 +8,7 @@ class HelpersTest < ::ActionView::TestCase
8
8
  include ActionView::Helpers::FormHelper
9
9
 
10
10
  attr_accessor :output_buffer
11
-
11
+
12
12
  before do
13
13
  @output_buffer ||= ''
14
14
  end
@@ -17,7 +17,7 @@ class HelpersTest < ::ActionView::TestCase
17
17
  context "custom_validatious_validators" do
18
18
 
19
19
  test "should not output custom validators if there are none" do
20
- helper_output = ::ValidatiousOnRails::Helpers.custom_validatious_validators
20
+ helper_output = ::ValidatiousOnRails::Helpers.attached_validators
21
21
  assert_equal '', helper_output.to_s
22
22
  end
23
23
 
@@ -27,7 +27,7 @@ class HelpersTest < ::ActionView::TestCase
27
27
  end
28
28
 
29
29
  # FIXME: Not sure how to test content_for-helpers...returns nil.
30
- concat ::ValidatiousOnRails::Helpers.custom_validatious_validators
30
+ concat ::ValidatiousOnRails::Helpers.attached_validators
31
31
 
32
32
  # In parts...
33
33
  # assert_match /<script.+>.*v2.Validator.*<\/script>/, output_buffer
@@ -7,9 +7,20 @@ class ModelValidationsTest < ::ActiveSupport::TestCase
7
7
 
8
8
  include ActionView::Helpers::FormHelper
9
9
 
10
- test "acceptance_of" do
11
- validation = ::ValidatiousOnRails::ModelValidations.acceptance_of(validation(:validates_acceptance_of))
12
- assert_equal 'required', validation[:class]
10
+ context "acceptance_of" do
11
+ test "with defaults" do
12
+ validators = ::ValidatiousOnRails::ModelValidations.acceptance_of(
13
+ validation(:validates_acceptance_of)
14
+ )
15
+ assert_validator_class 'acceptance-accept_1', validators
16
+ end
17
+
18
+ test "with :accept" do
19
+ validators = ::ValidatiousOnRails::ModelValidations.acceptance_of(
20
+ validation(:validates_acceptance_of, :accept => true)
21
+ )
22
+ assert_validator_class 'acceptance-accept_true', validators
23
+ end
13
24
  end
14
25
 
15
26
  test "associated" do
@@ -17,162 +28,163 @@ class ModelValidationsTest < ::ActiveSupport::TestCase
17
28
  end
18
29
 
19
30
  test "confirmation_of" do
20
- validation = ::ValidatiousOnRails::ModelValidations.confirmation_of(validation(:validates_confirmation_of))
21
- assert_equal 'confirmation-of_name', validation[:class]
31
+ validators = ::ValidatiousOnRails::ModelValidations.confirmation_of(
32
+ validation(:validates_confirmation_of)
33
+ )
34
+ assert_validator_class 'confirmation-of_name', validators
22
35
  end
23
36
 
24
37
  test "exclusion_of" do
25
38
  values = (6..10).to_a
26
- validation = ::ValidatiousOnRails::ModelValidations.exclusion_of(
39
+ validators = ::ValidatiousOnRails::ModelValidations.exclusion_of(
27
40
  validation(:validates_exclusion_of, :in => values)
28
41
  )
29
- assert_match /^exclusion-in-(\d+)/, validation[:class]
30
- assert_match /#{values.to_json}/, validation[:validator].fn
42
+ assert_validator_class /^exclusion-in-(\d+)/, validators
43
+ assert_match /#{values.to_json}/, validators.first.fn
31
44
  end
32
45
 
33
46
  test "format_of" do
34
47
  pattern = /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i
35
- validation = ::ValidatiousOnRails::ModelValidations.format_of(
48
+ validators = ::ValidatiousOnRails::ModelValidations.format_of(
36
49
  validation(:validates_format_of, :with => pattern)
37
50
  )
38
- assert_match /^format-with-(\d+)/, validation[:class]
39
- assert_match /#{pattern.inspect[1,-1]}/, validation[:validator].fn
51
+ assert_validator_class /^format-with-(\d+)/, validators
52
+ assert_match /#{pattern.inspect[1,-1]}/, validators.first.fn
40
53
  end
41
54
 
42
55
  test "inclusion_of" do
43
56
  values = (1..5).to_a
44
- validation = ::ValidatiousOnRails::ModelValidations.inclusion_of(
57
+ validators = ::ValidatiousOnRails::ModelValidations.inclusion_of(
45
58
  validation(:validates_inclusion_of, :in => values)
46
59
  )
47
- assert_match /^inclusion-in-(\d+)/, validation[:class]
48
- assert_match /#{values.to_json}/, validation[:validator].fn
60
+ assert_validator_class /^inclusion-in-(\d+)/, validators
61
+ assert_match /#{values.to_json}/, validators.first.fn
49
62
  end
50
63
 
51
64
  context "length_of" do
52
65
 
53
66
  test "with :is" do
54
- validation = ::ValidatiousOnRails::ModelValidations.length_of(
67
+ validators = ::ValidatiousOnRails::ModelValidations.length_of(
55
68
  validation(:validates_length_of, :is => 2)
56
69
  )
57
- assert_equal 'length-is_2', validation[:class]
70
+ assert_validator_class 'length-is_2', validators
58
71
  end
59
72
 
60
73
  test "with :in" do
61
- validation = ::ValidatiousOnRails::ModelValidations.length_of(
74
+ validators = ::ValidatiousOnRails::ModelValidations.length_of(
62
75
  validation(:validates_length_of, :in => 2..10)
63
76
  )
64
- assert_equal 'length-minimum_2 length-maximum_10', validation[:class]
77
+ assert_validator_class 'length-minimum_2 length-maximum_10', validators
65
78
  end
66
79
 
67
80
  test "with :within" do
68
- validation = ::ValidatiousOnRails::ModelValidations.length_of(
81
+ validators = ::ValidatiousOnRails::ModelValidations.length_of(
69
82
  validation(:validates_length_of, :within => 2..10)
70
83
  )
71
- assert_equal 'length-minimum_2 length-maximum_10', validation[:class]
84
+ assert_validator_class 'length-minimum_2 length-maximum_10', validators
72
85
  end
73
86
 
74
87
  test "with :minimum" do
75
- validation = ::ValidatiousOnRails::ModelValidations.length_of(
88
+ validators = ::ValidatiousOnRails::ModelValidations.length_of(
76
89
  validation(:validates_length_of, :minimum => 2)
77
90
  )
78
- assert_equal 'length-minimum_2', validation[:class]
91
+ assert_validator_class 'length-minimum_2', validators
79
92
  end
80
93
 
81
94
  test "with :maximum" do
82
- validation = ::ValidatiousOnRails::ModelValidations.length_of(
95
+ validators = ::ValidatiousOnRails::ModelValidations.length_of(
83
96
  validation(:validates_length_of, :maximum => 10)
84
97
  )
85
- assert_equal 'length-maximum_10', validation[:class]
98
+ assert_validator_class 'length-maximum_10', validators
86
99
  end
87
100
 
88
101
  test "with :minimum + :maximum" do
89
- validation = ::ValidatiousOnRails::ModelValidations.length_of(
102
+ validators = ::ValidatiousOnRails::ModelValidations.length_of(
90
103
  validation(:validates_length_of, :minimum => 2, :maximum => 10)
91
104
  )
92
- assert_equal 'length-minimum_2 length-maximum_10', validation[:class]
105
+ assert_validator_class 'length-minimum_2 length-maximum_10', validators
93
106
  end
94
107
  end
95
108
 
96
109
  context "numericality_of" do
97
-
110
+
98
111
  context ":odd/:even" do
99
112
  test "with :odd only" do
100
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
113
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
101
114
  validation(:validates_numericality_of, :even => false, :odd => true)
102
115
  )
103
- assert_equal 'numericality-odd', validation[:class]
116
+ assert_validator_class 'numericality-odd', validators
104
117
  end
105
118
 
106
119
  test "with :even only" do
107
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
120
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
108
121
  validation(:validates_numericality_of, :even => true, :odd => false)
109
122
  )
110
- assert_equal 'numericality-even', validation[:class]
123
+ assert_validator_class 'numericality-even', validators
111
124
  end
112
125
 
113
126
  test "with :odd and :even" do
114
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
127
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
115
128
  validation(:validates_numericality_of, :even => true, :odd => true)
116
129
  )
117
- assert_equal '', validation[:class]
130
+ assert_validator_class '', validators
118
131
  end
119
132
 
120
133
  test "with neither :odd or :even" do
121
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
134
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
122
135
  validation(:validates_numericality_of)
123
136
  )
124
- assert_equal '', validation[:class]
137
+ assert_validator_class '', validators
125
138
  end
126
139
  end
127
-
128
140
 
129
141
  test "with :only_integer" do
130
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
142
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
131
143
  validation(:validates_numericality_of, :only_integer => true)
132
144
  )
133
- # Alt. more generic idea: assert_equal 'numericality-precision_0', validation[:class]
134
- assert_equal 'numericality-only_integer', validation[:class]
145
+ # Alt. more generic idea: assert_equal 'numericality-precision_0', validator.to_class
146
+ assert_validator_class 'numericality-only_integer', validators
135
147
  end
136
148
 
137
149
  test "with :greater_than" do
138
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
150
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
139
151
  validation(:validates_numericality_of, :greater_than => 2)
140
152
  )
141
- assert_equal 'numericality-greater-than_2', validation[:class]
153
+ assert_validator_class 'numericality-greater-than_2', validators
142
154
  end
143
155
 
144
156
  test "with :greater_than_or_equal_to" do
145
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
157
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
146
158
  validation(:validates_numericality_of, :greater_than_or_equal_to => 2)
147
159
  )
148
- assert_equal 'numericality-greater-than-or-equal-to_2', validation[:class]
160
+ assert_validator_class 'numericality-greater-than-or-equal-to_2', validators
149
161
  end
150
162
 
151
163
  test "with :equal_to" do
152
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
164
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
153
165
  validation(:validates_numericality_of, :equal_to => 2)
154
166
  )
155
- assert_equal 'numericality-equal-to_2', validation[:class]
167
+ assert_validator_class 'numericality-equal-to_2', validators
156
168
  end
157
169
 
158
170
  test "with :less_than" do
159
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
171
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
160
172
  validation(:validates_numericality_of, :less_than => 10)
161
173
  )
162
- assert_equal 'numericality-less-than_10', validation[:class]
174
+ assert_validator_class 'numericality-less-than_10', validators
163
175
  end
164
176
 
165
177
  test "with :less_than_or_equal_to" do
166
- validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
178
+ validators = ::ValidatiousOnRails::ModelValidations.numericality_of(
167
179
  validation(:validates_numericality_of, :less_than_or_equal_to => 10)
168
180
  )
169
- assert_equal 'numericality-less-than-or-equal-to_10', validation[:class]
181
+ assert_validator_class 'numericality-less-than-or-equal-to_10', validators
170
182
  end
171
183
  end
172
184
 
173
185
  test "presence_of" do
174
- validation = ::ValidatiousOnRails::ModelValidations.presence_of(validation(:validates_presence_of))
175
- assert_equal 'required', validation[:class]
186
+ validators = ::ValidatiousOnRails::ModelValidations.presence_of(validation(:validates_presence_of))
187
+ assert_validator_class 'presence', validators
176
188
  end
177
189
 
178
190
  test "uniqueness_of" do
@@ -187,4 +199,12 @@ class ModelValidationsTest < ::ActiveSupport::TestCase
187
199
  ::ActiveRecord::Reflection::MacroReflection.new(macro, :name, options, BogusItem.new)
188
200
  end
189
201
 
202
+ def assert_validator_class(expected, actual)
203
+ if expected.is_a?(::Regexp)
204
+ assert_match expected, [*actual].collect { |v| v.to_class }.join(' ')
205
+ else
206
+ assert_equal expected, [*actual].collect { |v| v.to_class }.join(' ')
207
+ end
208
+ end
209
+
190
210
  end
@@ -38,69 +38,69 @@ class FormHelperTest < ::ActionView::TestCase
38
38
 
39
39
  test "required :text_field" do
40
40
  # Using helper
41
- assert_has_class 'required', text_field(:bogus_item, :name)
42
- assert_has_class 'required some_other_class', text_field(:bogus_item, :name, :class => 'some_other_class')
41
+ assert_has_class 'presence', text_field(:bogus_item, :name)
42
+ assert_has_class 'presence some_other_class', text_field(:bogus_item, :name, :class => 'some_other_class')
43
43
 
44
44
  # Using builder
45
- assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
45
+ assert_has_class 'presence', form_for(@bogus_item, :url => '/bogus_items') { |f|
46
46
  concat f.text_field(:name)
47
47
  }
48
- assert_has_class 'required text', form_for(@bogus_item, :url => '/bogus_items') { |f|
48
+ assert_has_class 'presence text', form_for(@bogus_item, :url => '/bogus_items') { |f|
49
49
  concat f.text_field(:name, :class => 'text')
50
50
  }
51
51
  end
52
52
 
53
53
  test "required :password_field" do
54
54
  # Using helper
55
- assert_has_class 'required', password_field(:bogus_item, :name)
56
- assert_has_class 'required some_other_class', password_field(:bogus_item, :name, :class => 'some_other_class')
55
+ assert_has_class 'presence', password_field(:bogus_item, :name)
56
+ assert_has_class 'presence some_other_class', password_field(:bogus_item, :name, :class => 'some_other_class')
57
57
 
58
58
  # Using builder
59
- assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
59
+ assert_has_class 'presence', form_for(@bogus_item, :url => '/bogus_items') { |f|
60
60
  concat f.password_field(:name)
61
61
  }
62
- assert_has_class 'required some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
62
+ assert_has_class 'presence some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
63
63
  concat f.password_field(:name, :class => 'some_other_class')
64
64
  }
65
65
  end
66
66
 
67
67
  test "required :text_area" do
68
68
  # Using helper
69
- assert_has_class 'required', text_area(:bogus_item, :body)
70
- assert_has_class 'required some_other_class', text_area(:bogus_item, :body, :class => 'some_other_class')
69
+ assert_has_class 'presence', text_area(:bogus_item, :body)
70
+ assert_has_class 'presence some_other_class', text_area(:bogus_item, :body, :class => 'some_other_class')
71
71
 
72
72
  # Using builder
73
- assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
73
+ assert_has_class 'presence', form_for(@bogus_item, :url => '/bogus_items') { |f|
74
74
  concat f.text_area(:body)
75
75
  }
76
- assert_has_class 'required some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
76
+ assert_has_class 'presence some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
77
77
  concat f.text_area(:body, :class => 'some_other_class')
78
78
  }
79
79
  end
80
80
 
81
81
  test "required :check_box" do # a.k.a. "acceptance required"
82
82
  # Using helper
83
- assert_has_class 'required', check_box(:bogus_item, :signed)
84
- assert_has_class 'required some_other_class', check_box(:bogus_item, :signed, :class => 'some_other_class')
83
+ assert_has_class 'acceptance-accept_true', check_box(:bogus_item, :signed)
84
+ assert_has_class 'acceptance-accept_true some_other_class', check_box(:bogus_item, :signed, :class => 'some_other_class')
85
85
 
86
86
  # Using builder
87
- assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
87
+ assert_has_class 'acceptance-accept_true', form_for(@bogus_item, :url => '/bogus_items') { |f|
88
88
  concat f.check_box(:signed)
89
89
  }
90
- assert_has_class 'required some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
90
+ assert_has_class 'acceptance-accept_true some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
91
91
  concat f.check_box(:signed, :class => 'some_other_class')
92
92
  }
93
93
  end
94
94
 
95
95
  test "required :radio_button" do
96
96
  # Using helper
97
- assert_has_class 'required', radio_button(:bogus_item, :variant, 1)
98
- assert_has_class 'required some_other_class', radio_button(:bogus_item, :variant, 1, :class => 'some_other_class')
97
+ assert_has_class 'presence', radio_button(:bogus_item, :variant, 1)
98
+ assert_has_class 'presence some_other_class', radio_button(:bogus_item, :variant, 1, :class => 'some_other_class')
99
99
 
100
- assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
100
+ assert_has_class 'presence', form_for(@bogus_item, :url => '/bogus_items') { |f|
101
101
  concat f.radio_button(:variant, 1)
102
102
  }
103
- assert_has_class 'required some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
103
+ assert_has_class 'presence some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
104
104
  concat f.radio_button(:variant, 1, :class => 'some_other_class')
105
105
  }
106
106
  end
@@ -16,7 +16,7 @@ class ValidatorTest < ::ActiveSupport::TestCase
16
16
  end
17
17
  end
18
18
 
19
- test "creating an empty validator - and generate valid v2.Validator (using #to_s)" do
19
+ test "creating an empty validator - and generate valid v2.Validator and class call" do
20
20
  assert_equal 'dummie', @empty_validator.name
21
21
  assert_equal '', @empty_validator.message
22
22
  assert_equal ([]), @empty_validator.params
@@ -30,10 +30,12 @@ class ValidatorTest < ::ActiveSupport::TestCase
30
30
  name: "dummie"
31
31
  });'
32
32
 
33
- assert_equal expected_v2_validator.gsub(/[\n\s\t]/, ''), @empty_validator.to_s.gsub(/[\n\s\t]/, '')
33
+ assert_equal @custom_validator.name, @custom_validator.to_class
34
+ assert_equal "#{@custom_validator.name}_1_hello_2", @custom_validator.to_class(1, "hello", 2)
35
+ assert_equal expected_v2_validator.gsub(/[\n\s\t]/, ''), @empty_validator.to_js.gsub(/[\n\s\t]/, '')
34
36
  end
35
37
 
36
- test "creating a custom validator - and generate valid v2.Validator (using #to_s)" do
38
+ test "creating a custom validator - and generate valid v2.Validator and class call" do
37
39
  assert_equal 'dummie', @custom_validator.name
38
40
  assert_equal 'Fail, fail, fail!', @custom_validator.message
39
41
  assert_equal (["some", "params"]), @custom_validator.params
@@ -50,7 +52,9 @@ class ValidatorTest < ::ActiveSupport::TestCase
50
52
  params: ["some", "params"]
51
53
  });'
52
54
 
53
- assert_equal expected_v2_validator.gsub(/[\n\s\t]/, ''), @custom_validator.to_s.gsub(/[\n\s\t]/, '')
55
+ assert_equal @custom_validator.name, @custom_validator.to_class
56
+ assert_equal "#{@custom_validator.name}_1_hello_2", @custom_validator.to_class(1, "hello", 2)
57
+ assert_equal expected_v2_validator.gsub(/[\n\s\t]/, ''), @custom_validator.to_js.gsub(/[\n\s\t]/, '')
54
58
  end
55
59
 
56
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validatious-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Johansen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-10-06 00:00:00 +02:00
13
+ date: 2009-10-08 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -35,6 +35,8 @@ extra_rdoc_files:
35
35
  - lib/validatious-on-rails/validatious/remote_validator.rb
36
36
  - lib/validatious-on-rails/validatious/validator.rb
37
37
  - lib/validatious-on-rails/validatious/validators.rb
38
+ - lib/validatious-on-rails/validatious/validators/acceptance_validator.rb
39
+ - lib/validatious-on-rails/validatious/validators/confirmation_validator.rb
38
40
  - lib/validatious-on-rails/validatious/validators/exclusion_validator.rb
39
41
  - lib/validatious-on-rails/validatious/validators/format_validator.rb
40
42
  - lib/validatious-on-rails/validatious/validators/inclusion_validator.rb
@@ -49,6 +51,7 @@ extra_rdoc_files:
49
51
  - lib/validatious-on-rails/validatious/validators/numericality/less_than_validator.rb
50
52
  - lib/validatious-on-rails/validatious/validators/numericality/odd_validator.rb
51
53
  - lib/validatious-on-rails/validatious/validators/numericality/only_integer_validator.rb
54
+ - lib/validatious-on-rails/validatious/validators/presence_validator.rb
52
55
  - rails/init.rb
53
56
  - test/test_helper.rb
54
57
  - test/validatious_on_rails/helpers_test.rb
@@ -71,6 +74,8 @@ files:
71
74
  - lib/validatious-on-rails/validatious/remote_validator.rb
72
75
  - lib/validatious-on-rails/validatious/validator.rb
73
76
  - lib/validatious-on-rails/validatious/validators.rb
77
+ - lib/validatious-on-rails/validatious/validators/acceptance_validator.rb
78
+ - lib/validatious-on-rails/validatious/validators/confirmation_validator.rb
74
79
  - lib/validatious-on-rails/validatious/validators/exclusion_validator.rb
75
80
  - lib/validatious-on-rails/validatious/validators/format_validator.rb
76
81
  - lib/validatious-on-rails/validatious/validators/inclusion_validator.rb
@@ -85,6 +90,7 @@ files:
85
90
  - lib/validatious-on-rails/validatious/validators/numericality/less_than_validator.rb
86
91
  - lib/validatious-on-rails/validatious/validators/numericality/odd_validator.rb
87
92
  - lib/validatious-on-rails/validatious/validators/numericality/only_integer_validator.rb
93
+ - lib/validatious-on-rails/validatious/validators/presence_validator.rb
88
94
  - rails/init.rb
89
95
  - test/test_helper.rb
90
96
  - test/validatious_on_rails/helpers_test.rb
@@ -116,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
122
  requirements: []
117
123
 
118
124
  rubyforge_project:
119
- rubygems_version: 1.3.3
125
+ rubygems_version: 1.3.5
120
126
  signing_key:
121
127
  specification_version: 3
122
128
  summary: Rails plugin that maps model validations to class names on form elements to integrate with Validatious.