diff --git a/app/models/article.rb b/app/models/article.rb index 146bd79dd..1878ee420 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -8,6 +8,7 @@ class Article < ApplicationRecord SEARCH_SERIALIZER = Search::ArticleSerializer SEARCH_CLASS = Search::FeedContent + DATA_SYNC_CLASS = DataSync::Elasticsearch::Article acts_as_taggable_on :tags resourcify @@ -100,8 +101,10 @@ class Article < ApplicationRecord after_update_commit :update_notifications, if: proc { |article| article.notifications.any? && !article.saved_changes.empty? } + after_commit :async_score_calc, :touch_collection, on: %i[create update] after_commit :index_to_elasticsearch, on: %i[create update] + after_commit :sync_related_elasticsearch_docs, on: %i[update] after_commit :remove_from_elasticsearch, on: [:destroy] serialize :cached_user diff --git a/app/serializers/search/comment_serializer.rb b/app/serializers/search/comment_serializer.rb index ca1fa14f3..13429d375 100644 --- a/app/serializers/search/comment_serializer.rb +++ b/app/serializers/search/comment_serializer.rb @@ -9,8 +9,8 @@ module Search comment.class.name end attribute :hotness_score, &:score - attribute :published do |_comment| - true + attribute :published do |comment| + comment.commentable&.published end attribute :published_at, &:created_at attribute :readable_publish_date_string, &:readable_publish_date diff --git a/app/services/data_sync/elasticsearch/article.rb b/app/services/data_sync/elasticsearch/article.rb new file mode 100644 index 000000000..abc03c709 --- /dev/null +++ b/app/services/data_sync/elasticsearch/article.rb @@ -0,0 +1,16 @@ +module DataSync + module Elasticsearch + class Article < Base + RELATED_DOCS = %i[ + comments + ].freeze + + SHARED_FIELDS = %i[ + published + title + ].freeze + + delegate :comments, to: :@updated_record + end + end +end diff --git a/lib/data_update_scripts/20210118194138_resync_unpublished_articles_comments_elasticsearch_document.rb b/lib/data_update_scripts/20210118194138_resync_unpublished_articles_comments_elasticsearch_document.rb new file mode 100644 index 000000000..5160419ff --- /dev/null +++ b/lib/data_update_scripts/20210118194138_resync_unpublished_articles_comments_elasticsearch_document.rb @@ -0,0 +1,9 @@ +module DataUpdateScripts + class ResyncUnpublishedArticlesCommentsElasticsearchDocument + def run + Article.unpublished.where.not(comments_count: 0).each do |article| + article.comments.each(&:index_to_elasticsearch) + end + end + end +end diff --git a/spec/lib/data_update_scripts/resync_unpublished_articles_comments_elasticsearch_document_spec.rb b/spec/lib/data_update_scripts/resync_unpublished_articles_comments_elasticsearch_document_spec.rb new file mode 100644 index 000000000..60c5705f3 --- /dev/null +++ b/spec/lib/data_update_scripts/resync_unpublished_articles_comments_elasticsearch_document_spec.rb @@ -0,0 +1,17 @@ +require "rails_helper" +require Rails.root.join( + "lib/data_update_scripts/20210118194138_resync_unpublished_articles_comments_elasticsearch_document.rb", +) + +describe DataUpdateScripts::ResyncUnpublishedArticlesCommentsElasticsearchDocument do + it "works" do + article = create(:article) + comment = create(:comment, commentable: article) + sidekiq_perform_enqueued_jobs + article.update_column(:published, false) + + sidekiq_assert_enqueued_with(job: Search::IndexWorker, args: ["Comment", comment.id]) do + described_class.new.run + end + end +end diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 6654ad632..00d5fd2ed 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -139,6 +139,12 @@ RSpec.describe Article, type: :model do article.destroy end end + + it "on update syncs elasticsearch data" do + allow(article).to receive(:sync_related_elasticsearch_docs) + article.save + expect(article).to have_received(:sync_related_elasticsearch_docs) + end end context "when published" do diff --git a/spec/services/data_sync/elasticsearch/article_spec.rb b/spec/services/data_sync/elasticsearch/article_spec.rb new file mode 100644 index 000000000..f28ae30b8 --- /dev/null +++ b/spec/services/data_sync/elasticsearch/article_spec.rb @@ -0,0 +1,8 @@ +require "rails_helper" + +RSpec.describe DataSync::Elasticsearch::Article, type: :service do + it "defines necessary constants" do + expect(described_class::RELATED_DOCS).not_to be_nil + expect(described_class::SHARED_FIELDS).not_to be_nil + end +end