Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback on "invalid" with missing translation #922

Merged
merged 1 commit into from
Oct 8, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 22.1.1 / 2023-10-08

* [BUGFIX] Fix a bug with missing translations ([#920](https://github.com/DavyJonesLocker/client_side_validations/issues/920))

## 22.1.0 / 2023-10-05

* [FEATURE] Rails 7.1 compatibility
Expand Down
19 changes: 18 additions & 1 deletion lib/client_side_validations/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,24 @@ def copy_conditional_attributes(attribute_to, attribute_from)
private

def build_client_side_hash(model, attribute, options)
{ message: model.errors.generate_message(attribute, message_type, options) }.merge(options.except(*callbacks_options - %i[allow_blank if unless]))
# Rails mutates `options` object when calling `model.errors.generate_message`
# by removing `message` option, if any.
# By raising on missing translations, CSV has the same behavior across
# all supported Rails versions and `config.i18n.raise_on_missing_translations`
# possible configurations.
options[:raise] = true

message =
begin
model.errors.generate_message(attribute, message_type, options)
rescue I18n::MissingTranslationData
options[:message] = :invalid
model.errors.generate_message(attribute, message_type, options)
end

options.delete(:raise)

{ message: message }.merge(options.except(*callbacks_options - %i[allow_blank if unless]))
end

def message_type
Expand Down
2 changes: 1 addition & 1 deletion lib/client_side_validations/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ClientSideValidations
VERSION = '22.1.0'
VERSION = '22.1.1'
end
14 changes: 14 additions & 0 deletions test/active_model/cases/test_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,20 @@ def test_multiple_validators_of_same_type_on_same_attribute
assert_equal expected_hash, person.client_side_validation_hash
end

def test_missing_translation
person = new_person do |p|
p.validates :first_name, custom_validation: true
end

expected_hash = {
first_name: {
custom_validation: [{ message: 'is invalid' }]
}
}

assert_equal expected_hash, person.client_side_validation_hash
end

def test_ignored_procs_validators
person = new_person do |p|
p.validates :first_name, format: proc { |o| o.matcher }
Expand Down
8 changes: 8 additions & 0 deletions test/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ class Application < Rails::Application

Rails.application.initialize!

class CustomValidationValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?

record.errors.add(attribute, :invalid, **options)
end
end

module ClientSideValidations; end