Add AlgoliaSearchable to Article (#20892)

* Add AlgoliaSearchable to Article

* Remove comments

* Fix spec
This commit is contained in:
Mac Siri 2024-04-26 08:59:13 -04:00 committed by GitHub
parent 13a07630ff
commit d5ba407629
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 0 deletions

View file

@ -5,6 +5,7 @@ class Article < ApplicationRecord
include Taggable
include UserSubscriptionSourceable
include PgSearch::Model
include AlgoliaSearchable
acts_as_taggable_on :tags
resourcify

View file

@ -0,0 +1,36 @@
module AlgoliaSearchable
module SearchableArticle
extend ActiveSupport::Concern
included do
include AlgoliaSearch
algoliasearch(**DEFAULT_ALGOLIA_SETTINGS, if: :indexable) do
attribute :user do
{ name: user.name, username: user.username, profile_image: user.profile_image_90 }
end
attribute :title, :tag_list, :reading_time, :score, :featured, :featured_number, :comments_count,
:reaction_counts, :positive_reaction_counts, :path, :main_image
attribute :published_at do
published_at.to_i
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 indexable
published && score.positive?
end
def indexable_changed?
published_changed? || score_changed?
end
end
end

View file

@ -1672,4 +1672,13 @@ RSpec.describe Article do
expect(article.skip_indexing_reason).to eq("unknown")
end
end
context "when indexing with Algolia", :algolia do
it "indexes the article" do
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
create(:article)
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("Article", kind_of(Integer),
false).once
end
end
end