client_side_validations 2.4.2 → 2.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,40 +1,14 @@
1
+ require 'client_side_validations/adapters/orm_base'
2
+
1
3
  module DNCLabs
2
4
  module ClientSideValidations
3
5
  module Adapters
4
- class ActiveModel
5
- attr_accessor :base
6
-
7
- def initialize(base)
8
- self.base = base
9
- end
10
-
11
- def validation_to_hash(_attr, _options = {})
12
- validation_hash = {}
13
- base._validators[_attr.to_sym].each do |validation|
14
- if can_validate?(validation) && supported_validation?(validation)
15
- message = get_validation_message(validation, _options[:locale])
16
- validation.options.delete(:message)
17
- options = get_validation_options(validation.options)
18
- method = get_validation_method(validation.kind)
19
- if conditional_method = remove_reserved_conditionals(options[:if])
20
- if base.instance_eval(conditional_method.to_s)
21
- options.delete(:if)
22
- validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
23
- end
24
- elsif conditional_method = options[:unless]
25
- unless base.instance_eval(conditional_method.to_s)
26
- options.delete(:unless)
27
- validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
28
- end
29
- else
30
- options.delete(:if)
31
- options.delete(:unless)
32
- validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
33
- end
34
- end
35
- end
6
+ class ActiveModel < ORMBase
36
7
 
37
- validation_hash
8
+ def validation_to_hash(attr)
9
+ base._validators[attr.to_sym].inject({}) do |hash, validation|
10
+ hash.merge!(build_validation_hash(validation.clone))
11
+ end
38
12
  end
39
13
 
40
14
  def validation_fields
@@ -42,80 +16,62 @@ module DNCLabs
42
16
  end
43
17
 
44
18
  private
45
-
46
- def supported_validation?(validation)
47
- [:presence, :format, :length, :numericality, :uniqueness,
48
- :confirmation ].include?(validation.kind.to_sym)
49
- end
50
-
51
- def can_validate?(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
- else
58
- true
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
59
23
  end
24
+ super
60
25
  end
61
26
 
62
- def get_validation_message(validation, locale)
27
+ def supported_validation?(validation)
28
+ SupportedValidations.include?(validation.kind.to_sym)
29
+ end
30
+
31
+ def get_validation_message(validation)
63
32
  default = case validation.kind
64
33
  when :presence
65
- I18n.translate('errors.messages.blank', :locale => locale)
34
+ I18n.translate('errors.messages.blank')
66
35
  when :format
67
- I18n.translate('errors.messages.invalid', :locale => locale)
36
+ I18n.translate('errors.messages.invalid')
68
37
  when :length
69
38
  if count = validation.options[:minimum]
70
- I18n.translate('errors.messages.too_short', :locale => locale).sub('%{count}', count.to_s)
39
+ I18n.translate('errors.messages.too_short').sub('%{count}', count.to_s)
71
40
  elsif count = validation.options[:maximum]
72
- I18n.translate('errors.messages.too_long', :locale => locale).sub('%{count}', count.to_s)
41
+ I18n.translate('errors.messages.too_long').sub('%{count}', count.to_s)
73
42
  end
74
43
  when :numericality
75
- I18n.translate('errors.messages.not_a_number', :locale => locale)
44
+ I18n.translate('errors.messages.not_a_number')
76
45
  when :uniqueness
77
46
  if defined?(ActiveRecord) && base.kind_of?(ActiveRecord::Base)
78
- I18n.translate('activerecord.errors.messages.taken', :locale => locale)
47
+ I18n.translate('activerecord.errors.messages.taken')
79
48
  elsif defined?(Mongoid) && base.class.included_modules.include?(Mongoid::Document)
80
- I18n.translate('errors.messages.taken', :locale => locale)
49
+ I18n.translate('errors.messages.taken')
81
50
  end
82
51
  when :confirmation
83
- I18n.translate('errors.messages.confirmation', :locale => locale)
52
+ I18n.translate('errors.messages.confirmation')
53
+ when :acceptance
54
+ I18n.translate('errors.messages.accepted')
55
+ when :inclusion
56
+ I18n.translate('errors.messages.inclusion')
57
+ when :exclusion
58
+ I18n.translate('errors.messages.exclusion')
84
59
  end
85
60
 
86
- message = validation.options[:message]
61
+ message = validation.options.delete(:message)
87
62
  if message.kind_of?(String)
88
63
  message
89
64
  elsif message.kind_of?(Symbol)
90
- I18n.translate("errors.models.#{base.class.to_s.downcase}.attributes.#{validation.attributes.first}.#{message}", :locale => locale)
65
+ I18n.translate("errors.models.#{base.class.to_s.downcase}.attributes.#{validation.attributes.first}.#{message}")
91
66
  else
92
67
  default
93
68
  end
94
69
  end
95
70
 
96
- def get_validation_options(options)
97
- options.symbolize_keys!
98
- options.delete(:on) == :create
99
- options.delete(:tokenizer)
100
- options.delete(:only_integer)
101
- options.delete(:allow_nil)
102
- options.delete(:case_sensitive)
103
- if options[:with].kind_of?(Regexp)
104
- options[:with] = options[:with].inspect.to_s.sub("\\A","^").sub("\\Z","$").sub(%r{^/},"").sub(%r{/i?$}, "")
105
- end
106
- options
71
+ def get_validation_method(validation)
72
+ validation.kind.to_s
107
73
  end
108
74
 
109
- def get_validation_method(kind)
110
- kind.to_s
111
- end
112
-
113
- def remove_reserved_conditionals(*conditionals)
114
- conditionals.flatten!
115
- conditionals.delete_if { |conditional| conditional =~ /@_/ }
116
- conditionals.delete_if { |conditional| conditional =~ /validation_context/ }
117
- conditionals.first
118
- end
119
75
  end
120
76
  end
121
77
  end
@@ -1,40 +1,15 @@
1
1
  require 'validation_reflection'
2
+ require 'client_side_validations/adapters/orm_base'
2
3
 
3
4
  module DNCLabs
4
5
  module ClientSideValidations
5
6
  module Adapters
6
- class ActiveRecord2
7
- attr_accessor :base
8
-
9
- def initialize(base)
10
- self.base = base
11
- end
12
-
13
- def validation_to_hash(attr, options = {})
14
- validation_hash = {}
15
- base.class.reflect_on_validations_for(attr).each do |validation|
16
- if can_validate?(validation) && supported_validation?(validation)
17
- message = get_validation_message(validation, options[:locale])
18
- validation.options.delete(:message)
19
- options = get_validationoptions(validation.options)
20
- method = get_validation_method(validation.macro)
21
- if conditional_method = options[:if]
22
- if base.instance_eval(conditional_method.to_s)
23
- options.delete(:if)
24
- validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
25
- end
26
- elsif conditional_method = options[:unless]
27
- unless base.instance_eval(conditional_method.to_s)
28
- options.delete(:unless)
29
- validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
30
- end
31
- else
32
- validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
33
- end
34
- end
35
- end
7
+ class ActiveRecord2 < ORMBase
36
8
 
37
- validation_hash
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
38
13
  end
39
14
 
40
15
  def validation_fields
@@ -43,79 +18,60 @@ module DNCLabs
43
18
 
44
19
  private
45
20
 
46
- def supported_validation?(validation)
47
- [:validates_presence_of, :validates_format_of, :validates_length_of,
48
- :validates_numericality_of, :validates_uniqueness_of,
49
- :validates_confirmation_of].include?(validation.macro.to_sym)
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
50
30
  end
51
31
 
52
- def can_validate?(validation)
53
- if on = validation.options[:on]
54
- on = on.to_sym
55
- (on == :save) ||
56
- (on == :create && base.new_record?) ||
57
- (on == :update && !base.new_record?)
58
- else
59
- true
60
- end
32
+ def supported_validation?(validation)
33
+ SupportedValidations.include?(validation.macro.to_s.match(/\w+_(\w+)_\w+/)[1].to_sym)
61
34
  end
62
-
63
- def get_validation_message(validation, locale)
35
+
36
+ def get_validation_message(validation)
64
37
  default = case validation.macro.to_sym
65
38
  when :validates_presence_of
66
- I18n.translate('activerecord.errors.messages.blank', :locale => locale)
39
+ I18n.translate('activerecord.errors.messages.blank')
67
40
  when :validates_format_of
68
- I18n.translate('activerecord.errors.messages.invalid', :locale => locale)
41
+ I18n.translate('activerecord.errors.messages.invalid')
69
42
  when :validates_length_of
70
43
  if count = validation.options[:minimum]
71
- I18n.translate('activerecord.errors.messages.too_short', :locale => locale).sub('{{count}}', count.to_s)
44
+ I18n.translate('activerecord.errors.messages.too_short').sub('{{count}}', count.to_s)
72
45
  elsif count = validation.options[:maximum]
73
- I18n.translate('activerecord.errors.messages.too_long', :locale => locale).sub('{{count}}', count.to_s)
46
+ I18n.translate('activerecord.errors.messages.too_long').sub('{{count}}', count.to_s)
74
47
  end
75
48
  when :validates_numericality_of
76
- I18n.translate('activerecord.errors.messages.not_a_number', :locale => locale)
49
+ I18n.translate('activerecord.errors.messages.not_a_number')
77
50
  when :validates_uniqueness_of
78
- I18n.translate('activerecord.errors.messages.taken', :locale => locale)
51
+ I18n.translate('activerecord.errors.messages.taken')
79
52
  when :validates_confirmation_of
80
- I18n.translate('activerecord.errors.messages.confirmation', :locale => locale)
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')
81
60
  end
82
-
83
- message = validation.options[:message]
61
+
62
+ message = validation.options.delete(:message)
84
63
  if message.kind_of?(String)
85
64
  message
86
65
  elsif message.kind_of?(Symbol)
87
- I18n.translate("activerecord.errors.models.#{base.class.to_s.downcase}.attributes.#{validation.name}.#{message}", :locale => locale)
66
+ I18n.translate("activerecord.errors.models.#{base.class.to_s.downcase}.attributes.#{validation.name}.#{message}")
88
67
  else
89
68
  default
90
69
  end
91
70
  end
92
71
 
93
- def get_validationoptions(options)
94
- options.symbolize_keys!
95
- options.delete(:on)
96
- if options[:with].kind_of?(Regexp)
97
- options[:with] = options[:with].inspect.to_s.sub("\\A","^").sub("\\Z","$").sub(%r{^/},"").sub(%r{/i?$}, "")
98
- end
99
- options
100
- end
101
-
102
- def get_validation_method(method)
103
- method = case method.to_sym
104
- when :validates_presence_of
105
- :presence
106
- when :validates_format_of
107
- :format
108
- when :validates_numericality_of
109
- :numericality
110
- when :validates_length_of
111
- :length
112
- when :validates_uniqueness_of
113
- :uniqueness
114
- when :validates_confirmation_of
115
- :confirmation
116
- end
117
-
118
- method.to_s
72
+ def get_validation_method(validation)
73
+ method = validation.macro.to_s
74
+ method.match(/\w+_(\w+)_\w+/)[1]
119
75
  end
120
76
  end
121
77
  end
@@ -0,0 +1,88 @@
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 validation_to_hash(attr)
12
+ end
13
+
14
+ def validation_fields
15
+ []
16
+ end
17
+
18
+ private
19
+
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)
29
+
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
45
+ else
46
+ {}
47
+ end
48
+ 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]
58
+ base.instance_eval(if_condition.to_s)
59
+ elsif unless_condition = validation.options[:unless]
60
+ !base.instance_eval(unless_condition.to_s)
61
+ else
62
+ true
63
+ end
64
+ end
65
+ end
66
+
67
+ def get_validation_message(validation)
68
+ end
69
+
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]
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
80
+ end
81
+
82
+ def get_validation_method(validation)
83
+ end
84
+
85
+ end
86
+ end
87
+ end
88
+ end
@@ -8,8 +8,8 @@ module DNCLabs
8
8
  hash.to_json
9
9
  end
10
10
 
11
- def validation_to_hash(field, options = {})
12
- dnc_csv_adapter.validation_to_hash(field, options)
11
+ def validation_to_hash(field)
12
+ dnc_csv_adapter.validation_to_hash(field)
13
13
  end
14
14
 
15
15
  def validation_fields
@@ -9,5 +9,7 @@ class ClientSideValidationsGenerator < Rails::Generators::Base
9
9
  def install_client_side_validations
10
10
  copy_file('../../javascript/lib/client_side_validations.js',
11
11
  'public/javascripts/client_side_validations.js')
12
+ copy_file('../../javascript/lib/jquery.validate.js',
13
+ 'public/javascripts/jquery.validate.js')
12
14
  end
13
15
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 2
7
- - 4
7
+ - 5
8
8
  - 2
9
- version: 2.4.2
9
+ version: 2.5.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Cardarella
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-11 00:00:00 -04:00
17
+ date: 2010-07-12 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -60,10 +60,12 @@ files:
60
60
  - README.markdown
61
61
  - generators/client_side_validations/client_side_validations_generator.rb
62
62
  - javascript/lib/client_side_validations.js
63
+ - javascript/lib/jquery.validate.js
63
64
  - lib/client_side_validations.rb
64
65
  - lib/client_side_validations/adapters/action_view.rb
65
66
  - lib/client_side_validations/adapters/active_model.rb
66
67
  - lib/client_side_validations/adapters/active_record_2.rb
68
+ - lib/client_side_validations/adapters/orm_base.rb
67
69
  - lib/client_side_validations/orm.rb
68
70
  - lib/client_side_validations/template.rb
69
71
  - lib/generators/client_side_validations_generator.rb