client_side_validations 2.8.0 → 2.9.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -53,21 +53,7 @@ This will copy client_side_validations.js to "public/javascripts"
53
53
  Download [jQuery](http://docs.jquery.com/Downloading_jQuery) and [jQuery Validation](http://bassistance.de/jquery-plugins/jquery-plugin-validation/) plugin to "public/javascripts"
54
54
 
55
55
  ### Rack
56
- If you want to validate_uniqueness_of a call to the server must be made. You can do this with the ClidenSideValidations::Uniqueness middleware.
57
-
58
- The following route will be reserved for client side validations:
59
-
60
- /validations/uniqueness.json
61
-
62
- Add the middleware to your stack:
63
-
64
- config/environment.rb for Rails 2.x
65
-
66
- config/application.rb for Rails 3.x
67
-
68
- ...
69
- config.middleware.use 'ClientSideValidations::Uniqueness'
70
- ...
56
+ As of version 2.9.0 the ClientSideValidations::Uniqueness middleware is automatically included as a Rails Engine. (both Rails 2 and Rails 3)
71
57
 
72
58
  ### Model
73
59
  Validate your models as you normally would
@@ -1,3 +1,15 @@
1
+ jQuery.validator.addMethod("numericality", function(value, element) {
2
+ return this.optional(element) || /^(\d+(\.|,)\d+|\d+)$/.test(value);
3
+ }, jQuery.validator.format("Is not a number."));
4
+
5
+ jQuery.validator.addMethod("odd", function(value, element) {
6
+ return this.optional(element) || parseInt(value) % 2 == 1;
7
+ }, jQuery.validator.format("Must be odd."));
8
+
9
+ jQuery.validator.addMethod("even", function(value, element) {
10
+ return this.optional(element) || parseInt(value) % 2 == 0;
11
+ }, jQuery.validator.format("Must be even."));
12
+
1
13
  jQuery.validator.addMethod("format", function(value, element, params) {
2
14
  var pattern = new RegExp(params, "i");
3
15
  return this.optional(element) || pattern.test(value);
@@ -44,6 +56,9 @@ $.extend($.fn, {
44
56
  var form = $(this[i]);
45
57
  var object = form.attr('data-csv');
46
58
  var validate_options = eval(object + "_validate_options");
59
+ if (typeof(validate_options['options']) == 'undefined') {
60
+ validate_options['options'] = { };
61
+ }
47
62
  validate_options.options.ignore = ':hidden';
48
63
  form.validate(validate_options);
49
64
  }
@@ -56,4 +56,5 @@ module ClientSideValidations
56
56
  end
57
57
 
58
58
  require 'client_side_validations/orm'
59
- require 'client_side_validations/template'
59
+ require 'client_side_validations/template'
60
+ require 'client_side_validations/rails' if defined?(Rails)
@@ -128,7 +128,11 @@ module DNCLabs
128
128
  when Class
129
129
  get_object_name(object.new)
130
130
  else
131
- ActionController::RecordIdentifier.singular_class_name(object)
131
+ if rails3?
132
+ ActiveModel::Naming.singular(object)
133
+ else
134
+ ActionController::RecordIdentifier.singular_class_name(object)
135
+ end
132
136
  end
133
137
  end
134
138
  end # BaseMethods
@@ -1,110 +1,120 @@
1
1
  require 'client_side_validations/adapters/orm_base'
2
2
 
3
- module DNCLabs
4
- module ClientSideValidations
5
- module Adapters
6
- class ActiveModel < ORMBase
3
+ module ClientSideValidations
4
+ module Adapters
5
+ class ActiveModel < ORMBase
7
6
 
8
- def validations_to_hash(attr)
9
- base._validators[attr.to_sym].inject({}) do |hash, validation|
10
- hash.merge!(build_validation_hash(validation.clone))
11
- end
7
+ def validations_to_hash(attr)
8
+ base._validators[attr.to_sym].inject({}) do |hash, validation|
9
+ hash.merge!(build_validation_hash(validation.clone))
12
10
  end
13
-
14
- def validation_fields
15
- base._validators.keys
11
+ end
12
+
13
+ private
14
+
15
+ def build_validation_hash(validation, message_key = 'message')
16
+ if validation.kind == :inclusion || validation.kind == :exclusion
17
+ validation.options[:in] = validation.options[:in].to_a
16
18
  end
17
19
 
18
- private
19
-
20
- def build_validation_hash(validation, message_key = 'message')
21
- if validation.kind == :inclusion || validation.kind == :exclusion
22
- validation.options[:in] = validation.options[:in].to_a
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
-
37
- super
20
+ if validation.kind == :size
21
+ validation.kind = :length
38
22
  end
39
-
40
- def supported_validation?(validation)
41
- SupportedValidations.include?(validation.kind.to_sym)
23
+
24
+ if validation.kind == :length &&
25
+ (range = (validation.options.delete(:within) || validation.options.delete(:in)))
26
+ validation.options[:minimum] = range.first
27
+ validation.options[:maximum] = range.last
28
+ elsif validation.kind == :inclusion || validation.kind == :exclusion
29
+ validation.options[:in] = validation.options[:in].to_a
42
30
  end
43
31
 
44
- def get_validation_message(validation)
45
- default = case validation.kind
46
- when :presence
47
- I18n.translate(i18n_prefix + 'errors.messages.blank')
48
- when :format
49
- I18n.translate(i18n_prefix + 'errors.messages.invalid')
50
- when :length
51
- if count = validation.options[:is]
52
- I18n.translate(i18n_prefix + 'errors.messages.wrong_length').sub(orm_error_interpolation(:count), count.to_s)
53
- elsif count = validation.options[:minimum]
54
- I18n.translate(i18n_prefix + 'errors.messages.too_short').sub(orm_error_interpolation(:count), count.to_s)
55
- elsif count = validation.options[:maximum]
56
- I18n.translate(i18n_prefix + 'errors.messages.too_long').sub(orm_error_interpolation(:count), count.to_s)
57
- end
58
- when :numericality
59
- I18n.translate(i18n_prefix + 'errors.messages.not_a_number')
60
- when :uniqueness
61
- if defined?(ActiveRecord) && base.kind_of?(ActiveRecord::Base)
62
- I18n.translate('activerecord.errors.messages.taken')
63
- elsif defined?(Mongoid) && base.class.included_modules.include?(Mongoid::Document)
64
- I18n.translate('errors.messages.taken')
65
- end
66
- when :confirmation
67
- I18n.translate(i18n_prefix + 'errors.messages.confirmation')
68
- when :acceptance
69
- I18n.translate(i18n_prefix + 'errors.messages.accepted')
70
- when :inclusion
71
- I18n.translate(i18n_prefix + 'errors.messages.inclusion')
72
- when :exclusion
73
- I18n.translate(i18n_prefix + 'errors.messages.exclusion')
74
- end
32
+ super
33
+ end
75
34
 
76
- message = validation.options.delete(:message)
77
- if message.kind_of?(String)
78
- message
79
- elsif message.kind_of?(Symbol)
80
- I18n.translate(i18n_prefix + "errors.models.#{base.class.to_s.downcase}.attributes.#{validation.attributes.first}.#{message}")
35
+ def supported_validation?(validation)
36
+ SupportedValidations.include?(validation.kind.to_sym)
37
+ end
38
+
39
+ def get_validation_message(validation)
40
+ default = case validation.kind
41
+ when :presence
42
+ I18n.translate(i18n_prefix + 'errors.messages.blank')
43
+ when :format
44
+ I18n.translate(i18n_prefix + 'errors.messages.invalid')
45
+ when :length
46
+ if count = validation.options[:is]
47
+ I18n.translate(i18n_prefix + 'errors.messages.wrong_length').sub(orm_error_interpolation(:count), count.to_s)
48
+ elsif count = validation.options[:minimum]
49
+ I18n.translate(i18n_prefix + 'errors.messages.too_short').sub(orm_error_interpolation(:count), count.to_s)
50
+ elsif count = validation.options[:maximum]
51
+ I18n.translate(i18n_prefix + 'errors.messages.too_long').sub(orm_error_interpolation(:count), count.to_s)
52
+ end
53
+ when :numericality
54
+ if validation.options[:only_integer]
55
+ I18n.translate(i18n_prefix + 'errors.messages.not_a_number')
56
+ elsif count = validation.options[:greater_than]
57
+ I18n.translate(i18n_prefix + 'errors.messages.greater_than').sub(orm_error_interpolation(:count), count.to_s)
58
+ elsif count = validation.options[:greater_than_or_equal_to]
59
+ I18n.translate(i18n_prefix + 'errors.messages.greater_than_or_equal_to').sub(orm_error_interpolation(:count), count.to_s)
60
+ elsif count = validation.options[:less_than]
61
+ I18n.translate(i18n_prefix + 'errors.messages.less_than').sub(orm_error_interpolation(:count), count.to_s)
62
+ elsif count = validation.options[:less_than_or_equal_to]
63
+ I18n.translate(i18n_prefix + 'errors.messages.less_than_or_equal_to').sub(orm_error_interpolation(:count), count.to_s)
64
+ elsif validation.options[:odd]
65
+ I18n.translate(i18n_prefix + 'errors.messages.odd')
66
+ elsif validation.options[:even]
67
+ I18n.translate(i18n_prefix + 'errors.messages.even')
81
68
  else
82
- default
69
+ I18n.translate(i18n_prefix + 'errors.messages.not_a_number')
83
70
  end
71
+ when :uniqueness
72
+ if defined?(ActiveRecord) && base.kind_of?(ActiveRecord::Base)
73
+ I18n.translate('activerecord.errors.messages.taken')
74
+ elsif defined?(Mongoid) && base.class.included_modules.include?(Mongoid::Document)
75
+ I18n.translate('errors.messages.taken')
76
+ end
77
+ when :confirmation
78
+ I18n.translate(i18n_prefix + 'errors.messages.confirmation')
79
+ when :acceptance
80
+ I18n.translate(i18n_prefix + 'errors.messages.accepted')
81
+ when :inclusion
82
+ I18n.translate(i18n_prefix + 'errors.messages.inclusion')
83
+ when :exclusion
84
+ I18n.translate(i18n_prefix + 'errors.messages.exclusion')
84
85
  end
85
86
 
86
- def get_validation_method(validation)
87
- validation.kind.to_s
87
+ message = validation.options.delete(:message)
88
+ if message.kind_of?(String)
89
+ message
90
+ elsif message.kind_of?(Symbol)
91
+ I18n.translate(i18n_prefix + "errors.models.#{base.class.to_s.downcase}.attributes.#{validation.attributes.first}.#{message}")
92
+ else
93
+ default
88
94
  end
89
-
90
- def i18n_prefix
91
- if defined?(::ActiveModel)
92
- ''
93
- else # ActiveRecord 2.x
94
- 'activerecord.'
95
- end
95
+ end
96
+
97
+ def get_validation_method(validation)
98
+ validation.kind.to_s
99
+ end
100
+
101
+ def i18n_prefix
102
+ if defined?(::ActiveModel)
103
+ ''
104
+ else # ActiveRecord 2.x
105
+ 'activerecord.'
96
106
  end
97
-
98
- def orm_error_interpolation(name)
99
- if defined?(::ActiveModel)
100
- "%{#{name}}"
101
-
102
- else # ActiveRecord 2.x
103
- "{{#{name}}}"
104
- end
107
+ end
108
+
109
+ def orm_error_interpolation(name)
110
+ if defined?(::ActiveModel)
111
+ "%{#{name}}"
112
+
113
+ else # ActiveRecord 2.x
114
+ "{{#{name}}}"
105
115
  end
106
-
107
116
  end
117
+
108
118
  end
109
119
  end
110
120
  end
@@ -1,88 +1,89 @@
1
- module DNCLabs
2
- module ClientSideValidations
3
- module Adapters
4
- class ORMBase
5
- attr_accessor :base
6
-
7
- def initialize(base)
8
- self.base = base
9
- end
10
-
11
- def validations_to_hash(attr)
12
- end
13
-
14
- def validation_fields
15
- []
16
- end
17
-
18
- private
1
+ module ClientSideValidations
2
+ module Adapters
3
+ class ORMBase
4
+ attr_accessor :base
5
+
6
+ def initialize(base)
7
+ self.base = base
8
+ end
9
+
10
+ def validations_to_hash(attr)
11
+ end
12
+
13
+ def validation_fields
14
+ []
15
+ end
16
+
17
+ private
19
18
 
20
- SupportedValidations = [:presence, :format, :length, :numericality, :uniqueness,
21
- :confirmation, :acceptance, :inclusion, :exclusion]
22
-
23
- def build_validation_hash(validation, message_key = 'message')
24
- if can_validate?(validation)
25
- validation_hash = {}
26
- message = get_validation_message(validation)
27
- options = get_validation_options(validation)
28
- method = get_validation_method(validation)
19
+ SupportedValidations = [:presence, :format, :length, :numericality, :uniqueness,
20
+ :confirmation, :acceptance, :inclusion, :exclusion]
21
+
22
+ def build_validation_hash(validation, message_key = 'message')
23
+ if can_validate?(validation)
24
+ validation_hash = {}
25
+ message = get_validation_message(validation)
26
+ options = get_validation_options(validation)
27
+ method = get_validation_method(validation)
29
28
 
30
- if validation.options.has_key?(:minimum) && validation.options.has_key?(:maximum)
31
- message_key = 'message_min'
32
- cloned_validation = validation.clone
33
- cloned_validation.options.delete(:minimum)
34
- validation_hash.merge!(build_validation_hash(cloned_validation, 'message_max'))
35
- end
36
-
37
- new_validation_hash = { method => { message_key => message }.merge(options.stringify_keys) }
38
-
39
- unless validation_hash.empty?
40
- validation_hash["length"].merge!(new_validation_hash["length"])
41
- validation_hash
42
- else
43
- new_validation_hash
44
- end
29
+ if validation.options.has_key?(:minimum) && validation.options.has_key?(:maximum)
30
+ message_key = 'message_min'
31
+ cloned_validation = validation.clone
32
+ cloned_validation.options.delete(:minimum)
33
+ validation_hash.merge!(build_validation_hash(cloned_validation, 'message_max'))
34
+ end
35
+
36
+ new_validation_hash = { method => { message_key => message }.merge(options.stringify_keys) }
37
+
38
+ unless validation_hash.empty?
39
+ validation_hash["length"].merge!(new_validation_hash["length"])
40
+ validation_hash
45
41
  else
46
- {}
42
+ new_validation_hash
47
43
  end
44
+ else
45
+ {}
48
46
  end
49
-
50
- def can_validate?(validation)
51
- if supported_validation?(validation)
52
- if on = validation.options[:on]
53
- on = on.to_sym
54
- (on == :save) ||
55
- (on == :create && base.new_record?) ||
56
- (on == :update && !base.new_record?)
57
- elsif(if_condition = (validation.options[:if] || validation.options[:allow_validation]))
58
- base.instance_eval(if_condition.to_s)
59
- elsif(unless_condition = (validation.options[:unless] || validation.options[:skip_validation]))
60
- !base.instance_eval(unless_condition.to_s)
61
- else
62
- true
63
- end
47
+ end
48
+
49
+ def can_validate?(validation)
50
+ if supported_validation?(validation)
51
+ if on = validation.options[:on]
52
+ on = on.to_sym
53
+ (on == :save) ||
54
+ (on == :create && base.new_record?) ||
55
+ (on == :update && !base.new_record?)
56
+ elsif(if_condition = (validation.options[:if] || validation.options[:allow_validation]))
57
+ base.instance_eval(if_condition.to_s)
58
+ elsif(unless_condition = (validation.options[:unless] || validation.options[:skip_validation]))
59
+ !base.instance_eval(unless_condition.to_s)
60
+ else
61
+ true
64
62
  end
65
63
  end
64
+ end
66
65
 
67
- def get_validation_message(validation)
68
- end
66
+ def get_validation_message(validation)
67
+ end
69
68
 
70
- def get_validation_options(validation)
71
- options = validation.options.clone
72
- options.symbolize_keys!
73
- deleteable_keys = [:on, :tokenizer, :only_integer, :allow_nil, :case_sensitive, :accept, :if, :unless, :allow_validation]
74
- options.delete(:maximum) if options.has_key?(:minimum)
75
- options.delete_if { |k, v| deleteable_keys.include?(k) }
76
- if options[:with].kind_of?(Regexp)
77
- options[:with] = options[:with].inspect.to_s.sub("\\A","^").sub("\\Z","$").sub(%r{^/},"").sub(%r{/i?$}, "")
78
- end
79
- options
69
+ def get_validation_options(validation)
70
+ options = validation.options.clone
71
+ options.symbolize_keys!
72
+ deleteable_keys = [:on, :tokenizer, :allow_nil, :case_sensitive, :accept, :if, :unless, :allow_validation]
73
+ options.delete(:maximum) if options.has_key?(:minimum)
74
+ options.delete_if { |k, v| deleteable_keys.include?(k) }
75
+ if options[:with].kind_of?(Regexp)
76
+ options[:with] = options[:with].inspect.to_s.sub("\\A","^").sub("\\Z","$").sub(%r{^/},"").sub(%r{/i?$}, "")
80
77
  end
81
-
82
- def get_validation_method(validation)
78
+ if options[:only_integer] == false
79
+ options.delete(:only_integer)
83
80
  end
84
-
81
+ options
82
+ end
83
+
84
+ def get_validation_method(validation)
85
85
  end
86
+
86
87
  end
87
88
  end
88
89
  end
@@ -1,163 +1,213 @@
1
- module DNCLabs
2
- module ClientSideValidations
1
+ require 'client_side_validations/adapters/active_model'
2
+
3
+ module ClientSideValidations
4
+ module ORM
3
5
  def validate_options
4
- rules = Hash.new { |h, field| h[field] = {} }
5
- messages = Hash.new { |h, field| h[field] = {} }
6
- validation_fields.each do |field|
7
- validations = validations_to_hash(field)
8
- rules[field.to_s].merge!(extract_rules(validations, field))
9
- messages[field.to_s].merge!(extract_messages(validations))
10
- end
11
- {"rules" => rules, "messages" => messages}
6
+ ValidateOptions.new(self).to_hash
12
7
  end
13
-
14
- def validations_to_hash(field)
15
- dnc_csv_adapter.validations_to_hash(field)
8
+
9
+ def validation_fields
10
+ self._validators.keys
16
11
  end
17
12
 
18
- private
13
+ class ValidateOptions
14
+ attr_accessor :base
15
+
16
+ def initialize(base)
17
+ self.base = base
18
+ end
19
19
 
20
- def convert_kind(kind, options = nil)
21
- case kind
22
- when 'acceptance', 'exclusion', 'inclusion'
23
- kind
24
- when 'confirmation'
25
- 'equalTo'
26
- when 'presence'
27
- 'required'
28
- when 'numericality'
29
- 'digits'
30
- when 'length'
31
- if options['is']
32
- 'islength'
33
- elsif options['minimum'] && options['maximum']
34
- ['minlength', 'maxlength']
35
- elsif options['minimum']
36
- 'minlength'
37
- elsif options['maximum']
38
- 'maxlength'
20
+ def to_hash
21
+ rules = Hash.new { |h, field| h[field] = {} }
22
+ messages = Hash.new { |h, field| h[field] = {} }
23
+ base.validation_fields.each do |field|
24
+ validations = validations_for(field)
25
+ rules[field.to_s].merge!(extract_rules(validations, field))
26
+ messages[field.to_s].merge!(extract_messages(validations))
39
27
  end
40
- when 'uniqueness'
41
- 'remote'
28
+ {"rules" => rules, "messages" => messages}
42
29
  end
43
- end
44
-
45
- def extract_rules(validations, field = nil)
46
- rules = {}
47
- validations.each do |kind, options|
48
- kind = convert_kind(kind, options)
49
- value = case kind
50
- when 'acceptance'
51
- true
52
- when 'equalTo'
53
- %{[name="#{field}_confirmation"]}
54
- when 'required'
55
- true
56
- when 'digits'
57
- true
58
- when 'exclusion', 'inclusion'
59
- options['in']
60
- when 'islength'
61
- options['is']
62
- when 'minlength'
63
- options['minimum']
64
- when 'maxlength'
65
- options['maximum']
66
- when 'remote'
67
- if self.new_record?
68
- data = {}
30
+
31
+ def validations_for(field)
32
+ adapter.validations_to_hash(field)
33
+ end
34
+
35
+ private
36
+
37
+ def convert_kind(kind, options = nil)
38
+ case kind
39
+ when 'acceptance', 'exclusion', 'inclusion', 'format'
40
+ kind
41
+ when 'confirmation'
42
+ 'equalTo'
43
+ when 'presence'
44
+ 'required'
45
+ when 'numericality'
46
+ if options['only_integer']
47
+ 'digits'
48
+ elsif options['greater_than']
49
+ 'greater_than'
50
+ elsif options['greater_than_or_equal_to']
51
+ 'min'
52
+ elsif options['less_than']
53
+ 'less_than'
54
+ elsif options['less_than_or_equal_to']
55
+ 'max'
56
+ elsif options['odd']
57
+ 'odd'
58
+ elsif options['even']
59
+ 'even'
69
60
  else
70
- data = { "#{self.class.to_s.underscore}[id]" => self.id}
61
+ 'numericality'
62
+ end
63
+ when 'length'
64
+ if options['is']
65
+ 'islength'
66
+ elsif options['minimum'] && options['maximum']
67
+ ['minlength', 'maxlength']
68
+ elsif options['minimum']
69
+ 'minlength'
70
+ elsif options['maximum']
71
+ 'maxlength'
71
72
  end
72
- {'url' => '/validations/uniqueness.json', 'data' => data}
73
+ when 'uniqueness'
74
+ 'remote'
73
75
  end
76
+ end
74
77
 
75
- unless Array === kind
76
- rules[kind] = value
77
- else
78
- kind.each do |k|
79
- special_rule = case k
80
- when 'minlength'
81
- options['minimum']
82
- when 'maxlength'
83
- options['maximum']
78
+ def extract_rules(validations, field = nil)
79
+ rules = {}
80
+ validations.each do |kind, options|
81
+ kind = convert_kind(kind, options)
82
+ value = case kind
83
+ when 'acceptance', 'required', 'digits', 'numericality', 'greater_than', 'min', 'less_than', 'max', 'odd', 'even'
84
+ true
85
+ when 'format'
86
+ options['with']
87
+ when 'equalTo'
88
+ %{[name="#{field}_confirmation"]}
89
+ when 'exclusion', 'inclusion'
90
+ options['in']
91
+ when 'islength'
92
+ options['is']
93
+ when 'minlength'
94
+ options['minimum']
95
+ when 'maxlength'
96
+ options['maximum']
97
+ when 'remote'
98
+ if base.new_record?
99
+ data = {}
100
+ else
101
+ data = { "#{base.class.to_s.underscore}[id]" => base.id}
84
102
  end
85
-
86
- rules[k] = special_rule
103
+ {'url' => '/validations/uniqueness.json', 'data' => data}
87
104
  end
88
- end
89
105
 
90
- if required?(kind, options)
91
- unless rules['required']
92
- rules['required'] = true
93
- end
94
- end
106
+ unless Array === kind
107
+ rules[kind] = value
108
+ else
109
+ kind.each do |k|
110
+ special_rule = case k
111
+ when 'minlength'
112
+ options['minimum']
113
+ when 'maxlength'
114
+ options['maximum']
115
+ end
95
116
 
96
- end
97
- rules
98
- end
117
+ rules[k] = special_rule
118
+ end
119
+ end
99
120
 
100
- def extract_messages(validations)
101
- messages = {}
102
- validations.each do |kind, options|
103
- kind = convert_kind(kind, options)
104
- unless Array === kind
105
- messages[kind] = options['message']
106
- else
107
- kind.each do |k|
108
- special_message = case k
109
- when 'minlength'
110
- options['message'] = options['message_min']
111
- 'message_min'
112
- when 'maxlength'
113
- 'message_max'
121
+ if numericality?(kind)
122
+ unless rules['numericality'] || rules['digits']
123
+ rules['numericality'] = true
114
124
  end
125
+ end
115
126
 
116
- messages[k] = options[special_message]
127
+ if required?(kind, options)
128
+ unless rules['required']
129
+ rules['required'] = true
130
+ end
117
131
  end
132
+
118
133
  end
134
+ rules
135
+ end
136
+
137
+ def extract_messages(validations)
138
+ messages = {}
139
+ validations.each do |kind, options|
140
+ kind = convert_kind(kind, options)
141
+ unless Array === kind
142
+ messages[kind] = options['message']
143
+ else
144
+ kind.each do |k|
145
+ special_message = case k
146
+ when 'minlength'
147
+ options['message'] = options['message_min']
148
+ 'message_min'
149
+ when 'maxlength'
150
+ 'message_max'
151
+ end
119
152
 
120
- if required?(kind, options)
121
- unless messages['required']
122
- messages['required'] = options['message']
153
+ messages[k] = options[special_message]
154
+ end
155
+ end
156
+
157
+ if numericality?(kind)
158
+ messages['numericality'] = I18n.translate(i18n_prefix + 'errors.messages.not_a_number')
159
+ end
160
+
161
+ if required?(kind, options)
162
+ unless messages['required']
163
+ if ['greater_than', 'min', 'less_than', 'max', 'odd', 'even'].include?(kind)
164
+ messages['required'] = I18n.translate(i18n_prefix + 'errors.messages.not_a_number')
165
+ else
166
+ messages['required'] = options['message']
167
+ end
168
+ end
123
169
  end
124
170
  end
171
+ messages
125
172
  end
126
- messages
127
- end
128
-
129
- def required?(kind, options)
130
- case kind
131
- when 'digits', 'exclusion', 'inclusion', 'islength', 'minlength', 'remote'
132
- !options['allow_blank']
133
- when Array
134
- required?('minlength', options) if kind.include?('minlength')
173
+
174
+ def i18n_prefix
175
+ if defined?(::ActiveModel)
176
+ ''
177
+ else # ActiveRecord 2.x
178
+ 'activerecord.'
179
+ end
135
180
  end
136
- end
137
-
138
- def validation_fields
139
- dnc_csv_adapter.validation_fields
140
- end
141
-
142
- def dnc_csv_adapter
143
- unless @dnc_csv_adapter
144
- @dnc_csv_adapter = Adapter.new(self)
181
+
182
+ def numericality?(kind)
183
+ ['greater_than', 'min', 'less_than', 'max', 'even', 'odd'].include?(kind)
184
+ end
185
+
186
+ def required?(kind, options)
187
+ case kind
188
+ when 'digits', 'exclusion', 'inclusion', 'islength', 'minlength', 'remote', 'numericality', 'greater_than', 'min', 'less_than', 'max', 'even', 'odd', 'format'
189
+ !options['allow_blank']
190
+ when Array
191
+ required?('minlength', options) if kind.include?('minlength')
192
+ end
193
+ end
194
+
195
+ def adapter
196
+ unless @adapter
197
+ @adapter = ClientSideValidations::Adapters::ActiveModel.new(base)
198
+ end
199
+ @adapter
145
200
  end
146
- @dnc_csv_adapter
147
201
  end
148
202
  end
149
203
  end
150
204
 
151
205
  if defined?(::ActiveModel)
152
- require 'client_side_validations/adapters/active_model'
153
- DNCLabs::ClientSideValidations::Adapter = DNCLabs::ClientSideValidations::Adapters::ActiveModel
154
206
  klass = ::ActiveModel::Validations
155
207
 
156
208
  elsif defined?(::ActiveRecord)
157
209
  if ::ActiveRecord::VERSION::MAJOR == 2
158
210
  require 'validation_reflection/active_model'
159
- require 'client_side_validations/adapters/active_model'
160
- DNCLabs::ClientSideValidations::Adapter = DNCLabs::ClientSideValidations::Adapters::ActiveModel
161
211
  klass = ::ActiveRecord::Base
162
212
 
163
213
  ActiveRecord::Base.class_eval do
@@ -169,6 +219,6 @@ end
169
219
 
170
220
  if klass
171
221
  klass.class_eval do
172
- include DNCLabs::ClientSideValidations
222
+ include ClientSideValidations::ORM
173
223
  end
174
224
  end
@@ -0,0 +1,11 @@
1
+ module ClientSideValidations
2
+ if Rails::VERSION::MAJOR == 3
3
+ class Engine < ::Rails::Engine
4
+ config.app_middleware.use ClientSideValidations::Uniqueness
5
+ end
6
+ else
7
+ Rails.configuration.after_initialize do
8
+ Rails.configuration.middleware.use ClientSideValidations::Uniqueness
9
+ end
10
+ end
11
+ end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
- - 8
9
- - 0
10
- version: 2.8.0
8
+ - 9
9
+ - 2
10
+ version: 2.9.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brian Cardarella
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-25 00:00:00 -04:00
18
+ date: 2010-08-10 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -50,6 +50,144 @@ dependencies:
50
50
  version: 1.4.3
51
51
  type: :runtime
52
52
  version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: jspec
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: rspec
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: actionpack
83
+ prerelease: false
84
+ requirement: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - "="
88
+ - !ruby/object:Gem::Version
89
+ hash: 7712042
90
+ segments:
91
+ - 3
92
+ - 0
93
+ - 0
94
+ - rc
95
+ version: 3.0.0.rc
96
+ type: :development
97
+ version_requirements: *id005
98
+ - !ruby/object:Gem::Dependency
99
+ name: activerecord
100
+ prerelease: false
101
+ requirement: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - "="
105
+ - !ruby/object:Gem::Version
106
+ hash: 7712042
107
+ segments:
108
+ - 3
109
+ - 0
110
+ - 0
111
+ - rc
112
+ version: 3.0.0.rc
113
+ type: :development
114
+ version_requirements: *id006
115
+ - !ruby/object:Gem::Dependency
116
+ name: bson_ext
117
+ prerelease: false
118
+ requirement: &id007 !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - "="
122
+ - !ruby/object:Gem::Version
123
+ hash: 31
124
+ segments:
125
+ - 1
126
+ - 0
127
+ - 4
128
+ version: 1.0.4
129
+ type: :development
130
+ version_requirements: *id007
131
+ - !ruby/object:Gem::Dependency
132
+ name: mongoid
133
+ prerelease: false
134
+ requirement: &id008 !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - "="
138
+ - !ruby/object:Gem::Version
139
+ hash: 62196419
140
+ segments:
141
+ - 2
142
+ - 0
143
+ - 0
144
+ - beta
145
+ - 16
146
+ version: 2.0.0.beta.16
147
+ type: :development
148
+ version_requirements: *id008
149
+ - !ruby/object:Gem::Dependency
150
+ name: crack
151
+ prerelease: false
152
+ requirement: &id009 !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ hash: 3
158
+ segments:
159
+ - 0
160
+ version: "0"
161
+ type: :development
162
+ version_requirements: *id009
163
+ - !ruby/object:Gem::Dependency
164
+ name: bourne
165
+ prerelease: false
166
+ requirement: &id010 !ruby/object:Gem::Requirement
167
+ none: false
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ hash: 3
172
+ segments:
173
+ - 0
174
+ version: "0"
175
+ type: :development
176
+ version_requirements: *id010
177
+ - !ruby/object:Gem::Dependency
178
+ name: rack-test
179
+ prerelease: false
180
+ requirement: &id011 !ruby/object:Gem::Requirement
181
+ none: false
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ hash: 3
186
+ segments:
187
+ - 0
188
+ version: "0"
189
+ type: :development
190
+ version_requirements: *id011
53
191
  description: Client Side Validations for Rails 2.x and 3.x
54
192
  email: bcardarella@gmail.com
55
193
  executables: []
@@ -70,6 +208,7 @@ files:
70
208
  - lib/client_side_validations/adapters/active_model.rb
71
209
  - lib/client_side_validations/adapters/orm_base.rb
72
210
  - lib/client_side_validations/orm.rb
211
+ - lib/client_side_validations/rails.rb
73
212
  - lib/client_side_validations/template.rb
74
213
  - lib/generators/client_side_validations_generator.rb
75
214
  has_rdoc: true