diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index dd317c2b1aa..f9e3d3de7de 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -45,7 +45,7 @@ def create set_wip update_published_at if @product.save - Newspaper.publish(:product_create, { product: @product }) + ActiveSupport::Notifications.instrument('product.create', product: @product) Newspaper.publish(:product_save, { product: @product }) redirect_to Redirection.determin_url(self, @product), notice: notice_message(@product, :create) else diff --git a/app/models/product_author_watcher.rb b/app/models/product_author_watcher.rb index d92cee7bb41..ca3a0c57a61 100644 --- a/app/models/product_author_watcher.rb +++ b/app/models/product_author_watcher.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true class ProductAuthorWatcher - def call(payload) + def call(_name, _started, _finished, _unique_id, payload) product = payload[:product] - Watch.create!(user: product.user, watchable: product) + Watch.find_or_create_by!(user: product.user, watchable: product) end end diff --git a/app/models/product_notifier_for_colleague.rb b/app/models/product_notifier_for_colleague.rb index 5ac9e18fad3..26504562952 100644 --- a/app/models/product_notifier_for_colleague.rb +++ b/app/models/product_notifier_for_colleague.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class ProductNotifierForColleague - def call(payload) + def call(_name, _started, _finished, _unique_id, payload) product = payload[:product] return if product.wip diff --git a/app/models/product_notifier_for_practice_watcher.rb b/app/models/product_notifier_for_practice_watcher.rb index 243b873820a..856253ddac4 100644 --- a/app/models/product_notifier_for_practice_watcher.rb +++ b/app/models/product_notifier_for_practice_watcher.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class ProductNotifierForPracticeWatcher - def call(payload) + def call(_name, _started, _finished, _unique_id, payload) product = payload[:product] return if product.wip diff --git a/config/initializers/active_support_notifications.rb b/config/initializers/active_support_notifications.rb index c234f5531e8..00470e88461 100644 --- a/config/initializers/active_support_notifications.rb +++ b/config/initializers/active_support_notifications.rb @@ -14,4 +14,7 @@ ActiveSupport::Notifications.subscribe('article.create', ArticleNotifier.new) ActiveSupport::Notifications.subscribe('article.destroy', ArticleNotificationDestroyer.new) ActiveSupport::Notifications.subscribe('work.create', WorkNotifier.new) + ActiveSupport::Notifications.subscribe('product.create', ProductAuthorWatcher.new) + ActiveSupport::Notifications.subscribe('product.create', ProductNotifierForColleague.new) + ActiveSupport::Notifications.subscribe('product.create', ProductNotifierForPracticeWatcher.new) end diff --git a/config/initializers/newspaper.rb b/config/initializers/newspaper.rb index b4692be8acf..82f261cdfcf 100644 --- a/config/initializers/newspaper.rb +++ b/config/initializers/newspaper.rb @@ -25,8 +25,6 @@ Newspaper.subscribe(:comeback_update, ComebackNotifier.new) - Newspaper.subscribe(:product_create, ProductAuthorWatcher.new) - learning_status_updater = LearningStatusUpdater.new Newspaper.subscribe(:check_create, learning_status_updater) Newspaper.subscribe(:product_save, learning_status_updater) @@ -36,10 +34,6 @@ Newspaper.subscribe(:page_create, page_notifier) Newspaper.subscribe(:page_update, page_notifier) - Newspaper.subscribe(:product_save, ProductNotifierForColleague.new) - - Newspaper.subscribe(:product_save, ProductNotifierForPracticeWatcher.new) - mentors_watch_for_question_creator = MentorsWatchForQuestionCreator.new Newspaper.subscribe(:question_create, mentors_watch_for_question_creator) Newspaper.subscribe(:question_update, mentors_watch_for_question_creator) diff --git a/test/models/product_notifier_for_practice_watcher_test.rb b/test/models/product_notifier_for_practice_watcher_test.rb index 6e27188b383..265bd86924b 100644 --- a/test/models/product_notifier_for_practice_watcher_test.rb +++ b/test/models/product_notifier_for_practice_watcher_test.rb @@ -23,8 +23,15 @@ class ProductNotifierForPracticeWatcherTest < ActiveSupport::TestCase ) product.save! + payload = { product: } perform_enqueued_jobs do - ProductNotifierForPracticeWatcher.new.call({ product: }) + ProductNotifierForPracticeWatcher.new.call( + 'product_create', + Time.current, + Time.current, + 'unique_id', + payload + ) end assert Notification.where(user_id: watch.user_id, sender_id: product.user_id, message: "#{product.user.login_name}さんが「#{product.practice.title}」の提出物を提出しました。").exists?