Skip to content

Commit

Permalink
fix #435, allow for the :message option when adding an error
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Feb 10, 2019
1 parent 88972e5 commit 2bd61cb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
70 changes: 36 additions & 34 deletions lib/active_interaction/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,60 +96,62 @@ class Errors < ActiveModel::Errors
#
# @return [Errors]
def merge!(other)
if other.respond_to?(:details)
merge_details!(other)
else
merge_messages!(other)
end
if other.respond_to?(:details)
merge_details!(other)
else
merge_messages!(other)
end

self
self
end

private

def attribute?(attribute)
@base.respond_to?(attribute)
end

def detailed_error?(detail)
detail[:error].is_a?(Symbol)
end

def merge_messages!(other)
other.messages.each do |attribute, messages|
messages.each do |message|
unless attribute?(attribute)
message = full_message(attribute, message)
attribute = :base
end
add(attribute, message) unless added?(attribute, message)
merge_message!(attribute, message)
end
end
end

def merge_details!(other)
other.details.each do |attribute, details|
details.each do |detail|
detail = detail.dup
error = detail.delete(:error)

merge_detail!(other, attribute, detail, error)
end
def merge_message!(attribute, message)
unless attribute?(attribute)
message = full_message(attribute, message)
attribute = :base
end
add(attribute, message) unless added?(attribute, message)
end

def merge_detail!(other, attribute, detail, error)
if attribute?(attribute) || attribute == :base
add(attribute, error, detail) unless added?(attribute, error, detail)
else
translated_error = translate(other, attribute, error, detail)
message = full_message(attribute, translated_error)
attribute = :base
add(attribute, message) unless added?(attribute, message)
def merge_details!(other)
other.messages.each do |attribute, messages|
messages.zip(other.details[attribute]) do |message, detail|
if detailed_error?(detail)
merge_detail!(attribute, detail, message)
else
merge_message!(attribute, message)
end
end
end
end

def attribute?(attribute)
@base.respond_to?(attribute)
end
def merge_detail!(attribute, detail, message)
if attribute?(attribute) || attribute == :base
options = detail.dup
error = options.delete(:error)
options[:message] = message

def translate(other, attribute, error, detail)
if error.is_a?(Symbol)
other.generate_message(attribute, error, detail)
add(attribute, error, options) unless added?(attribute, error, options)
else
error
merge_message!(attribute, message)
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/active_interaction/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ def self.name
expect(errors.details[:base]).to eql [{ error: message }]
end
end

context 'that uses the :message option' do
let(:message) { SecureRandom.hex }
let(:error_name) { :some_error }

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

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

context 'with an interpolated detailed error' do
Expand Down

0 comments on commit 2bd61cb

Please sign in to comment.