Commit 90940105cb813fb223814215fadcbd553b74450d

Authored by Andrew Kane
1 parent 97e647a8

Prefer model over klass [skip ci]

lib/searchkick/relation.rb
... ... @@ -6,8 +6,8 @@ module Searchkick
6 6 delegate :body, :params, to: :@query
7 7 delegate_missing_to :private_execute
8 8  
9   - def initialize(klass, term = "*", **options)
10   - @query = Query.new(klass, term, **options)
  9 + def initialize(model, term = "*", **options)
  10 + @query = Query.new(model, term, **options)
11 11 end
12 12  
13 13 # same as Active Record
... ...
test/query_test.rb
... ... @@ -75,8 +75,8 @@ class QueryTest < Minitest::Test
75 75  
76 76 assert_equal 2, result.length
77 77  
78   - result.group_by(&:class).each_pair do |klass, records|
79   - assert records.first.association(associations[klass].first).loaded?
  78 + result.group_by(&:class).each_pair do |model, records|
  79 + assert records.first.association(associations[model].first).loaded?
80 80 end
81 81 end
82 82  
... ...
test/results_test.rb
... ... @@ -45,13 +45,13 @@ class ResultsTest < Minitest::Test
45 45 assert_equal 2, count
46 46 end
47 47  
48   - def test_model_name_with_klass
  48 + def test_model_name_with_model
49 49 store_names ["Product A", "Product B"]
50 50 results = Product.search("product")
51 51 assert_equal "Product", results.model_name.human
52 52 end
53 53  
54   - def test_model_name_without_klass
  54 + def test_model_name_without_model
55 55 store_names ["Product A", "Product B"]
56 56 results = Searchkick.search("product")
57 57 assert_equal "Result", results.model_name.human
... ...
test/support/helpers.rb
... ... @@ -10,48 +10,48 @@ class Minitest::Test
10 10  
11 11 protected
12 12  
13   - def store(documents, klass = default_model, reindex: true)
  13 + def store(documents, model = default_model, reindex: true)
14 14 if reindex
15 15 documents.shuffle.each do |document|
16   - klass.create!(document)
  16 + model.create!(document)
17 17 end
18   - klass.searchkick_index.refresh
  18 + model.searchkick_index.refresh
19 19 else
20 20 Searchkick.callbacks(false) do
21 21 documents.shuffle.each do |document|
22   - klass.create!(document)
  22 + model.create!(document)
23 23 end
24 24 end
25 25 end
26 26 end
27 27  
28   - def store_names(names, klass = default_model, reindex: true)
29   - store names.map { |name| {name: name} }, klass, reindex: reindex
  28 + def store_names(names, model = default_model, reindex: true)
  29 + store names.map { |name| {name: name} }, model, reindex: reindex
30 30 end
31 31  
32 32 # no order
33   - def assert_search(term, expected, options = {}, klass = default_model)
34   - assert_equal expected.sort, klass.search(term, **options).map(&:name).sort
  33 + def assert_search(term, expected, options = {}, model = default_model)
  34 + assert_equal expected.sort, model.search(term, **options).map(&:name).sort
35 35 end
36 36  
37   - def assert_order(term, expected, options = {}, klass = default_model)
38   - assert_equal expected, klass.search(term, **options).map(&:name)
  37 + def assert_order(term, expected, options = {}, model = default_model)
  38 + assert_equal expected, model.search(term, **options).map(&:name)
39 39 end
40 40  
41   - def assert_equal_scores(term, options = {}, klass = default_model)
42   - assert_equal 1, klass.search(term, **options).hits.map { |a| a["_score"] }.uniq.size
  41 + def assert_equal_scores(term, options = {}, model = default_model)
  42 + assert_equal 1, model.search(term, **options).hits.map { |a| a["_score"] }.uniq.size
43 43 end
44 44  
45   - def assert_first(term, expected, options = {}, klass = default_model)
46   - assert_equal expected, klass.search(term, **options).map(&:name).first
  45 + def assert_first(term, expected, options = {}, model = default_model)
  46 + assert_equal expected, model.search(term, **options).map(&:name).first
47 47 end
48 48  
49   - def assert_misspellings(term, expected, misspellings = {}, klass = default_model)
  49 + def assert_misspellings(term, expected, misspellings = {}, model = default_model)
50 50 options = {
51 51 fields: [:name, :color],
52 52 misspellings: misspellings
53 53 }
54   - assert_search(term, expected, options, klass)
  54 + assert_search(term, expected, options, model)
55 55 end
56 56  
57 57 def assert_warns(message)
... ... @@ -61,15 +61,15 @@ class Minitest::Test
61 61 assert_match "[searchkick] WARNING: #{message}", stderr
62 62 end
63 63  
64   - def with_options(options, klass = default_model)
65   - previous_options = klass.searchkick_options.dup
  64 + def with_options(options, model = default_model)
  65 + previous_options = model.searchkick_options.dup
66 66 begin
67   - klass.searchkick_options.merge!(options)
68   - klass.reindex
  67 + model.searchkick_options.merge!(options)
  68 + model.reindex
69 69 yield
70 70 ensure
71   - klass.searchkick_options.clear
72   - klass.searchkick_options.merge!(previous_options)
  71 + model.searchkick_options.clear
  72 + model.searchkick_options.merge!(previous_options)
73 73 end
74 74 end
75 75  
... ...