Files
fizzy/app/models/concerns/searchable.rb
T
Donal McBreen b571303385 Don't use Current.account for search records
It doesn't actually work, and even if we could make it work reliably
we are better off if the records always know to go to the right shard.

It does make the interface a bit more complicated as we need to select
the right shard class with `for(account_id)`.
2025-11-25 11:34:41 +00:00

51 lines
1.3 KiB
Ruby

module Searchable
extend ActiveSupport::Concern
included do
after_create_commit :create_in_search_index
after_update_commit :update_in_search_index
after_destroy_commit :remove_from_search_index
end
def reindex
update_in_search_index
end
private
def create_in_search_index
search_record_class.create!(search_record_attributes)
end
def update_in_search_index
search_record_class.upsert!(search_record_attributes)
end
def remove_from_search_index
search_record_class.find_by(searchable_type: self.class.name, searchable_id: id)&.destroy
end
def search_record_attributes
{
account_id: account_id,
searchable_type: self.class.name,
searchable_id: id,
card_id: search_card_id,
board_id: search_board_id,
title: search_title,
content: search_content,
created_at: created_at
}
end
def search_record_class
Search::Record.for(account_id)
end
# Models must implement these methods:
# - account_id: returns the account id
# - search_title: returns title string or nil
# - search_content: returns content string
# - search_card_id: returns the card id (self.id for cards, card_id for comments)
# - search_board_id: returns the board id
end