client_side_validations 4.2.10 → 4.2.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3815d50af1d432488773ed5a27e0392c6c07d239
4
- data.tar.gz: 995f9c98958cbcae9c130cf798a10c015aa7d0e8
3
+ metadata.gz: 1e8b7b85a661425cfac3b38f15b96a273561c4f0
4
+ data.tar.gz: 325fa1311d5d869af1a0da27217ad4f64df5f226
5
5
  SHA512:
6
- metadata.gz: 827bfafbc7334a09397601c991c5549bac46e1f0f5d8b5ef50574cc4ce3b2d978771096927eeca650f9063aa8182b74e3e8a2b3a5979d58455485a45325e91cd
7
- data.tar.gz: acbb3113992799c6be12c2624b01a78fa0682e1f122a5cc471414a200056eaed0cb3d6aed60fc2c73eab6de0efa8b7b9a2ac5f54b2f074bb0a4a1d3e75d9fbad
6
+ metadata.gz: fc6e52713b743f1cd307b8531718ab1076158c478d463bff62a5dedbb0a5a9ff38e2b2bd81c974fa1cd3129abc05d0f8999e60314b8115598e62a794bed8bb32
7
+ data.tar.gz: 23983f27530ca135ba03380a05ed237f528f66c8f242301be38329b3785c561242c6ee27ce539b2fca28f57d155770b7b11e79fb3cf8e36a6bd4911b440c6eac
data/README.md CHANGED
@@ -143,7 +143,7 @@ individual validators:
143
143
 
144
144
  In the above case only the `presence` validator will be passed to the client.
145
145
 
146
- This is also the case with Procs, or any object that responds to `#call`
146
+ This is also the case with [other supported conditional validations](http://guides.rubyonrails.org/v4.2.0/active_record_validations.html#conditional-validation) (such as Procs, Arrays or Strings).
147
147
 
148
148
  ### Turning off validators ###
149
149
 
@@ -1,4 +1,5 @@
1
1
  require 'client_side_validations/core_ext'
2
+ require 'client_side_validations/active_model/conditionals'
2
3
 
3
4
  module ClientSideValidations
4
5
  module ActiveModel
@@ -23,12 +24,14 @@ module ClientSideValidations
23
24
  end
24
25
 
25
26
  module Validations
27
+ include ClientSideValidations::ActiveModel::Conditionals
28
+
26
29
  def client_side_validation_hash(force = nil)
27
30
  _validators.inject({}) do |attr_hash, attr|
28
31
  return attr_hash if [nil, :block].include?(attr[0])
29
32
 
30
33
  validator_hash = attr[1].each_with_object(Hash.new { |h, k| h[k] = [] }) do |validator, kind_hash|
31
- next nil unless can_use_for_client_side_validation?(attr[0], validator, force)
34
+ next unless can_use_for_client_side_validation?(attr[0], validator, force)
32
35
 
33
36
  client_side_hash = validator.client_side_hash(self, attr[0], extract_force_option(attr[0], force))
34
37
  if client_side_hash
@@ -68,10 +71,10 @@ module ClientSideValidations
68
71
  else
69
72
  result = can_force_validator?(attr, validator, force)
70
73
  if validator.options[:if]
71
- result &&= run_conditional(validator.options[:if])
74
+ result &&= run_conditionals(validator.options[:if], :if)
72
75
  end
73
76
  if validator.options[:unless]
74
- result &&= !run_conditional(validator.options[:unless])
77
+ result &&= run_conditionals(validator.options[:unless], :unless)
75
78
  end
76
79
  end
77
80
  end
@@ -79,14 +82,6 @@ module ClientSideValidations
79
82
  result
80
83
  end
81
84
 
82
- def run_conditional(method_name_value_or_proc)
83
- if method_name_value_or_proc.respond_to?(:call)
84
- method_name_value_or_proc.call self
85
- else
86
- send method_name_value_or_proc
87
- end
88
- end
89
-
90
85
  def validator_turned_off?(attr, validator, force)
91
86
  return true if ::ClientSideValidations::Config.disabled_validators.include?(validator.kind)
92
87
  case force
@@ -0,0 +1,41 @@
1
+ module ClientSideValidations
2
+ module ActiveModel
3
+ module Conditionals
4
+ private
5
+
6
+ def run_conditionals(conditionals, conditional_type)
7
+ Array.wrap(conditionals).all? do |conditional|
8
+ value = run_one_conditional(conditional)
9
+ if conditional_type == :unless
10
+ !value
11
+ else
12
+ value
13
+ end
14
+ end
15
+ end
16
+
17
+ def run_one_conditional(conditional)
18
+ case conditional
19
+ when ::Proc
20
+ case conditional.arity
21
+ when 0
22
+ instance_exec(&conditional)
23
+ when 1
24
+ instance_exec(self, &conditional)
25
+ else
26
+ raise ArgumentError, 'Missing argument'
27
+ end
28
+ when String
29
+ # rubocop:disable Lint/Eval'
30
+ l = eval "lambda { |value| #{conditional} }"
31
+ # rubocop:enable Lint/Eval'
32
+ instance_exec(nil, &l)
33
+ when Symbol
34
+ send conditional
35
+ else
36
+ raise ArgumentError, "Unknown conditional #{conditional}. If supported by ActiveModel/ActiveRecord open a bug for client_side_validations gem."
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ClientSideValidations
3
- VERSION = '4.2.10'.freeze
3
+ VERSION = '4.2.11'.freeze
4
4
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  /*!
3
- * Client Side Validations - v4.2.10 (https://github.com/DavyJonesLocker/client_side_validations)
3
+ * Client Side Validations - v4.2.11 (https://github.com/DavyJonesLocker/client_side_validations)
4
4
  * Copyright (c) 2016 Geremia Taglialatela, Brian Cardarella
5
5
  * Licensed under MIT (http://opensource.org/licenses/mit-license.php)
6
6
  */
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: client_side_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.10
4
+ version: 4.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geremia Taglialatela
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-16 00:00:00.000000000 Z
12
+ date: 2016-12-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -105,14 +105,14 @@ dependencies:
105
105
  requirements:
106
106
  - - "~>"
107
107
  - !ruby/object:Gem::Version
108
- version: 0.8.15
108
+ version: 0.8.17
109
109
  type: :development
110
110
  prerelease: false
111
111
  version_requirements: !ruby/object:Gem::Requirement
112
112
  requirements:
113
113
  - - "~>"
114
114
  - !ruby/object:Gem::Version
115
- version: 0.8.15
115
+ version: 0.8.17
116
116
  - !ruby/object:Gem::Dependency
117
117
  name: m
118
118
  requirement: !ruby/object:Gem::Requirement
@@ -167,28 +167,28 @@ dependencies:
167
167
  requirements:
168
168
  - - "~>"
169
169
  - !ruby/object:Gem::Version
170
- version: '11.3'
170
+ version: '12.0'
171
171
  type: :development
172
172
  prerelease: false
173
173
  version_requirements: !ruby/object:Gem::Requirement
174
174
  requirements:
175
175
  - - "~>"
176
176
  - !ruby/object:Gem::Version
177
- version: '11.3'
177
+ version: '12.0'
178
178
  - !ruby/object:Gem::Dependency
179
179
  name: rubocop
180
180
  requirement: !ruby/object:Gem::Requirement
181
181
  requirements:
182
182
  - - "~>"
183
183
  - !ruby/object:Gem::Version
184
- version: 0.45.0
184
+ version: 0.46.0
185
185
  type: :development
186
186
  prerelease: false
187
187
  version_requirements: !ruby/object:Gem::Requirement
188
188
  requirements:
189
189
  - - "~>"
190
190
  - !ruby/object:Gem::Version
191
- version: 0.45.0
191
+ version: 0.46.0
192
192
  - !ruby/object:Gem::Dependency
193
193
  name: simplecov
194
194
  requirement: !ruby/object:Gem::Requirement
@@ -291,6 +291,7 @@ files:
291
291
  - lib/client_side_validations/active_model.rb
292
292
  - lib/client_side_validations/active_model/absence.rb
293
293
  - lib/client_side_validations/active_model/acceptance.rb
294
+ - lib/client_side_validations/active_model/conditionals.rb
294
295
  - lib/client_side_validations/active_model/exclusion.rb
295
296
  - lib/client_side_validations/active_model/format.rb
296
297
  - lib/client_side_validations/active_model/inclusion.rb