Add AlgoliaSearchable to Article (#20892)
* Add AlgoliaSearchable to Article * Remove comments * Fix spec
This commit is contained in:
parent
13a07630ff
commit
d5ba407629
3 changed files with 46 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ class Article < ApplicationRecord
|
|||
include Taggable
|
||||
include UserSubscriptionSourceable
|
||||
include PgSearch::Model
|
||||
include AlgoliaSearchable
|
||||
|
||||
acts_as_taggable_on :tags
|
||||
resourcify
|
||||
|
|
|
|||
36
app/models/concerns/algolia_searchable/searchable_article.rb
Normal file
36
app/models/concerns/algolia_searchable/searchable_article.rb
Normal 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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue