Skip to content

Commit 7b9e7f1

Browse files
committed
Allow proc values in lenght validator options
Fix #997
1 parent f35f615 commit 7b9e7f1

File tree

7 files changed

+48
-6
lines changed

7 files changed

+48
-6
lines changed

.rubocop.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Layout/LineLength:
2727
Enabled: false
2828

2929
Metrics/AbcSize:
30-
Max: 21.91
30+
Max: 24.52
3131

3232
Metrics/BlockLength:
3333
Exclude:
@@ -87,3 +87,6 @@ Style/FormatStringToken:
8787

8888
Style/IfUnlessModifier:
8989
Enabled: false
90+
91+
Style/NumericPredicate:
92+
Enabled: false

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [FEATURE] Drop Internet Explorer and other older browsers support
1111
* [FEATURE] Drop Ruby < 3.1
1212
* [FEATURE] Drop jQuery < 3.7.1 Compatibility
13+
* [BUGFIX] Allow `proc` values in length validator options
1314
* [ENHANCEMENT] Update QUnit to 2.24.3
1415
* [ENHANCEMENT] Test against Ruby 3.4
1516

lib/client_side_validations/active_model.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def message_type
4545
def callbacks_options
4646
::ActiveModel::Error::CALLBACKS_OPTIONS
4747
end
48+
49+
def resolve_proc(value, object)
50+
if value.arity.zero?
51+
value.call
52+
else
53+
value.call(object)
54+
end
55+
end
4856
end
4957

5058
module Validations
@@ -172,7 +180,7 @@ def client_side_hash(model, attribute, force = nil)
172180
if options[:in].respond_to?(:call)
173181
return unless force
174182

175-
options[:in] = options[:in].call(model)
183+
options[:in] = resolve_proc(options[:in], model)
176184
end
177185

178186
hash = build_client_side_hash(model, attribute, options)

lib/client_side_validations/active_model/format.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ def client_side_hash(model, attribute, force = nil)
88
if options[:with].respond_to?(:call)
99
return unless force
1010

11-
options[:with] = options[:with].call(model)
11+
options[:with] = resolve_proc(options[:with], model)
1212
build_client_side_hash(model, attribute, options)
1313
elsif options[:without].respond_to?(:call)
1414
return unless force
1515

16-
options[:without] = options[:without].call(model)
16+
options[:without] = resolve_proc(options[:without], model)
1717
build_client_side_hash(model, attribute, options)
1818
else
1919
super

lib/client_side_validations/active_model/length.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
module ClientSideValidations
44
module ActiveModel
55
module Length
6-
def client_side_hash(model, attribute, _force = nil)
6+
def client_side_hash(model, attribute, force = nil)
77
options = self.options.dup
88
hash = options_hash(options)
99

1010
self.class::MESSAGES.each do |option, message_type|
1111
count = options[option]
1212
next unless count
1313

14+
if count.respond_to?(:call)
15+
next unless force
16+
17+
count = resolve_proc(count, model)
18+
end
19+
1420
options[:message] = options[message_type] if options[message_type].present?
1521
options.delete(:message) if options[:message].nil?
1622
hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(count: count))

lib/client_side_validations/active_model/numericality.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def client_side_hash(model, attribute, force = nil)
2323
if count.respond_to?(:call)
2424
next unless force
2525

26-
count = count.call(model)
26+
count = resolve_proc(count, model)
2727
end
2828

2929
hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(count: count))

test/active_model/cases/test_length_validator.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,29 @@ def test_length_client_side_hash_with_range
7070

7171
assert_equal expected_hash, LengthValidator.new(attributes: [:age], within: 5..10).client_side_hash(@person, :age)
7272
end
73+
74+
def test_length_client_side_hash_with_minimum_and_maximum_proc_force
75+
expected_hash = {
76+
messages: {
77+
minimum: 'is too short (minimum is 5 characters)',
78+
maximum: 'is too long (maximum is 10 characters)'
79+
},
80+
minimum: 5,
81+
maximum: 10
82+
}
83+
84+
assert_equal expected_hash, LengthValidator.new(attributes: [:first_name], minimum: proc { 5 }, maximum: proc { 10 }).client_side_hash(@person, :first_name, force: true)
85+
end
86+
87+
def test_length_client_side_hash_with_is_proc_force
88+
expected_hash = {
89+
messages: {
90+
is: 'is the wrong length (should be 10 characters)'
91+
},
92+
is: 10
93+
}
94+
95+
assert_equal expected_hash, LengthValidator.new(attributes: [:age], is: proc { 10 }).client_side_hash(@person, :first_name, force: true)
96+
end
7397
end
7498
end

0 commit comments

Comments
 (0)