Skip to content

Commit

Permalink
fix #349 by not attempting to translate strings
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Jan 15, 2016
1 parent d371114 commit 50ac2af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
12 changes: 10 additions & 2 deletions lib/active_interaction/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def merge_detail!(other, attribute, detail, error)
if attribute?(attribute)
add(attribute, error, detail) unless added?(attribute, error, detail)
else
message = full_message(
attribute, other.generate_message(attribute, error))
translated_error = translate(other, attribute, error)
message = full_message(attribute, translated_error)
attribute = :base
add(attribute, message) unless added?(attribute, message)
end
Expand All @@ -131,5 +131,13 @@ def merge_detail!(other, attribute, detail, error)
def attribute?(attribute)
@base.respond_to?(attribute)
end

def translate(other, attribute, error)
if error.is_a?(Symbol)
other.generate_message(attribute, error)
else
error
end
end
end
end
25 changes: 20 additions & 5 deletions spec/active_interaction/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,28 @@ def self.name
end

context 'with a detailed error' do
before do
other.add(:attribute)
context 'that is a symbol' do
before do
other.add(:attribute)
end

it 'adds the error' do
errors.merge!(other)
expect(errors.details[:attribute]).to eql [{ error: :invalid }]
end
end

it 'adds the error' do
errors.merge!(other)
expect(errors.details[:attribute]).to eql [{ error: :invalid }]
context 'that is a string' do
let(:message) { SecureRandom.hex }

before do
other.add(:base, message)
end

it 'adds the error' do
errors.merge!(other)
expect(errors.details[:base]).to eql [{ error: message }]
end
end
end

Expand Down

0 comments on commit 50ac2af

Please sign in to comment.