2015-03-26 16:57:50 -04:00
|
|
|
# Helper functions for dealing with errors and objects that have
|
|
|
|
# child objects with errors
|
|
|
|
module HasErrors
|
2016-07-14 23:36:06 -04:00
|
|
|
attr_reader :errors
|
2015-03-26 16:57:50 -04:00
|
|
|
|
|
|
|
def errors
|
|
|
|
@errors ||= ActiveModel::Errors.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_child(obj)
|
|
|
|
return true if obj.valid?
|
|
|
|
add_errors_from(obj)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def rollback_with!(obj, error)
|
2016-05-05 08:58:25 -04:00
|
|
|
obj.errors.add(:base, error)
|
2015-03-26 16:57:50 -04:00
|
|
|
rollback_from_errors!(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
def rollback_from_errors!(obj)
|
|
|
|
add_errors_from(obj)
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_errors_from(obj)
|
|
|
|
obj.errors.full_messages.each do |msg|
|
|
|
|
errors[:base] << msg unless errors[:base].include?(msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|