Skip to content

Commit b574462

Browse files
authored
Merge pull request #21 from tharropoulos/collection-resolution
fix: resolve top-level model constants from Object
2 parents 0b689b2 + f8fbe02 commit b574462

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

lib/typesense-rails.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,11 +955,11 @@ def typesense_settings_changed?(prev, current)
955955
def typesense_full_const_get(name)
956956
list = name.split("::")
957957
list.shift if list.first.blank?
958-
obj = Object.const_defined?(:RUBY_VERSION) && RUBY_VERSION.to_f < 1.9 ? Object : self
958+
obj = Object
959959
list.each do |x|
960960
# This is required because const_get tries to look for constants in the
961961
# ancestor chain, but we only want constants that are HERE
962-
obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
962+
obj = obj.const_defined?(x, false) ? obj.const_get(x, false) : obj.const_missing(x)
963963
end
964964
obj
965965
end

spec/integration_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
t.string :short_name
7474
t.integer :hex
7575
end
76+
create_table :collections do |t|
77+
t.string :name
78+
end
7679
create_table :namespaced_models do |t|
7780
t.string :name
7881
t.integer :another_private_value
@@ -212,6 +215,14 @@ def will_save_change_to_short_name?
212215
end
213216
end
214217

218+
class Collection < ActiveRecord::Base
219+
include Typesense
220+
221+
typesense auto_index: false, index_name: safe_index_name("Collection") do
222+
attribute :name
223+
end
224+
end
225+
215226
class DisabledBoolean < ActiveRecord::Base
216227
include Typesense
217228

@@ -642,6 +653,35 @@ class SerializedObject < ActiveRecord::Base
642653
end
643654
end
644655

656+
describe "Collection" do
657+
it "resolves the model class instead of Typesense::Collection" do
658+
expect(Collection.typesense_options[:type]).to eq(Collection)
659+
end
660+
661+
it "uses the ActiveRecord model when loading search hits" do
662+
record = Collection.create!(name: "Archive")
663+
664+
allow(Collection).to receive(:typesense_raw_search).and_return(
665+
{
666+
"hits" => [
667+
{
668+
"document" => { "id" => record.id.to_s },
669+
"highlights" => []
670+
}
671+
],
672+
"found" => 1,
673+
"page" => 1,
674+
"request_params" => { "per_page" => 10 }
675+
}
676+
)
677+
678+
results = Collection.search("*", "name")
679+
680+
expect(results.length).to eq(1)
681+
expect(results.first).to eq(record)
682+
end
683+
end
684+
645685
describe "UniqUsers" do
646686
before(:all) do
647687
UniqUser.clear_index!

0 commit comments

Comments
 (0)