Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Layout/LineLength:
Enabled: false

Metrics/AbcSize:
Max: 21.91
Max: 24.52

Metrics/BlockLength:
Exclude:
Expand Down Expand Up @@ -87,3 +87,6 @@ Style/FormatStringToken:

Style/IfUnlessModifier:
Enabled: false

Style/NumericPredicate:
Enabled: false
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [FEATURE] Drop Internet Explorer and other older browsers support
* [FEATURE] Drop Ruby < 3.1
* [FEATURE] Drop jQuery < 3.7.1 Compatibility
* [BUGFIX] Allow `proc` values in length validator options
* [ENHANCEMENT] Update QUnit to 2.24.3
* [ENHANCEMENT] Test against Ruby 3.4

Expand Down
10 changes: 9 additions & 1 deletion lib/client_side_validations/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def message_type
def callbacks_options
::ActiveModel::Error::CALLBACKS_OPTIONS
end

def resolve_proc(value, object)
if value.arity == 0
value.call
else
value.call(object)
end
end
end

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

options[:in] = options[:in].call(model)
options[:in] = resolve_proc(options[:in], model)
end

hash = build_client_side_hash(model, attribute, options)
Expand Down
4 changes: 2 additions & 2 deletions lib/client_side_validations/active_model/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def client_side_hash(model, attribute, force = nil)
if options[:with].respond_to?(:call)
return unless force

options[:with] = options[:with].call(model)
options[:with] = resolve_proc(options[:with], model)
build_client_side_hash(model, attribute, options)
elsif options[:without].respond_to?(:call)
return unless force

options[:without] = options[:without].call(model)
options[:without] = resolve_proc(options[:without], model)
build_client_side_hash(model, attribute, options)
else
super
Expand Down
8 changes: 7 additions & 1 deletion lib/client_side_validations/active_model/length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
module ClientSideValidations
module ActiveModel
module Length
def client_side_hash(model, attribute, _force = nil)
def client_side_hash(model, attribute, force = nil)
options = self.options.dup
hash = options_hash(options)

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

if count.respond_to?(:call)
next unless force

count = resolve_proc(count, model)
end

options[:message] = options[message_type] if options[message_type].present?
options.delete(:message) if options[:message].nil?
hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(count: count))
Expand Down
2 changes: 1 addition & 1 deletion lib/client_side_validations/active_model/numericality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def client_side_hash(model, attribute, force = nil)
if count.respond_to?(:call)
next unless force

count = count.call(model)
count = resolve_proc(count, model)
end

hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(count: count))
Expand Down
24 changes: 24 additions & 0 deletions test/active_model/cases/test_length_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,29 @@ def test_length_client_side_hash_with_range

assert_equal expected_hash, LengthValidator.new(attributes: [:age], within: 5..10).client_side_hash(@person, :age)
end

def test_length_client_side_hash_with_minimum_and_maximum_proc_force
expected_hash = {
messages: {
minimum: 'is too short (minimum is 5 characters)',
maximum: 'is too long (maximum is 10 characters)'
},
minimum: 5,
maximum: 10
}

assert_equal expected_hash, LengthValidator.new(attributes: [:age], minimum: proc { 5 }, maximum: proc { 10 }).client_side_hash(@person, :age, force: true)
end

def test_length_client_side_hash_with_is_proc_force
expected_hash = {
messages: {
is: 'is the wrong length (should be 10 characters)'
},
is: 10
}

assert_equal expected_hash, LengthValidator.new(attributes: [:age], is: proc { 10 }).client_side_hash(@person, :age, force: true)
end
end
end