Add AlgoliaSearchable to Tag (#20888)

* Add AlgoliaSearch::SearchableTag

* Refactor

* Update app/models/concerns/algolia_searchable/searchable_tag.rb
This commit is contained in:
Mac Siri 2024-04-24 10:41:58 -04:00 committed by GitHub
parent b17b323562
commit 755939733d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 1 deletions

View file

@ -0,0 +1,21 @@
module AlgoliaSearchable
module SearchableTag
extend ActiveSupport::Concern
included do
include AlgoliaSearch
algoliasearch(**DEFAULT_ALGOLIA_SETTINGS) do
attribute :name, :pretty_name, :short_summary, :hotness_score
customRanking ["desc(hotness_score)"]
end
end
class_methods do
def trigger_sidekiq_worker(record, delete)
AlgoliaSearch::SearchIndexWorker.perform_async(record.class.name, record.id, delete)
end
end
end
end

View file

@ -21,6 +21,7 @@ class Tag < ActsAsTaggableOn::Tag
# This model doesn't inherit from ApplicationRecord so this has to be included
include Purgeable
include PgSearch::Model
include AlgoliaSearchable
# @note Even though we have a data migration script (see further
# comments below), as of <2022-01-04 Tue> we had 5 tags where
@ -237,7 +238,7 @@ class Tag < ActsAsTaggableOn::Tag
# @see Tag#explicit_points
# @see Tag#implicit_points
def points
(attributes["points"] || @points || 0)
attributes["points"] || @points || 0
end
# @!attribute [rw] explicit_points

View file

@ -289,4 +289,12 @@ RSpec.describe Tag do
end
end
end
context "when indexing with Algolia", :algolia do
it "indexes on create" do
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
create(:tag)
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("Tag", kind_of(Integer), false)
end
end
end