From 4a75da4fa9faaac9aea36fe4f9d7c775a12d5724 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 11 Aug 2015 15:46:21 +1000 Subject: [PATCH] Extensibility: allow validation of topics to be extended Add before_create_topic and after_validate_topic extensibility points --- lib/topic_creator.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index 413144431..cff51e7c8 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -1,6 +1,9 @@ require_dependency 'has_errors' class TopicCreator + + attr_reader :user, :guardian, :opts + include HasErrors def self.create(user, guardian, opts) @@ -16,11 +19,21 @@ class TopicCreator def valid? topic = Topic.new(setup_topic_params) - validate_child(topic) + # validate? will clear the error hash + # so we fire the validation event after + # this allows us to add errors + valid = topic.valid? + DiscourseEvent.trigger(:after_validate_topic, topic, self) + valid &&= topic.errors.empty? + + add_errors_from(topic) unless valid + + valid end def create topic = Topic.new(setup_topic_params) + DiscourseEvent.trigger(:before_create_topic, topic, self) setup_auto_close_time(topic) process_private_message(topic)