judge 2.1.1 → 3.1.0

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: fcf1e3ed1bb5500b69d1cc9e5074e56f43e86a46
4
- data.tar.gz: 9536e4c10547ccf7abbcb7dd6e83699096f7c282
3
+ metadata.gz: dca4cf9efce355af38a266acbebedbf4e9a248bc
4
+ data.tar.gz: 8d7c349e62de4f849f624a4ad752c15ca5ea4697
5
5
  SHA512:
6
- metadata.gz: 208177747be57442d0ff59bf79b86ceda1a675c40ae2d28f9b883a40da6b7fd8f7281b746074138e3c9e885d47a78ec31b29f424bd971f3816527e9c9156b90f
7
- data.tar.gz: b7893ca20ab01d91c325de6585642177af687a2065ca2931543f45a73459e8628063bfd535f77bbef8cd7916eb3673ba9abc9ebfaeacfa35c5aad8d99b387921
6
+ metadata.gz: a86cf16382daebc3d30cb73b27505f388d48dda9129c9e599c626d2672b89df8ba1bb9839562951dd6015e943a1ddb2f77ae597308cef7162862b9ad0a8b42a7
7
+ data.tar.gz: a7bf16c54d06e2546a7dbc7b4b069e37fdcfcaf3a555812e07bf7ea9fa64799dad5cc438994d562046e4205444fa717a247a850a4510cb272ce57029fb679afb
data/README.md CHANGED
@@ -12,7 +12,8 @@ In many cases it would be simpler to safely expose the validation information fr
12
12
 
13
13
  ## Installation
14
14
 
15
- Judge supports Rails >= 3.1.
15
+ Judge supports Rails 5.0+.
16
+ If you require Rails 4 support, please use version 2.1.x.
16
17
 
17
18
  Judge relies on [Underscore.js](underscore) in general and [JSON2.js](json2) for browsers that lack proper JSON support. If your application already makes use of these files, you can safely ignore the versions provided with Judge.
18
19
 
@@ -116,7 +117,48 @@ end
116
117
  * uniqueness;
117
118
  * any `EachValidator` that you have written, provided you add a JavaScript version too and add it to `judge.eachValidators`.
118
119
 
119
- The *allow_blank* option is available everywhere it should be. Error messages are looked up according to the [Rails i18n API](http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models).
120
+ The *allow_blank* option is available everywhere it should be.
121
+
122
+ ### Error messages
123
+ Error messages are looked up according to the [Rails i18n API](http://guides.rubyonrails.org/i18n.html#error-message-interpolation).
124
+
125
+ To override error messages, add entries to `config/locales/en.yml` for the messages you'd like to customize. Here's an example with Rails default values:
126
+ ```
127
+ en:
128
+ errors:
129
+ format: "%{attribute} %{message}"
130
+ messages:
131
+ accepted: must be accepted
132
+ blank: can't be blank
133
+ present: must be blank
134
+ confirmation: doesn't match %{attribute}
135
+ empty: can't be empty
136
+ equal_to: must be equal to %{count}
137
+ even: must be even
138
+ exclusion: is reserved
139
+ greater_than: must be greater than %{count}
140
+ greater_than_or_equal_to: must be greater than or equal to %{count}
141
+ inclusion: is not included in the list
142
+ invalid: is invalid
143
+ less_than: must be less than %{count}
144
+ less_than_or_equal_to: must be less than or equal to %{count}
145
+ model_invalid: "Validation failed: %{errors}"
146
+ not_a_number: is not a number
147
+ not_an_integer: must be an integer
148
+ odd: must be odd
149
+ required: must exist
150
+ taken: has already been taken
151
+ too_long:
152
+ one: is too long (maximum is 1 character)
153
+ other: is too long (maximum is %{count} characters)
154
+ too_short:
155
+ one: is too short (minimum is 1 character)
156
+ other: is too short (minimum is %{count} characters)
157
+ wrong_length:
158
+ one: is the wrong length (should be 1 character)
159
+ other: is the wrong length (should be %{count} characters)
160
+ other_than: must be other than %{count}
161
+ ```
120
162
 
121
163
  ## Validating uniqueness
122
164
 
@@ -321,6 +363,6 @@ $ bundle exec rake appraisal
321
363
 
322
364
  ## Credit
323
365
 
324
- Created by [Joe Corcoran](mailto:joecorcoran@gmail.com), whose [blog](http://blog.joecorcoran.co.uk) and [tweets](http://twitter.com/josephcorcoran) you can read on the world wide internet. Thank you to every user, email corresponder and pull request submitter.
366
+ Created by [Joe Corcoran](https://corcoran.io). Thank you to every user, email corresponder and pull request submitter.
325
367
 
326
368
  [Released under an MIT license](https://github.com/joecorcoran/judge/blob/master/LICENSE.txt).
@@ -0,0 +1 @@
1
+ //= link javascripts/judge.js
data/lib/judge/config.rb CHANGED
@@ -6,9 +6,10 @@ module Judge
6
6
 
7
7
  @@exposed = {}
8
8
  @@ignore_unsupported_validators = false
9
+ @@use_association_name_for_validations = false
9
10
 
10
11
  def expose(klass, *attributes)
11
- attrs = (@@exposed[klass] ||= [])
12
+ attrs = (@@exposed[klass.name] ||= [])
12
13
  attrs.concat(attributes).uniq!
13
14
  end
14
15
 
@@ -17,15 +18,15 @@ module Judge
17
18
  end
18
19
 
19
20
  def exposed?(klass, attribute)
20
- @@exposed.has_key?(klass) && @@exposed[klass].include?(attribute)
21
+ @@exposed.has_key?(klass.name) && @@exposed[klass.name].include?(attribute)
21
22
  end
22
23
 
23
24
  def unexpose(klass, *attributes)
24
25
  attributes.each do |a|
25
- @@exposed[klass].delete(a)
26
+ @@exposed[klass.name].delete(a)
26
27
  end
27
- if attributes.empty? || @@exposed[klass].empty?
28
- @@exposed.delete(klass)
28
+ if attributes.empty? || @@exposed[klass.name].empty?
29
+ @@exposed.delete(klass.name)
29
30
  end
30
31
  end
31
32
 
@@ -36,6 +37,14 @@ module Judge
36
37
  def ignore_unsupported_validators?
37
38
  @@ignore_unsupported_validators
38
39
  end
40
+
41
+ def use_association_name_for_validations(status)
42
+ @@use_association_name_for_validations = status
43
+ end
44
+
45
+ def use_association_name_for_validations?
46
+ @@use_association_name_for_validations
47
+ end
39
48
  end
40
49
 
41
50
  def self.config
@@ -45,4 +54,4 @@ module Judge
45
54
  def self.configure(&block)
46
55
  Config.instance.instance_eval(&block)
47
56
  end
48
- end
57
+ end
@@ -1,5 +1,3 @@
1
- require 'uri'
2
-
3
1
  module Judge
4
2
  module Controller
5
3
 
@@ -41,9 +39,9 @@ module Judge
41
39
  params = params.dup.keep_if {|k| REQUIRED_PARAMS.include?(k) || (k == 'original_value' && params[:kind] == 'uniqueness')}
42
40
  params[:klass] = find_klass(params[:klass]) if params[:klass]
43
41
  params[:attribute] = params[:attribute].to_sym if params[:attribute]
44
- params[:value] = URI.decode(params[:value]) if params[:value]
42
+ params[:value] = CGI::unescape(params[:value]) if params[:value]
45
43
  params[:kind] = params[:kind].to_sym if params[:kind]
46
- params[:original_value] = URI.decode(params[:original_value]) if params[:original_value]
44
+ params[:original_value] = CGI::unescape(params[:original_value]) if params[:original_value]
47
45
  params
48
46
  end
49
47
 
@@ -4,7 +4,7 @@ module Judge
4
4
 
5
5
  include Judge::Html
6
6
 
7
- %w{text_field text_area password_field email_field}.each do |type|
7
+ %w{text_field text_area password_field email_field telephone_field}.each do |type|
8
8
  helper = <<-END
9
9
  def #{type}(method, options = {})
10
10
  add_validate_attr!(self.object, method, options)
@@ -33,7 +33,7 @@ module Judge
33
33
  end
34
34
 
35
35
  def as_json(options = {})
36
- record.errors.get(@attribute) || []
36
+ record.errors[@attribute] || []
37
37
  end
38
38
  end
39
39
 
@@ -51,4 +51,4 @@ module Judge
51
51
  end
52
52
  end
53
53
 
54
- end
54
+ end
@@ -33,7 +33,23 @@ module Judge
33
33
  # unsupported by Judge
34
34
  # if it's a confirmation field, an AM::V like class is added to handle the confirmation validations
35
35
  def amvs
36
- amvs = object.class.validators_on(method)
36
+ method_to_search = method
37
+
38
+ if Judge.config.use_association_name_for_validations?
39
+ # since the method that gets passed in here for associations comes in the form of the generated form attribute
40
+ # i.e. :wine_id or :acclaim_ids
41
+ # object.class.validators_on(:wine_id) will fail if the active model validation is on the association directly
42
+ # this ensures that validations defined as 'validates :wine, presence: true' still get applied
43
+ # and client side error messages get generated
44
+ regex_for_assocations = /_id|_ids/
45
+ if method.to_s =~ regex_for_assocations
46
+ parsed_method = method.to_s.gsub(regex_for_assocations, '');
47
+ reflection = find_association_reflection(parsed_method)
48
+ method_to_search = reflection.name if reflection
49
+ end
50
+ end
51
+
52
+ amvs = object.class.validators_on(method_to_search)
37
53
  amvs = amvs.reject { |amv| reject?(amv) || amv.class.name['ConfirmationValidator'] }
38
54
  amvs = amvs.reject { |amv| unsupported_options?(amv) && reject?(amv) != false } if Judge.config.ignore_unsupported_validators?
39
55
  amvs << Judge::ConfirmationValidator.new(object, method) if is_confirmation?
@@ -41,6 +57,12 @@ module Judge
41
57
  amvs
42
58
  end
43
59
 
60
+ def find_association_reflection(association)
61
+ if object.class.respond_to?(:reflect_on_association)
62
+ object.class.reflect_on_association(association) || object.class.reflect_on_association(association.pluralize)
63
+ end
64
+ end
65
+
44
66
  def unsupported_options?(amv)
45
67
  unsupported = !(amv.options.keys & UNSUPPORTED_OPTIONS).empty?
46
68
  return false unless unsupported
data/lib/judge/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Judge
2
- VERSION = '2.1.1'
2
+ VERSION = '3.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: judge
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Corcoran
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-24 00:00:00.000000000 Z
11
+ date: 2023-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.1'
19
+ version: '5.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '3.1'
26
+ version: '5.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec-rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '3.1'
33
+ version: 4.0.1
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '3.1'
40
+ version: 4.0.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec-extra-formatters
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +116,7 @@ extra_rdoc_files: []
116
116
  files:
117
117
  - LICENSE.txt
118
118
  - README.md
119
+ - app/assets/config/manifest.js
119
120
  - app/assets/javascripts/judge.js
120
121
  - app/controllers/judge/validations_controller.rb
121
122
  - config/routes.rb
@@ -158,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
159
  version: '0'
159
160
  requirements: []
160
161
  rubyforge_project:
161
- rubygems_version: 2.4.3
162
+ rubygems_version: 2.5.2.3
162
163
  signing_key:
163
164
  specification_version: 4
164
165
  summary: Client side validation for Rails