From f2b59cc882b0a35ac283bf13d3fb17ee5c7db5ec Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Mon, 14 Sep 2020 11:37:38 -0500 Subject: [PATCH] Optimization:Only Enqueue UpdateMainImageBackgroundHexWorker If Main Image Changes (#10325) --- app/models/article.rb | 1 + spec/models/article_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/app/models/article.rb b/app/models/article.rb index a9b80f394..e6de3937f 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -422,6 +422,7 @@ class Article < ApplicationRecord end def update_main_image_background_hex + return unless saved_changes.key?("main_image") return if main_image.blank? || main_image_background_hex_color != "#dddddd" Articles::UpdateMainImageBackgroundHexWorker.perform_async(id) diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 9eff3a8f2..18b1edadf 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -765,6 +765,15 @@ RSpec.describe Article, type: :model do end expect(article).to have_received(:update_main_image_background_hex) end + + it "does not enqueue a job if main_image has not changed" do + article.save + allow(article).to receive(:update_main_image_background_hex).and_call_original + sidekiq_assert_no_enqueued_jobs(only: Articles::UpdateMainImageBackgroundHexWorker) do + article.save + end + expect(article).to have_received(:update_main_image_background_hex) + end end describe "async score calc" do