2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe HasSearchData do
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "belongs to its model" do
|
2017-08-15 11:46:57 -04:00
|
|
|
before do
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec("create temporary table model_items(id SERIAL primary key)")
|
|
|
|
DB.exec("create temporary table model_item_search_data(model_item_id int primary key, search_data tsvector, raw_data text, locale text)")
|
2017-08-15 11:46:57 -04:00
|
|
|
|
|
|
|
class ModelItem < ActiveRecord::Base
|
|
|
|
has_one :model_item_search_data, dependent: :destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
class ModelItemSearchData < ActiveRecord::Base
|
|
|
|
include HasSearchData
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec("drop table model_items")
|
|
|
|
DB.exec("drop table model_item_search_data")
|
2017-08-15 11:46:57 -04:00
|
|
|
|
2019-04-30 02:58:18 -04:00
|
|
|
# this weakref in the descendant tracker should clean up the two tests
|
|
|
|
# if this becomes an issue we can revisit (watch out for erratic tests)
|
|
|
|
Object.send(:remove_const, :ModelItem)
|
|
|
|
Object.send(:remove_const, :ModelItemSearchData)
|
2017-08-15 11:46:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:item) do
|
|
|
|
item = ModelItem.create!
|
|
|
|
item.create_model_item_search_data!(
|
|
|
|
model_item_id: item.id,
|
|
|
|
search_data: 'a',
|
|
|
|
raw_data: 'a',
|
|
|
|
locale: 'en')
|
|
|
|
item
|
|
|
|
end
|
|
|
|
|
2018-06-26 12:32:13 -04:00
|
|
|
it 'sets its primary key into associated model' do
|
2017-08-15 11:46:57 -04:00
|
|
|
expect(ModelItemSearchData.primary_key).to eq 'model_item_id'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can access the model' do
|
|
|
|
record_id = item.id
|
|
|
|
expect(ModelItemSearchData.find_by(model_item_id: record_id).model_item_id).to eq record_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|