diff --git a/lib/search_entries.rb b/lib/search_entries.rb index 5281dbf3..65fc4d1c 100644 --- a/lib/search_entries.rb +++ b/lib/search_entries.rb @@ -180,6 +180,11 @@ module SearchEntries end def index_template(template) + # Skip search indexing for partnership templates since they don't belong to accounts + # We currently don't utilize search, so this can be implemented later for partnerships + # if that changes. + return if template.account_id.blank? + sql = SearchEntry.sanitize_sql_array( ["SELECT to_tsvector(:text), to_tsvector('simple', :text)", { text: TextUtils.transliterate(template.name.to_s.downcase).delete("\0") }] diff --git a/spec/lib/search_entries_spec.rb b/spec/lib/search_entries_spec.rb new file mode 100644 index 00000000..21b27ea0 --- /dev/null +++ b/spec/lib/search_entries_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe SearchEntries do + describe '.index_template' do + context 'with partnership template' do + let(:partnership) { create(:partnership) } + let(:template) do + create(:template, :partnership_template, partnership: partnership, name: 'Partnership Template') + end + + it 'skips search indexing for partnership templates' do + result = described_class.index_template(template) + + expect(result).to be_nil + expect(template.reload.search_entry).to be_nil + end + + it 'does not raise error when account_id is blank' do + expect { described_class.index_template(template) }.not_to raise_error + end + + it 'logs the reason for skipping partnership templates' do + # Verify the early return works as expected + expect(template.account_id).to be_nil + expect(template.partnership_id).to be_present + + result = described_class.index_template(template) + expect(result).to be_nil + end + end + + context 'with account template' do + let(:account) { create(:account) } + let(:user) { create(:user, account: account) } + let(:template) { create(:template, account: account, author: user, name: 'Test Template') } + + it 'processes account templates normally' do + expect(template.account_id).to be_present + expect(template.partnership_id).to be_nil + + expect { described_class.index_template(template) }.not_to raise_error(ArgumentError, /account_id.blank?/) + end + end + end +end