Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/typesense-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -955,11 +955,11 @@ def typesense_settings_changed?(prev, current)
def typesense_full_const_get(name)
list = name.split("::")
list.shift if list.first.blank?
obj = Object.const_defined?(:RUBY_VERSION) && RUBY_VERSION.to_f < 1.9 ? Object : self
obj = Object
list.each do |x|
# This is required because const_get tries to look for constants in the
# ancestor chain, but we only want constants that are HERE
obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
obj = obj.const_defined?(x, false) ? obj.const_get(x, false) : obj.const_missing(x)
end
obj
end
Expand Down
40 changes: 40 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
t.string :short_name
t.integer :hex
end
create_table :collections do |t|
t.string :name
end
create_table :namespaced_models do |t|
t.string :name
t.integer :another_private_value
Expand Down Expand Up @@ -212,6 +215,14 @@ def will_save_change_to_short_name?
end
end

class Collection < ActiveRecord::Base
include Typesense

typesense auto_index: false, index_name: safe_index_name("Collection") do
attribute :name
end
end

class DisabledBoolean < ActiveRecord::Base
include Typesense

Expand Down Expand Up @@ -642,6 +653,35 @@ class SerializedObject < ActiveRecord::Base
end
end

describe "Collection" do
it "resolves the model class instead of Typesense::Collection" do
expect(Collection.typesense_options[:type]).to eq(Collection)
end

it "uses the ActiveRecord model when loading search hits" do
record = Collection.create!(name: "Archive")

allow(Collection).to receive(:typesense_raw_search).and_return(
{
"hits" => [
{
"document" => { "id" => record.id.to_s },
"highlights" => []
}
],
"found" => 1,
"page" => 1,
"request_params" => { "per_page" => 10 }
}
)

results = Collection.search("*", "name")

expect(results.length).to eq(1)
expect(results.first).to eq(record)
end
end

describe "UniqUsers" do
before(:all) do
UniqUser.clear_index!
Expand Down
Loading