diff --git a/app/models/concerns/algolia_searchable/searchable_tag.rb b/app/models/concerns/algolia_searchable/searchable_tag.rb new file mode 100644 index 000000000..851fab3b1 --- /dev/null +++ b/app/models/concerns/algolia_searchable/searchable_tag.rb @@ -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 diff --git a/app/models/tag.rb b/app/models/tag.rb index df027f7a1..ac52e85d5 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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 diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 560800415..37b5a9e8c 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -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