ensure that when Tag and ActsAsTaggableOn::Tag are updated their respective related docs are as well (#7218)

This commit is contained in:
Molly Struve 2020-04-14 08:38:09 -05:00 committed by GitHub
parent ea1c27f6be
commit 91972dd2c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 1 deletions

View file

@ -1,7 +1,11 @@
module ActsAsTaggableOn
class Tag
after_commit on: :create do
after_commit on: %i[create update] do
::Tag.find(id).index_to_elasticsearch
end
after_commit on: :update do
DataSync::Elasticsearch::Tag.new(self).call
end
end
end

View file

@ -33,11 +33,13 @@ class Tag < ActsAsTaggableOn::Tag
after_commit :bust_cache
after_commit :index_to_elasticsearch, on: %i[create update]
after_commit :sync_related_elasticsearch_docs, on: [:update]
after_commit :remove_from_elasticsearch, on: [:destroy]
include Searchable
SEARCH_SERIALIZER = Search::TagSerializer
SEARCH_CLASS = Search::Tag
DATA_SYNC_CLASS = DataSync::Elasticsearch::Tag
# This model doesn't inherit from ApplicationRecord so this has to be included
include Purgeable

View file

@ -0,0 +1,31 @@
module DataSync
module Elasticsearch
class Tag < Base
RELATED_DOCS = %i[
articles
podcast_episodes
reactions
].freeze
SHARED_FIELDS = %i[
keywords_for_search
].freeze
private
def articles
::Article.cached_tagged_with(updated_record.name)
end
def reactions
::Reaction.readinglist.where(reactable: articles)
end
def podcast_episodes
::PodcastEpisode.where(
id: updated_record.taggings.where(taggable_type: "PodcastEpisode").select(:taggable_id),
)
end
end
end
end

View file

@ -9,5 +9,27 @@ RSpec.describe ActsAsTaggableOn::Tag, type: :lib do
tag = Tag.find_by(name: tag_name)
expect(tag.elasticsearch_doc).not_to be_nil
end
it "syncs related elasticsearch documents" do
article = create(:article)
podcast_episode = create(:podcast_episode)
tag = article.tags.first
podcast_episode.tags << tag
reaction = create(:reaction, reactable: article, category: "readinglist")
new_keywords = "keyword1, keyword2, keyword3"
sidekiq_perform_enqueued_jobs
tag.update(keywords_for_search: new_keywords)
sidekiq_perform_enqueued_jobs
expect(collect_keywords(article)).to include(new_keywords)
expect(
reaction.elasticsearch_doc.dig("_source", "reactable", "tags").flat_map { |t| t["keywords_for_search"] },
).to include(new_keywords)
expect(collect_keywords(podcast_episode)).to include(new_keywords)
end
end
def collect_keywords(record)
record.elasticsearch_doc.dig("_source", "tags").flat_map { |t| t["keywords_for_search"] }
end
end

View file

@ -103,5 +103,27 @@ RSpec.describe Tag, type: :model do
tag.destroy
end
end
it "syncs related elasticsearch documents" do
article = create(:article)
podcast_episode = create(:podcast_episode)
tag = described_class.find(article.tags.first.id)
podcast_episode.tags << tag
reaction = create(:reaction, reactable: article, category: "readinglist")
new_keywords = "keyword1, keyword2, keyword3"
sidekiq_perform_enqueued_jobs
tag.update(keywords_for_search: new_keywords)
sidekiq_perform_enqueued_jobs
expect(collect_keywords(article)).to include(new_keywords)
expect(
reaction.elasticsearch_doc.dig("_source", "reactable", "tags").flat_map { |t| t["keywords_for_search"] },
).to include(new_keywords)
expect(collect_keywords(podcast_episode)).to include(new_keywords)
end
end
def collect_keywords(record)
record.elasticsearch_doc.dig("_source", "tags").flat_map { |t| t["keywords_for_search"] }
end
end

View file

@ -0,0 +1,8 @@
require "rails_helper"
RSpec.describe DataSync::Elasticsearch::Tag, 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