Skip to content

Commit df5c615

Browse files
authored
Merge pull request #105 from validates-email-format-of/fix-i18n-key-handling
2 parents f6ab58a + 625fe8b commit df5c615

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

lib/validates_email_format_of/active_model.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module ActiveModel
99
module Validations
1010
class EmailFormatValidator < EachValidator
1111
def validate_each(record, attribute, value)
12-
(ValidatesEmailFormatOf.validate_email_format(value, options.merge(generate_message: true)) || []).each do |error|
12+
(ValidatesEmailFormatOf.validate_email_format(value, options) || []).each do |error|
1313
record.errors.add(attribute, error)
1414
end
1515
end

spec/spec_helper.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
require "byebug"
55

66
RSpec::Matchers.define :have_errors_on_email do
7-
match do |actual|
7+
match do |user|
8+
actual = user.errors.full_messages
9+
expect(user.errors.added?(:email, ValidatesEmailFormatOf::ERROR_MESSAGE_I18N_KEY))
810
expect(actual).not_to be_nil, "#{actual} should not be nil"
911
expect(actual).not_to be_empty, "#{actual} should not be empty"
1012
expect(actual.size).to eq(@reasons.size), "#{actual} should have #{@reasons.size} elements"
@@ -19,7 +21,7 @@
1921
chain :and_because do |reason|
2022
(@reasons ||= []) << reason
2123
end
22-
match_when_negated do |actual|
23-
expect(actual).to(be_empty)
24+
match_when_negated do |user|
25+
expect(user.errors).to(be_empty)
2426
end
2527
end

spec/validates_email_format_of_spec.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def self.model_name
2222
ActiveModel::Name.new(self, nil, "User")
2323
end
2424
end
25-
user.new(example.example_group_instance.email).tap(&:valid?).errors.full_messages
25+
user.new(example.example_group_instance.email).tap(&:valid?)
2626
end
2727
let(:options) { {} }
2828
let(:email) { |example| example.example_group.description }
@@ -248,13 +248,17 @@ def self.model_name
248248
let(:mx_record) { [] }
249249
describe email do
250250
it { should have_errors_on_email.because("is not routable") }
251+
it "adds the i18n key" do
252+
subject.errors.added?(:email, ValidatesEmailFormatOf::ERROR_MX_MESSAGE_I18N_KEY)
253+
end
251254
end
252255
describe "with a custom error message" do
253256
let(:options) { {check_mx: true, mx_message: "There ain't no such domain!"} }
254257
describe email do
255258
it { should have_errors_on_email.because("There ain't no such domain!") }
256259
end
257260
end
261+
258262
describe "i18n" do
259263
before(:each) do
260264
allow(I18n.config).to receive(:locale).and_return(locale)
@@ -383,7 +387,7 @@ def self.model_name
383387
ActiveModel::Name.new(self, nil, "User")
384388
end
385389
end
386-
user.new(example.example_group_instance.email).tap(&:valid?).errors.full_messages
390+
user.new(example.example_group_instance.email).tap(&:valid?)
387391
end
388392

389393
it_should_behave_like :all_specs

0 commit comments

Comments
 (0)