mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
PostEnqueuer
object to handle validation of enqueued posts
This commit is contained in:
31
lib/has_errors.rb
Normal file
31
lib/has_errors.rb
Normal file
@ -0,0 +1,31 @@
|
||||
# Helper functions for dealing with errors and objects that have
|
||||
# child objects with errors
|
||||
module HasErrors
|
||||
|
||||
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[:base] << error
|
||||
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
|
Reference in New Issue
Block a user