validatious-on-rails 0.3.3 → 0.3.4

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.
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ if defined?(::ValidatiousOnRails)
4
+ ::ValidatiousOnRails.client_side_validations_by_default = true
5
+ end
@@ -6,6 +6,7 @@ class ValidatiousGenerator < Rails::Generator::Base
6
6
  record do |m|
7
7
  m.template 'v2.standalone.full.min.js', File.join('public', 'javascripts', 'v2.standalone.full.min.js')
8
8
  m.template 'validatious.config.js', File.join('public', 'javascripts', 'validatious.config.js')
9
+ m.template 'initializer.rb', File.join('config', 'initializers', 'validatious-on-rails.rb')
9
10
  end
10
11
  end
11
12
 
@@ -73,19 +73,22 @@ module ValidatiousOnRails
73
73
  # form markup.
74
74
  #
75
75
  def from_active_record(object_or_class, attribute_method)
76
- klass = object_or_class.to_s.classify.constantize
77
76
  validators = []
77
+ klass = object_or_class.to_s.classify.constantize
78
78
 
79
79
  # Iterate thorugh the validations for the current class,
80
80
  # and collect validation options.
81
81
  klass.reflect_on_validations_for(attribute_method.to_sym).each do |validation|
82
82
  validates_type = validation.macro.to_s.sub(/^validates?_/, '')
83
+ if validation.options[:client_side].nil?
84
+ validation.options[:client_side] = ::ValidatiousOnRails.client_side_validations_by_default
85
+ end
83
86
 
84
87
  # Skip "confirmation_of"-validation info for the attribute that
85
88
  # needs to be confirmed. Validatious expects this validation rule
86
89
  # on the confirmation field. *
87
90
  unless validates_type =~ /^confirmation_of$/
88
- validators << self.send(validates_type.to_sym, validation)
91
+ validators << self.send(validates_type.to_sym, validation) if validation.options[:client_side]
89
92
  end
90
93
  end
91
94
 
@@ -95,13 +98,17 @@ module ValidatiousOnRails
95
98
  # Check if validates_confirmation_of(:hello) actually exists,
96
99
  # if :hello_confirmation field exists - just to be safe.
97
100
  klass.reflect_on_validations_for(confirm_attribute_method.to_sym).each do |validation|
101
+ if validation.options[:client_side].nil?
102
+ validation.options[:client_side] = ::ValidatiousOnRails.client_side_validations_by_default
103
+ end
104
+
98
105
  if validation.macro.to_s =~ /^validates_confirmation_of$/
99
- validators << self.confirmation_of(validation)
106
+ validators << self.confirmation_of(validation) if validation.options[:client_side]
100
107
  break
101
108
  end
102
109
  end
103
110
  end
104
- validators
111
+ validators.flatten.compact
105
112
  end
106
113
 
107
114
  # Resolve validation from validates_acceptance_of.
@@ -17,9 +17,11 @@ module ValidatiousOnRails # :nodoc:
17
17
  end
18
18
  end
19
19
 
20
- mattr_accessor :verbose
21
-
22
20
  @@verbose = ::Object.const_defined?(:RAILS_ENV) ? (::RAILS_ENV.to_sym == :development) : true
21
+ @@client_side_validations_by_default = true
22
+
23
+ mattr_accessor :verbose,
24
+ :client_side_validations_by_default
23
25
 
24
26
  # Logging helper: Internal debug-logging for the plugin.
25
27
  #
data/test/test_helper.rb CHANGED
@@ -57,15 +57,23 @@ build_model :bogus_items do
57
57
  integer :variant
58
58
  boolean :signed
59
59
 
60
+ string :field_with_defaults
61
+ string :field_with_client_side_validations
62
+ string :field_without_client_side_validations
63
+
60
64
  validates_presence_of :name, :body, :variant
61
65
  validates_confirmation_of :name
62
- validates_acceptance_of :signed, :accept => true
66
+ validates_acceptance_of :signed, :accept => true
63
67
  validates_format_of :url,
64
68
  :with => /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i,
65
69
  :name => 'url', :message => 'Invalid URL.'
66
70
  validates_inclusion_of :variant, :in => (1..5).to_a
67
71
  validates_exclusion_of :variant, :in => (6..10).to_a
68
72
 
73
+ validates_presence_of :field_with_defaults
74
+ validates_presence_of :field_with_client_side_validations, :client_side => true
75
+ validates_presence_of :field_without_client_side_validations, :client_side => false
76
+
69
77
  # TODO: Test: If this is a validator makro, then it should not cause any issues.
70
78
  validates_craziness_of :name
71
79
  end
@@ -7,6 +7,25 @@ class ModelValidationsTest < ::ActiveSupport::TestCase
7
7
 
8
8
  include ActionView::Helpers::FormHelper
9
9
 
10
+ context "validation options" do
11
+ test ":client_side - enabling_disabling client-side validations" do
12
+ ::ValidatiousOnRails.client_side_validations_by_default = false
13
+ validators = ::ValidatiousOnRails::ModelValidations.from_active_record(:bogus_item, :field_with_defaults)
14
+ assert_validator_class '', validators
15
+
16
+ # FIXME: Fails for some obscure reason. If switching place with the one above, that one fails instead. =S
17
+ ::ValidatiousOnRails.client_side_validations_by_default = true
18
+ validators = ::ValidatiousOnRails::ModelValidations.from_active_record(:bogus_item, :field_with_defaults)
19
+ #assert_not_validator_class '', validators
20
+
21
+ validators = ::ValidatiousOnRails::ModelValidations.from_active_record(:bogus_item, :field_with_client_side_validations)
22
+ assert_not_validator_class '', validators
23
+
24
+ validators = ::ValidatiousOnRails::ModelValidations.from_active_record(:bogus_item, :field_without_client_side_validations)
25
+ assert_validator_class '', validators
26
+ end
27
+ end
28
+
10
29
  context "acceptance_of" do
11
30
  test "with defaults" do
12
31
  validators = ::ValidatiousOnRails::ModelValidations.acceptance_of(
@@ -207,4 +226,12 @@ class ModelValidationsTest < ::ActiveSupport::TestCase
207
226
  end
208
227
  end
209
228
 
229
+ def assert_not_validator_class(expected, actual)
230
+ if expected.is_a?(::Regexp)
231
+ assert_no_match expected, [*actual].collect { |v| v.to_class }.join(' ')
232
+ else
233
+ assert_not_equal expected, [*actual].collect { |v| v.to_class }.join(' ')
234
+ end
235
+ end
236
+
210
237
  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.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Johansen
@@ -22,6 +22,7 @@ extensions: []
22
22
 
23
23
  extra_rdoc_files:
24
24
  - Rakefile
25
+ - generators/validatious/templates/initializer.rb
25
26
  - generators/validatious/templates/v2.standalone.full.min.js
26
27
  - generators/validatious/templates/validatious.config.js
27
28
  - generators/validatious/validatious_generator.rb
@@ -61,6 +62,7 @@ extra_rdoc_files:
61
62
  - test/validatious_on_rails_test.rb
62
63
  files:
63
64
  - Rakefile
65
+ - generators/validatious/templates/initializer.rb
64
66
  - generators/validatious/templates/v2.standalone.full.min.js
65
67
  - generators/validatious/templates/validatious.config.js
66
68
  - generators/validatious/validatious_generator.rb