Add AlgoliaSearchable to Comment (#20899)
* Add AlgoliaSearchable to Comment * Improve test * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Refinement * Fix broken spec * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
d5ba407629
commit
7890944a9d
7 changed files with 51 additions and 5 deletions
|
|
@ -4,6 +4,7 @@ class Comment < ApplicationRecord
|
||||||
|
|
||||||
include PgSearch::Model
|
include PgSearch::Model
|
||||||
include Reactable
|
include Reactable
|
||||||
|
include AlgoliaSearchable
|
||||||
|
|
||||||
BODY_MARKDOWN_SIZE_RANGE = (1..25_000)
|
BODY_MARKDOWN_SIZE_RANGE = (1..25_000)
|
||||||
|
|
||||||
|
|
|
||||||
34
app/models/concerns/algolia_searchable/searchable_comment.rb
Normal file
34
app/models/concerns/algolia_searchable/searchable_comment.rb
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
module AlgoliaSearchable
|
||||||
|
module SearchableComment
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
include AlgoliaSearch
|
||||||
|
|
||||||
|
algoliasearch(**DEFAULT_ALGOLIA_SETTINGS, unless: :bad_comment?) do
|
||||||
|
attribute :commentable_id, :commentable_type, :path, :parent_id
|
||||||
|
attribute :body do
|
||||||
|
title
|
||||||
|
end
|
||||||
|
|
||||||
|
attribute :published_at do
|
||||||
|
readable_publish_date
|
||||||
|
end
|
||||||
|
|
||||||
|
attribute :user do
|
||||||
|
{ name: user.name, username: user.username, profile_image: user.profile_image_90 }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class_methods do
|
||||||
|
def trigger_sidekiq_worker(record, delete)
|
||||||
|
AlgoliaSearch::SearchIndexWorker.perform_async(record.class.name, record.id, delete)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def bad_comment?
|
||||||
|
score.negative?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -612,4 +612,13 @@ RSpec.describe Comment do
|
||||||
expect(comment.by_staff_account?).to be(false)
|
expect(comment.by_staff_account?).to be(false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when indexing with Algolia", :algolia do
|
||||||
|
it "indexes on create" do
|
||||||
|
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
|
||||||
|
create(:comment)
|
||||||
|
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("Comment", kind_of(Integer),
|
||||||
|
false).once
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -445,7 +445,7 @@ RSpec.describe Organization do
|
||||||
search_index_worker = AlgoliaSearch::SearchIndexWorker
|
search_index_worker = AlgoliaSearch::SearchIndexWorker
|
||||||
allow(search_index_worker).to receive(:perform_async)
|
allow(search_index_worker).to receive(:perform_async)
|
||||||
create(:organization)
|
create(:organization)
|
||||||
expect(search_index_worker).to have_received(:perform_async).with("Organization", kind_of(Integer), false)
|
expect(search_index_worker).to have_received(:perform_async).with("Organization", kind_of(Integer), false).once
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -137,7 +137,7 @@ RSpec.describe PodcastEpisode do
|
||||||
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
|
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
|
||||||
create(:podcast_episode)
|
create(:podcast_episode)
|
||||||
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("PodcastEpisode",
|
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("PodcastEpisode",
|
||||||
kind_of(Integer), false)
|
kind_of(Integer), false).once
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -294,7 +294,8 @@ RSpec.describe Tag do
|
||||||
it "indexes on create" do
|
it "indexes on create" do
|
||||||
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
|
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
|
||||||
create(:tag)
|
create(:tag)
|
||||||
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("Tag", kind_of(Integer), false)
|
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("Tag", kind_of(Integer),
|
||||||
|
false).once
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1027,14 +1027,15 @@ RSpec.describe User do
|
||||||
it "indexes the user on create" do
|
it "indexes the user on create" do
|
||||||
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
|
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
|
||||||
create(:user)
|
create(:user)
|
||||||
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("User", kind_of(Integer), false)
|
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("User", kind_of(Integer),
|
||||||
|
false).once
|
||||||
end
|
end
|
||||||
|
|
||||||
it "updates user index if user's name has changed" do
|
it "updates user index if user's name has changed" do
|
||||||
user = create(:user)
|
user = create(:user)
|
||||||
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
|
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
|
||||||
user.update(name: "New Name")
|
user.update(name: "New Name")
|
||||||
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("User", user.id, false)
|
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("User", user.id, false).once
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#bad_actor?" do
|
describe "#bad_actor?" do
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue