Commit 2055b70997e4306569af32fffe2b81e8b625a76c

Authored by Andrew Kane
1 parent 5d050c74

Added model and model_includes

Showing 2 changed files with 31 additions and 1 deletions   Show diff stats
lib/searchkick/relation.rb
@@ -136,6 +136,27 @@ module Searchkick @@ -136,6 +136,27 @@ module Searchkick
136 self 136 self
137 end 137 end
138 138
  139 + def models(*args)
  140 + spawn.models!(*args)
  141 + end
  142 +
  143 + def models!(*args)
  144 + options[:models] = Array(options[:models]) + args
  145 + self
  146 + end
  147 +
  148 + def model_includes(value)
  149 + spawn.model_includes!(value)
  150 + end
  151 +
  152 + def model_includes!(value)
  153 + options[:model_includes] ||= {}
  154 + value.each do |k, v|
  155 + options[:model_includes][k] = Array(options[:model_includes][k]) + v
  156 + end
  157 + self
  158 + end
  159 +
139 # same as Active Record 160 # same as Active Record
140 def inspect 161 def inspect
141 entries = results.first(11).map!(&:inspect) 162 entries = results.first(11).map!(&:inspect)
test/sql_test.rb
@@ -169,8 +169,10 @@ class SqlTest < Minitest::Test @@ -169,8 +169,10 @@ class SqlTest < Minitest::Test
169 169
170 def test_includes 170 def test_includes
171 skip unless defined?(ActiveRecord) 171 skip unless defined?(ActiveRecord)
  172 +
172 store_names ["Product A"] 173 store_names ["Product A"]
173 assert Product.search("product", includes: [:store]).first.association(:store).loaded? 174 assert Product.search("product", includes: [:store]).first.association(:store).loaded?
  175 + assert Product.search("product", relation: true).includes(:store).first.association(:store).loaded?
174 end 176 end
175 177
176 def test_model_includes 178 def test_model_includes
@@ -180,10 +182,17 @@ class SqlTest < Minitest::Test @@ -180,10 +182,17 @@ class SqlTest < Minitest::Test
180 store_names ["Store A"], Store 182 store_names ["Store A"], Store
181 183
182 associations = {Product => [:store], Store => [:products]} 184 associations = {Product => [:store], Store => [:products]}
183 - result = Searchkick.search("*", models: [Product, Store], model_includes: associations)  
184 185
  186 + result = Searchkick.search("*", models: [Product, Store], model_includes: associations)
185 assert_equal 2, result.length 187 assert_equal 2, result.length
  188 + result.group_by(&:class).each_pair do |klass, records|
  189 + assert records.first.association(associations[klass].first).loaded?
  190 + end
186 191
  192 + skip "Not working yet"
  193 +
  194 + result = Searchkick.search("*", relation: true).models(Product, Store).model_includes(associations)
  195 + assert_equal 2, result.length
187 result.group_by(&:class).each_pair do |klass, records| 196 result.group_by(&:class).each_pair do |klass, records|
188 assert records.first.association(associations[klass].first).loaded? 197 assert records.first.association(associations[klass].first).loaded?
189 end 198 end