client_side_validations 2.5.2 → 2.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,6 +16,7 @@ Currently the following validations are supported:
16
16
  * validates_format_of
17
17
  * validates_numericality_of
18
18
  * validates_length_of
19
+ * validates_size_of
19
20
  * validates_uniqueness_of
20
21
  * validates_confirmation_of
21
22
  * validatse_acceptance_of
@@ -81,30 +81,33 @@ ClientSideValidations = function(id, adapter, object_id) {
81
81
  messages[name] = {};
82
82
  for(var validation in this.validations[attr]) {
83
83
  rule = null;
84
+ required = false;
84
85
  switch(validation) {
85
86
  case 'presence':
86
- rule = 'required'
87
+ rule = 'required';
87
88
  value = true;
88
89
  break;
89
90
  case 'format':
90
- value = this.validations[attr][validation]['with'];
91
+ value = this.validations[attr][validation]['with'];
92
+ required = !this.validations[attr][validation]['allow_blank'];
91
93
  break;
92
94
  case 'numericality':
93
- rule = 'digits';
94
- value = true;
95
+ rule = 'digits';
96
+ value = true;
97
+ required = !this.validations[attr][validation]['allow_blank'];
95
98
  break;
96
99
  case 'length':
97
100
  if ('minimum' in this.validations[attr][validation] && 'maximum' in this.validations[attr][validation]) {
98
- minrule = 'minlength';
99
- rules[name][minrule] = this.validations[attr][validation]['minimum'];
100
- messages[name][minrule] = this.validations[attr][validation]['message_min'];
101
-
102
- rule = 'maxlength';
103
- value = this.validations[attr][validation]['maximum'];
104
- this.validations[attr][validation]['message'] = this.validations[attr][validation]['message_max']
101
+ maxrule = 'maxlength';
102
+ rules[name][maxrule] = this.validations[attr][validation]['maximum'];
103
+ messages[name][maxrule] = this.validations[attr][validation]['message_max'];
104
+
105
+ rule = 'minlength';
106
+ value = this.validations[attr][validation]['minimum'];
107
+ this.validations[attr][validation]['message'] = this.validations[attr][validation]['message_min']
105
108
  } else if('minimum' in this.validations[attr][validation]) {
106
- rule = 'minlength';
107
- value = this.validations[attr][validation]['minimum'];
109
+ rule = 'minlength';
110
+ value = this.validations[attr][validation]['minimum'];
108
111
  } else if('maximum' in this.validations[attr][validation]) {
109
112
  rule = 'maxlength';
110
113
  value = this.validations[attr][validation]['maximum'];
@@ -121,6 +124,7 @@ ClientSideValidations = function(id, adapter, object_id) {
121
124
  return String(object_id);
122
125
  }
123
126
  }
127
+ required = !this.validations[attr][validation]['allow_blank'];
124
128
  break;
125
129
  case 'confirmation':
126
130
  rule = 'equalTo';
@@ -131,12 +135,14 @@ ClientSideValidations = function(id, adapter, object_id) {
131
135
  value = true;
132
136
  break;
133
137
  case 'inclusion':
134
- rule = 'inclusion';
135
- value = this.validations[attr][validation]['in']
138
+ rule = 'inclusion';
139
+ value = this.validations[attr][validation]['in']
140
+ required = !this.validations[attr][validation]['allow_blank'];
136
141
  break;
137
142
  case 'exclusion':
138
- rule = 'exclusion';
139
- value = this.validations[attr][validation]['in']
143
+ rule = 'exclusion';
144
+ value = this.validations[attr][validation]['in']
145
+ required = !this.validations[attr][validation]['allow_blank'];
140
146
  break;
141
147
 
142
148
  default:
@@ -144,8 +150,15 @@ ClientSideValidations = function(id, adapter, object_id) {
144
150
  if(rule == null) {
145
151
  rule = validation;
146
152
  }
147
- rules[name][rule] = value;
148
- messages[name][rule] = this.validations[attr][validation]['message'];
153
+
154
+ if (typeof(value) != 'undefined') {
155
+ rules[name][rule] = value;
156
+ messages[name][rule] = this.validations[attr][validation]['message'];
157
+ }
158
+ if (required && rules[name]['required'] == null) {
159
+ rules[name].required = true;
160
+ messages[name].required = this.validations[attr][validation]['message'];
161
+ }
149
162
  }
150
163
  }
151
164
 
@@ -21,6 +21,19 @@ module DNCLabs
21
21
  if validation.kind == :inclusion || validation.kind == :exclusion
22
22
  validation.options[:in] = validation.options[:in].to_a
23
23
  end
24
+
25
+ if validation.kind == :size
26
+ validation.kind = :length
27
+ end
28
+
29
+ if validation.kind == :length &&
30
+ (range = (validation.options.delete(:within) || validation.options.delete(:in)))
31
+ validation.options[:minimum] = range.first
32
+ validation.options[:maximum] = range.last
33
+ elsif validation.kind == :inclusion || validation.kind == :exclusion
34
+ validation.options[:in] = validation.options[:in].to_a
35
+ end
36
+
24
37
  super
25
38
  end
26
39
 
@@ -31,17 +44,17 @@ module DNCLabs
31
44
  def get_validation_message(validation)
32
45
  default = case validation.kind
33
46
  when :presence
34
- I18n.translate('errors.messages.blank')
47
+ I18n.translate(i18n_prefix + 'errors.messages.blank')
35
48
  when :format
36
- I18n.translate('errors.messages.invalid')
49
+ I18n.translate(i18n_prefix + 'errors.messages.invalid')
37
50
  when :length
38
51
  if count = validation.options[:minimum]
39
- I18n.translate('errors.messages.too_short').sub('%{count}', count.to_s)
52
+ I18n.translate(i18n_prefix + 'errors.messages.too_short').sub(orm_error_interpolation(:count), count.to_s)
40
53
  elsif count = validation.options[:maximum]
41
- I18n.translate('errors.messages.too_long').sub('%{count}', count.to_s)
54
+ I18n.translate(i18n_prefix + 'errors.messages.too_long').sub(orm_error_interpolation(:count), count.to_s)
42
55
  end
43
56
  when :numericality
44
- I18n.translate('errors.messages.not_a_number')
57
+ I18n.translate(i18n_prefix + 'errors.messages.not_a_number')
45
58
  when :uniqueness
46
59
  if defined?(ActiveRecord) && base.kind_of?(ActiveRecord::Base)
47
60
  I18n.translate('activerecord.errors.messages.taken')
@@ -49,20 +62,20 @@ module DNCLabs
49
62
  I18n.translate('errors.messages.taken')
50
63
  end
51
64
  when :confirmation
52
- I18n.translate('errors.messages.confirmation')
65
+ I18n.translate(i18n_prefix + 'errors.messages.confirmation')
53
66
  when :acceptance
54
- I18n.translate('errors.messages.accepted')
67
+ I18n.translate(i18n_prefix + 'errors.messages.accepted')
55
68
  when :inclusion
56
- I18n.translate('errors.messages.inclusion')
69
+ I18n.translate(i18n_prefix + 'errors.messages.inclusion')
57
70
  when :exclusion
58
- I18n.translate('errors.messages.exclusion')
71
+ I18n.translate(i18n_prefix + 'errors.messages.exclusion')
59
72
  end
60
73
 
61
74
  message = validation.options.delete(:message)
62
75
  if message.kind_of?(String)
63
76
  message
64
77
  elsif message.kind_of?(Symbol)
65
- I18n.translate("errors.models.#{base.class.to_s.downcase}.attributes.#{validation.attributes.first}.#{message}")
78
+ I18n.translate(i18n_prefix + "errors.models.#{base.class.to_s.downcase}.attributes.#{validation.attributes.first}.#{message}")
66
79
  else
67
80
  default
68
81
  end
@@ -72,6 +85,23 @@ module DNCLabs
72
85
  validation.kind.to_s
73
86
  end
74
87
 
88
+ def i18n_prefix
89
+ if defined?(::ActiveModel)
90
+ ''
91
+ else # ActiveRecord 2.x
92
+ 'activerecord.'
93
+ end
94
+ end
95
+
96
+ def orm_error_interpolation(name)
97
+ if defined?(::ActiveModel)
98
+ "%{#{name}}"
99
+
100
+ else # ActiveRecord 2.x
101
+ "{{#{name}}}"
102
+ end
103
+ end
104
+
75
105
  end
76
106
  end
77
107
  end
@@ -54,9 +54,9 @@ module DNCLabs
54
54
  (on == :save) ||
55
55
  (on == :create && base.new_record?) ||
56
56
  (on == :update && !base.new_record?)
57
- elsif if_condition = validation.options[:if]
57
+ elsif(if_condition = (validation.options[:if] || validation.options[:allow_validation]))
58
58
  base.instance_eval(if_condition.to_s)
59
- elsif unless_condition = validation.options[:unless]
59
+ elsif(unless_condition = (validation.options[:unless] || validation.options[:skip_validation]))
60
60
  !base.instance_eval(unless_condition.to_s)
61
61
  else
62
62
  true
@@ -70,7 +70,7 @@ module DNCLabs
70
70
  def get_validation_options(validation)
71
71
  options = validation.options.clone
72
72
  options.symbolize_keys!
73
- deleteable_keys = [:on, :tokenizer, :only_integer, :allow_nil, :case_sensitive, :accept, :if, :unless]
73
+ deleteable_keys = [:on, :tokenizer, :only_integer, :allow_nil, :case_sensitive, :accept, :if, :unless, :allow_validation]
74
74
  options.delete(:maximum) if options.has_key?(:minimum)
75
75
  options.delete_if { |k, v| deleteable_keys.include?(k) }
76
76
  if options[:with].kind_of?(Regexp)
@@ -25,16 +25,22 @@ module DNCLabs
25
25
  end
26
26
  end
27
27
 
28
- if defined?(ActiveModel)
28
+ if defined?(::ActiveModel)
29
29
  require 'client_side_validations/adapters/active_model'
30
30
  DNCLabs::ClientSideValidations::Adapter = DNCLabs::ClientSideValidations::Adapters::ActiveModel
31
- klass = ActiveModel::Validations
31
+ klass = ::ActiveModel::Validations
32
32
 
33
- elsif defined?(ActiveRecord)
34
- if ActiveRecord::VERSION::MAJOR == 2
35
- require 'client_side_validations/adapters/active_record_2'
36
- DNCLabs::ClientSideValidations::Adapter = DNCLabs::ClientSideValidations::Adapters::ActiveRecord2
37
- klass = ActiveRecord::Base
33
+ elsif defined?(::ActiveRecord)
34
+ if ::ActiveRecord::VERSION::MAJOR == 2
35
+ require 'validation_reflection/active_model'
36
+ require 'client_side_validations/adapters/active_model'
37
+ DNCLabs::ClientSideValidations::Adapter = DNCLabs::ClientSideValidations::Adapters::ActiveModel
38
+ klass = ::ActiveRecord::Base
39
+
40
+ ActiveRecord::Base.class_eval do
41
+ ::ActiveRecordExtensions::ValidationReflection.reflected_validations << :validates_size_of
42
+ ::ActiveRecordExtensions::ValidationReflection.install(self)
43
+ end
38
44
  end
39
45
  end
40
46
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 2
7
- - 5
8
- - 2
9
- version: 2.5.2
7
+ - 6
8
+ - 1
9
+ version: 2.6.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Cardarella
@@ -14,21 +14,21 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-12 00:00:00 -04:00
17
+ date: 2010-07-15 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: validation_reflection
21
+ name: validation_reflection-active_model
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - "="
26
26
  - !ruby/object:Gem::Version
27
27
  segments:
28
28
  - 0
29
- - 3
30
- - 6
31
- version: 0.3.6
29
+ - 2
30
+ - 0
31
+ version: 0.2.0
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
@@ -64,7 +64,6 @@ files:
64
64
  - lib/client_side_validations.rb
65
65
  - lib/client_side_validations/adapters/action_view.rb
66
66
  - lib/client_side_validations/adapters/active_model.rb
67
- - lib/client_side_validations/adapters/active_record_2.rb
68
67
  - lib/client_side_validations/adapters/orm_base.rb
69
68
  - lib/client_side_validations/orm.rb
70
69
  - lib/client_side_validations/template.rb
@@ -1,79 +0,0 @@
1
- require 'validation_reflection'
2
- require 'client_side_validations/adapters/orm_base'
3
-
4
- module DNCLabs
5
- module ClientSideValidations
6
- module Adapters
7
- class ActiveRecord2 < ORMBase
8
-
9
- def validation_to_hash(attr)
10
- base.class.reflect_on_validations_for(attr).inject({}) do |hash, validation|
11
- hash.merge!(build_validation_hash(validation.clone))
12
- end
13
- end
14
-
15
- def validation_fields
16
- base.class.reflect_on_all_validations.map { |v| v.name }.uniq
17
- end
18
-
19
- private
20
-
21
- def build_validation_hash(validation, message_key = 'message')
22
- if validation.macro == :validates_length_of &&
23
- (range = (validation.options.delete(:within) || validation.options.delete(:in)))
24
- validation.options[:minimum] = range.first
25
- validation.options[:maximum] = range.last
26
- elsif validation.macro == :validates_inclusion_of || validation.macro == :validates_exclusion_of
27
- validation.options[:in] = validation.options[:in].to_a
28
- end
29
- super
30
- end
31
-
32
- def supported_validation?(validation)
33
- SupportedValidations.include?(validation.macro.to_s.match(/\w+_(\w+)_\w+/)[1].to_sym)
34
- end
35
-
36
- def get_validation_message(validation)
37
- default = case validation.macro.to_sym
38
- when :validates_presence_of
39
- I18n.translate('activerecord.errors.messages.blank')
40
- when :validates_format_of
41
- I18n.translate('activerecord.errors.messages.invalid')
42
- when :validates_length_of
43
- if count = validation.options[:minimum]
44
- I18n.translate('activerecord.errors.messages.too_short').sub('{{count}}', count.to_s)
45
- elsif count = validation.options[:maximum]
46
- I18n.translate('activerecord.errors.messages.too_long').sub('{{count}}', count.to_s)
47
- end
48
- when :validates_numericality_of
49
- I18n.translate('activerecord.errors.messages.not_a_number')
50
- when :validates_uniqueness_of
51
- I18n.translate('activerecord.errors.messages.taken')
52
- when :validates_confirmation_of
53
- I18n.translate('activerecord.errors.messages.confirmation')
54
- when :validates_acceptance_of
55
- I18n.translate('activerecord.errors.messages.accepted')
56
- when :validates_inclusion_of
57
- I18n.translate('activerecord.errors.messages.inclusion')
58
- when :validates_exclusion_of
59
- I18n.translate('activerecord.errors.messages.exclusion')
60
- end
61
-
62
- message = validation.options.delete(:message)
63
- if message.kind_of?(String)
64
- message
65
- elsif message.kind_of?(Symbol)
66
- I18n.translate("activerecord.errors.models.#{base.class.to_s.downcase}.attributes.#{validation.name}.#{message}")
67
- else
68
- default
69
- end
70
- end
71
-
72
- def get_validation_method(validation)
73
- method = validation.macro.to_s
74
- method.match(/\w+_(\w+)_\w+/)[1]
75
- end
76
- end
77
- end
78
- end
79
- end