Skip to content

Commit

Permalink
Move detailed errors into backports
Browse files Browse the repository at this point in the history
  • Loading branch information
tfausak committed Apr 29, 2015
1 parent 39f2dd7 commit 87a43d8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
48 changes: 48 additions & 0 deletions lib/active_interaction/backports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,53 @@ def []=(name, value)
class Errors
# Required for Rails < 3.2.13.
protected :initialize_dup

# Required for Rails < 5.
#
# Extracted from active_model-errors_details 1.2.0. Modified to add support
# for ActiveModel 3.2.0.
module Details
extend ActiveSupport::Concern

CALLBACKS_OPTIONS = ::ActiveModel::Errors::CALLBACKS_OPTIONS
private_constant :CALLBACKS_OPTIONS

included do
attr_reader :details

%w[initialize initialize_dup add clear delete].each do |method|
alias_method "#{method}_without_details", method
alias_method method, "#{method}_with_details"
end
end

def initialize_with_details(base)
@details = Hash.new { |details, attribute| details[attribute] = [] }
initialize_without_details(base)
end

def initialize_dup_with_details(other)
@details = other.details.deep_dup
initialize_dup_without_details(other)
end

def add_with_details(attribute, message = :invalid, options = {})
message = message.call if message.respond_to?(:call)
error = options.except(*CALLBACKS_OPTIONS).merge(error: message)
details[attribute].push(error)
add_without_details(attribute, message, options)
end

def clear_with_details
details.clear
clear_without_details
end

def delete_with_details(attribute)
details.delete(attribute)
delete_without_details(attribute)
end
end
include Details unless method_defined?(:details)
end
end
47 changes: 0 additions & 47 deletions lib/active_interaction/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,53 +91,6 @@ def initialize(outcome)

# An extension that provides the ability to merge other errors into itself.
class Errors < ActiveModel::Errors
# Extracted from active_model-errors_details version 1.2.0. Modified to add
# support for ActiveModel version 3.2.0.
module Details
extend ActiveSupport::Concern

CALLBACKS_OPTIONS = ::ActiveModel::Errors::CALLBACKS_OPTIONS
private_constant :CALLBACKS_OPTIONS

included do
attr_reader :details

%w[initialize initialize_dup add clear delete].each do |method|
alias_method "#{method}_without_details", method
alias_method method, "#{method}_with_details"
end
end

def initialize_with_details(base)
@details = Hash.new { |details, attribute| details[attribute] = [] }
initialize_without_details(base)
end

def initialize_dup_with_details(other)
@details = other.details.deep_dup
initialize_dup_without_details(other)
end

def add_with_details(attribute, message = :invalid, options = {})
message = message.call if message.respond_to?(:call)
error = options.except(*CALLBACKS_OPTIONS).merge(error: message)
details[attribute].push(error)
add_without_details(attribute, message, options)
end

def clear_with_details
details.clear
clear_without_details
end

def delete_with_details(attribute)
details.delete(attribute)
delete_without_details(attribute)
end
end

include Details

# Merge other errors into this one.
#
# @param other [Errors]
Expand Down

0 comments on commit 87a43d8

Please sign in to comment.