validatious-on-rails 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ module ValidatiousOnRails
12
12
  def attach_validator_for(object_name, method, options = {})
13
13
  options = ::ValidatiousOnRails::ModelValidations.options_for(object_name, method, options, @content_for_validatious)
14
14
  custom_js = options.delete(:js)
15
- content_for :validatious, custom_js if custom_js.present?
15
+ content_for :validatious, custom_js << "\n" if custom_js.present?
16
16
  options
17
17
  end
18
18
 
@@ -52,7 +52,7 @@ module ValidatiousOnRails
52
52
  validator_classes << v.to_class
53
53
  end
54
54
  classes = validator_classes.compact.join(' ').strip
55
- js = validator_js.compact.join(' ').strip
55
+ js = validator_js.compact.join("\n").strip
56
56
  options.merge!(:class => (classes unless classes.blank?), :js => (js unless js.blank?))
57
57
  end
58
58
 
@@ -12,7 +12,7 @@ module ValidatiousOnRails
12
12
  # Override default Validator-fn, with default a ClientSideValidator-fn.
13
13
  #
14
14
  def fn
15
- (@fn ||= "function(field, value, params) {return true;}").gsub(/[\n\t]/, '')
15
+ self.class.truncate_whitespace(@fn ||= "function(field, value, params){return true;}")
16
16
  end
17
17
 
18
18
  end
@@ -17,11 +17,11 @@ module ValidatiousOnRails
17
17
  # 2. Always return true, callback-function should perform the actual client side validation.
18
18
  #
19
19
  def fn
20
- (@fn ||= %{
20
+ self.class.truncate_whitespace(@fn ||= %{
21
21
  function(field, value, params) {
22
22
  return !!v2.Rails.performRemoteValidation('#{self.name}', field, value, params, '#{self.message}');
23
23
  }
24
- }).gsub(/[\n\t]/, '')
24
+ })
25
25
  end
26
26
 
27
27
  class << self
@@ -35,23 +35,23 @@ module ValidatiousOnRails
35
35
  def perform_validation(record, attribute_name, value, params = {})
36
36
  return true if record.blank?
37
37
  record.send :"#{attribute_name}=", value
38
-
38
+
39
39
  if record.valid?
40
40
  ValidatiousOnRails.log "Validation: SUCCESS"
41
41
  true
42
42
  else
43
43
  return true if record.errors[attribute_name.to_sym].blank?
44
-
44
+
45
45
  # TODO: Refactor this when "the better" namin convention is used (see TODO).
46
46
  validation_macro = ("validates_%s" % self.name.split('::').last.underscore.gsub(/_validator$/, ''))
47
47
  validation = record.class.reflect_on_validations_for(attribute_name.to_sym).select { |v|
48
48
  v.macro.to_s == validation_macro || v.macro.to_s == "#{validation_macro}_of"
49
49
  }.first
50
50
  return true if validation.blank?
51
-
51
+
52
52
  # {{variable}} => .*
53
53
  validation_error_message = self.new(validation).message.gsub(/\{\{.*\}\}/, '.*')
54
-
54
+
55
55
  # Ugly, but probably the only way (?) to identify a certain error without open
56
56
  # up rails core validation methods - not scalable.
57
57
  is_invalid = record.errors[attribute_name.to_sym].any? do |error_message|
@@ -122,7 +122,7 @@ module ValidatiousOnRails
122
122
  value ||= ''
123
123
  # If no function specified yet, always validate true by default.
124
124
  value << "\nreturn true;" unless value =~ /return (.+)/i
125
- "function(field, value, params) {#{value}}"
125
+ "function(field, value, params){#{value}}"
126
126
  end
127
127
  end
128
128
 
@@ -140,8 +140,8 @@ module ValidatiousOnRails
140
140
  js_options = options.keys.collect(&:to_s).sort.collect { |k|
141
141
  v = options[k.to_sym]
142
142
  ("#{k}: #{k.to_sym == :fn ? v : v.to_json}" if [false, true].include?(v) || v.present?)
143
- }.compact.join(', ')
144
- "v2.Validator.add({#{js_options}});"
143
+ }.compact.join(',')
144
+ self.class.truncate_whitespace("v2.Validator.add({#{js_options}});")
145
145
  end
146
146
  alias :to_s :to_js
147
147
 
@@ -155,10 +155,14 @@ module ValidatiousOnRails
155
155
 
156
156
  class << self
157
157
 
158
- def validate_blank(validation)
158
+ def truncate_whitespace(string)
159
+ string.gsub(/[\n]+[\s]+/, '')
160
+ end
161
+
162
+ def validate_blank(allow_blank)
159
163
  %{
160
- var isBlank = /^[\s\t\n]*$/.test(value);
161
- if (#{validation.options[:allow_blank] == true} && isBlank) {
164
+ var isBlank = /^[#{'\s\t\n'}]*$/.test(value);
165
+ if (#{allow_blank == true} && isBlank) {
162
166
  return true;
163
167
  };
164
168
  }
@@ -8,7 +8,7 @@ module ValidatiousOnRails
8
8
  def initialize(validation, options = {})
9
9
  name = 'acceptance-accept'
10
10
  super name, options
11
- self.message = self.class.generate_message(validation)
11
+ self.message = self.class.generate_message(validation, :key => :accepted)
12
12
  self.accept_empty = validation.options[:allow_nil]
13
13
  self.fn = %{
14
14
  var accept_value = params[0] + '';
@@ -13,7 +13,7 @@ module ValidatiousOnRails
13
13
  self.message = self.class.generate_message(validation)
14
14
  self.accept_empty = validation.options[:allow_nil]
15
15
  self.fn = %{
16
- #{self.class.validate_blank(validation)}
16
+ #{self.class.validate_blank(validation.options[:allow_blank])}
17
17
  var exclusion_values = #{validation.options[:in].to_json};
18
18
  for (var i = 0; i < exclusion_values.length; i++) {
19
19
  if (exclusion_values[i] == value) { return false; }
@@ -22,7 +22,7 @@ module ValidatiousOnRails
22
22
  self.message = self.class.generate_message(validation, :key => :invalid)
23
23
  self.accept_empty = validation.options[:allow_nil]
24
24
  self.fn = %{
25
- #{self.class.validate_blank(validation)}
25
+ #{self.class.validate_blank(validation.options[:allow_blank])}
26
26
  return #{validation.options[:with].inspect.gsub(/\\A/, '^').gsub(/\\z/, '$')}.test(value);
27
27
  }
28
28
  self.fn.freeze
@@ -13,7 +13,7 @@ module ValidatiousOnRails
13
13
  self.message = self.class.generate_message(validation)
14
14
  self.accept_empty = validation.options[:allow_nil]
15
15
  self.fn = %{
16
- #{self.class.validate_blank(validation)}
16
+ #{self.class.validate_blank(validation.options[:allow_blank])}
17
17
  var inclusion_values = #{validation.options[:in].to_json};
18
18
  for (var i = 0; i < inclusion_values.length; i++) {
19
19
  if (inclusion_values[i] == value) { return true; }
@@ -14,7 +14,7 @@ module ValidatiousOnRails
14
14
  self.message = self.class.generate_message(validation, :key => :wrong_length, :count => '{{count}}')
15
15
  self.accept_empty = validation.options[:allow_nil]
16
16
  self.fn = %{
17
- #{self.class.validate_blank(validation)}
17
+ #{self.class.validate_blank(validation.options[:allow_blank])}
18
18
  value += '';
19
19
  return value == params[0];
20
20
  }
@@ -14,7 +14,7 @@ module ValidatiousOnRails
14
14
  self.message = self.class.generate_message(validation, :key => :too_long, :count => '{{count}}')
15
15
  self.accept_empty = validation.options[:allow_nil]
16
16
  self.fn = %{
17
- #{self.class.validate_blank(validation)}
17
+ #{self.class.validate_blank(validation.options[:allow_blank])}
18
18
  value += '';
19
19
  return value.length <= params[0];
20
20
  }
@@ -14,7 +14,7 @@ module ValidatiousOnRails
14
14
  self.message = self.class.generate_message(validation, :key => :too_short, :count => '{{count}}')
15
15
  self.accept_empty = validation.options[:allow_nil]
16
16
  self.fn = %{
17
- #{self.class.validate_blank(validation)}
17
+ #{self.class.validate_blank(validation.options[:allow_blank])}
18
18
  value += '';
19
19
  return value.length >= params[0];
20
20
  }
@@ -12,7 +12,7 @@ module ValidatiousOnRails
12
12
  self.accept_empty = false
13
13
  # Identical to Validatious "required" validator, but we want Rails I18n message support, so...
14
14
  self.fn = %{
15
- return !v2.empty(value) && !(typeof value.length !== 'undefined' && value.length === 0);
15
+ return !v2.empty(value) && !(typeof value.length !== 'undefined' && value.length === 0) && !/^[#{'\s\t\n'}]*$/.test(value);
16
16
  }
17
17
  self.fn.freeze
18
18
  end
@@ -224,7 +224,7 @@ class FormHelperTest < ::ActionView::TestCase
224
224
  assert_has_class 'url some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
225
225
  concat f.text_field(:url, :class => 'some_other_class')
226
226
  }
227
- assert_match /v2.Validator\.add\(.*\{.*"url"/, @content_for_validatious
227
+ assert_match /v2.Validator\.add\(\{.*\"url\"/m, @content_for_validatious
228
228
  end
229
229
 
230
230
  # TODO: The other validators...
@@ -22,17 +22,17 @@ class ValidatorTest < ::ActiveSupport::TestCase
22
22
  assert_equal ([]), @empty_validator.params
23
23
  assert_equal ([]), @empty_validator.aliases
24
24
  assert_equal true, @empty_validator.accept_empty
25
- assert_equal "function(field, value, params) {return true;}", @empty_validator.fn.gsub(/\n/, '')
25
+ assert_equal "function(field, value, params){return true;}", @empty_validator.fn
26
26
 
27
27
  expected_v2_validator = 'v2.Validator.add({
28
28
  acceptEmpty: true,
29
- fn: function(field, value, params) {return true;},
29
+ fn: function(field, value, params){return true;},
30
30
  name: "dummie"
31
31
  });'
32
32
 
33
33
  assert_equal @custom_validator.name, @custom_validator.to_class
34
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]/, '')
35
+ assert_equal ::ValidatiousOnRails::Validatious::Validator.truncate_whitespace(expected_v2_validator), @empty_validator.to_js
36
36
  end
37
37
 
38
38
  test "creating a custom validator - and generate valid v2.Validator and class call" do
@@ -41,25 +41,25 @@ class ValidatorTest < ::ActiveSupport::TestCase
41
41
  assert_equal (["some", "params"]), @custom_validator.params
42
42
  assert_equal (["some", "aliases"]), @custom_validator.aliases
43
43
  assert_equal false, @custom_validator.accept_empty
44
- assert_equal "function(field, value, params) {return false;}", @custom_validator.fn.gsub(/\n/, '')
44
+ assert_equal "function(field, value, params){return false;}", @custom_validator.fn
45
45
 
46
46
  expected_v2_validator = 'v2.Validator.add({
47
47
  acceptEmpty: false,
48
- aliases: ["some", "aliases"],
49
- fn: function(field, value, params) {return false;},
48
+ aliases: ["some","aliases"],
49
+ fn: function(field, value, params){return false;},
50
50
  message: "Fail, fail, fail!",
51
51
  name: "dummie",
52
- params: ["some", "params"]
52
+ params: ["some","params"]
53
53
  });'
54
54
 
55
55
  assert_equal @custom_validator.name, @custom_validator.to_class
56
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]/, '')
57
+ assert_equal ::ValidatiousOnRails::Validatious::Validator.truncate_whitespace(expected_v2_validator), @custom_validator.to_js
58
58
  end
59
59
 
60
60
  context "Message" do
61
61
  test "I18n lookup" do
62
- validator_klass = ValidatiousOnRails::Validatious::Validator
62
+ validator_klass = ::ValidatiousOnRails::Validatious::Validator
63
63
  # For some reason can't raise this in tests. =S
64
64
  # assert_raise(::I18n::MissingInterpolationArgument) {
65
65
  # validator_klass.generate_message :key => :too_short
@@ -70,4 +70,22 @@ class ValidatorTest < ::ActiveSupport::TestCase
70
70
  end
71
71
  end
72
72
 
73
+ context "JavaScript" do
74
+ test "truncation of whitespace characters" do
75
+ duck_validator = ::ValidatiousOnRails::Validatious::ClientSideValidator.new('duck')
76
+ # Well... =)
77
+ duck_validator.fn = %{
78
+ var duck_says = "Quack!";
79
+
80
+ if (duck_says != "Quack!") {
81
+ return (duck_says == "Quack! ${{interpolation}} Quack!");
82
+ }
83
+
84
+ return true;
85
+ }
86
+ compact_fn = %{function(field, value, params){var duck_says = "Quack!";if (duck_says != "Quack!") {return (duck_says == "Quack! ${{interpolation}} Quack!");}return true;}}
87
+ assert_equal(compact_fn, duck_validator.fn)
88
+ end
89
+ end
90
+
73
91
  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.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Grimfelt
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-10-23 00:00:00 +02:00
13
+ date: 2009-10-26 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -46,7 +46,6 @@ extra_rdoc_files:
46
46
  - lib/validatious-on-rails/validatious/validators/exclusion_validator.rb
47
47
  - lib/validatious-on-rails/validatious/validators/format_validator.rb
48
48
  - lib/validatious-on-rails/validatious/validators/inclusion_validator.rb
49
- - lib/validatious-on-rails/validatious/validators/invalid_validator.rb
50
49
  - lib/validatious-on-rails/validatious/validators/length/is_validator.rb
51
50
  - lib/validatious-on-rails/validatious/validators/length/maximum_validator.rb
52
51
  - lib/validatious-on-rails/validatious/validators/length/minimum_validator.rb
@@ -59,7 +58,6 @@ extra_rdoc_files:
59
58
  - lib/validatious-on-rails/validatious/validators/numericality/odd_validator.rb
60
59
  - lib/validatious-on-rails/validatious/validators/numericality/only_integer_validator.rb
61
60
  - lib/validatious-on-rails/validatious/validators/presence_validator.rb
62
- - lib/validatious-on-rails/validatious/validators/remote_client_validator.rb
63
61
  - lib/validatious-on-rails/validatious/validators/uniqueness_validator.rb
64
62
  - rails/init.rb
65
63
  - test/test_helper.rb
@@ -95,7 +93,6 @@ files:
95
93
  - lib/validatious-on-rails/validatious/validators/exclusion_validator.rb
96
94
  - lib/validatious-on-rails/validatious/validators/format_validator.rb
97
95
  - lib/validatious-on-rails/validatious/validators/inclusion_validator.rb
98
- - lib/validatious-on-rails/validatious/validators/invalid_validator.rb
99
96
  - lib/validatious-on-rails/validatious/validators/length/is_validator.rb
100
97
  - lib/validatious-on-rails/validatious/validators/length/maximum_validator.rb
101
98
  - lib/validatious-on-rails/validatious/validators/length/minimum_validator.rb
@@ -108,7 +105,6 @@ files:
108
105
  - lib/validatious-on-rails/validatious/validators/numericality/odd_validator.rb
109
106
  - lib/validatious-on-rails/validatious/validators/numericality/only_integer_validator.rb
110
107
  - lib/validatious-on-rails/validatious/validators/presence_validator.rb
111
- - lib/validatious-on-rails/validatious/validators/remote_client_validator.rb
112
108
  - lib/validatious-on-rails/validatious/validators/uniqueness_validator.rb
113
109
  - rails/init.rb
114
110
  - test/test_helper.rb
@@ -1,19 +0,0 @@
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 RemoteClientValidator < ClientSideValidator
7
-
8
- def initialize(validation, options = {})
9
- super 'remote-client', options
10
- self.message = self.class.generate_message(validation)
11
- self.accept_empty = false
12
- self.fn = %{
13
- return !!params[0];
14
- }
15
- end
16
-
17
- end
18
- end
19
- end
@@ -1,19 +0,0 @@
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 RemoteClientValidator < ClientSideValidator
7
-
8
- def initialize(validation, options = {})
9
- super 'remote-client', options
10
- self.message = self.class.generate_message(validation)
11
- self.accept_empty = false
12
- self.fn = %{
13
- return !!params[0];
14
- }
15
- end
16
-
17
- end
18
- end
19
- end