From 9383695c5b3e7109db3061d396145c26c5772442 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 4 May 2020 19:02:49 +1000 Subject: [PATCH] Add trigger post jobs when post is created --- lib/custom_wizard/action.rb | 35 ++++++++++++++++++++++-------- lib/custom_wizard/action_result.rb | 15 +++++++++++++ plugin.rb | 1 + 3 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 lib/custom_wizard/action_result.rb diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index cd85ec21..d85067bb 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -10,22 +10,19 @@ class CustomWizard::Action @user = params[:user] @data = params[:data] @log = [] + @result = CustomWizard::ActionResult.new end - def perform + def perform ActiveRecord::Base.transaction do self.send(action['type'].to_sym) end - - log = "wizard: #{@wizard.id}; action: #{action['type']}; user: #{user.username}" - - if @log.any? - @log.each do |item| - log << "; #{item.to_s}" - end + + if creates_post? && @result.success? + @result.handler.enqueue_jobs end - CustomWizard::Log.create(log) + save_log end def mapper @@ -51,6 +48,7 @@ class CustomWizard::Action if creator.errors.blank? log_success("created topic", "id: #{post.topic.id}") + result.handler = creator end else log_error("invalid topic params", "title: #{params[:title]}; post: #{params[:raw]}") @@ -88,6 +86,7 @@ class CustomWizard::Action if creator.errors.blank? log_success("created message", "id: #{post.topic.id}") + result.handler = creator end else log_error( @@ -320,6 +319,10 @@ class CustomWizard::Action add_custom_fields(params) end + def creates_post? + [:create_topic, :send_message].include?(action['type'].to_sym) + end + def profile_url_fields ['profile_background', 'card_background'] end @@ -371,13 +374,27 @@ class CustomWizard::Action def log_success(message, detail = nil) @log.push("success: #{message} - #{detail}") + @result.success = true end def log_error(message, detail = nil) @log.push("error: #{message} - #{detail}") + @result.success = false end def log_info(message, detail = nil) @log.push("info: #{message} - #{detail}") end + + def save_log + log = "wizard: #{@wizard.id}; action: #{action['type']}; user: #{user.username}" + + if @log.any? + @log.each do |item| + log << "; #{item.to_s}" + end + end + + CustomWizard::Log.create(log) + end end \ No newline at end of file diff --git a/lib/custom_wizard/action_result.rb b/lib/custom_wizard/action_result.rb new file mode 100644 index 00000000..c731e752 --- /dev/null +++ b/lib/custom_wizard/action_result.rb @@ -0,0 +1,15 @@ +class CustomWizard::ActionResult + attr_accessor :success, :handler + + def initialize + @success = false + end + + def success? + @success + end + + def failed? + !success + end +end diff --git a/plugin.rb b/plugin.rb index a6720d80..d4c04a99 100644 --- a/plugin.rb +++ b/plugin.rb @@ -55,6 +55,7 @@ after_initialize do ../jobs/clear_after_time_wizard.rb ../jobs/refresh_api_access_token.rb ../jobs/set_after_time_wizard.rb + ../lib/custom_wizard/action_result.rb ../lib/custom_wizard/action.rb ../lib/custom_wizard/builder.rb ../lib/custom_wizard/field.rb