discourse/lib/has_errors.rb
Sam Saffron 1be01f8dd4 DEV: Add support for Rails 6
Minor fixes to add Rails 6 support to Discourse, we now will boot
with RAILS_MASTER=1, all specs pass

Only one tiny deprecation left

Largest change was the way ActiveModel:Errors changed interface a
bit but there is a simple backwards compat way of working it
2019-05-02 16:23:25 +10:00

42 lines
843 B
Ruby

# Helper functions for dealing with errors and objects that have
# child objects with errors
module HasErrors
attr_reader :errors
attr_accessor :forbidden, :not_found, :conflict
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)
obj.errors.add(:base, error)
rollback_from_errors!(obj)
end
def rollback_from_errors!(obj)
add_errors_from(obj)
raise ActiveRecord::Rollback.new
end
def add_error(msg)
errors.add(:base, msg) unless errors[:base].include?(msg)
end
def add_errors_from(obj)
return if obj.blank?
if obj.is_a?(StandardError)
return add_error(obj.message)
end
obj.errors.full_messages.each { |msg| add_error(msg) }
end
end