Commit 5d050c7480959059385c17000362f00b4fe2decc

Authored by Andrew Kane
1 parent 1f3c523d

Added load

Showing 2 changed files with 22 additions and 0 deletions   Show diff stats
lib/searchkick/relation.rb
@@ -118,6 +118,24 @@ module Searchkick @@ -118,6 +118,24 @@ module Searchkick
118 self 118 self
119 end 119 end
120 120
  121 + def load(value)
  122 + spawn.load!(value)
  123 + end
  124 +
  125 + def load!(value)
  126 + options[:load] = value
  127 + self
  128 + end
  129 +
  130 + def includes(*args)
  131 + spawn.includes!(*args)
  132 + end
  133 +
  134 + def includes!(*args)
  135 + options[:includes] = Array(options[:includes]) + args
  136 + self
  137 + end
  138 +
121 # same as Active Record 139 # same as Active Record
122 def inspect 140 def inspect
123 entries = results.first(11).map!(&:inspect) 141 entries = results.first(11).map!(&:inspect)
test/sql_test.rb
@@ -70,22 +70,26 @@ class SqlTest < Minitest::Test @@ -70,22 +70,26 @@ class SqlTest < Minitest::Test
70 def test_load_false 70 def test_load_false
71 store_names ["Product A"] 71 store_names ["Product A"]
72 assert_kind_of Hash, Product.search("product", load: false).first 72 assert_kind_of Hash, Product.search("product", load: false).first
  73 + assert_kind_of Hash, Product.search("product", relation: true).load(false).first
73 end 74 end
74 75
75 def test_load_false_methods 76 def test_load_false_methods
76 store_names ["Product A"] 77 store_names ["Product A"]
77 assert_equal "Product A", Product.search("product", load: false).first.name 78 assert_equal "Product A", Product.search("product", load: false).first.name
  79 + assert_equal "Product A", Product.search("product", relation: true).load(false).first.name
78 end 80 end
79 81
80 def test_load_false_with_includes 82 def test_load_false_with_includes
81 store_names ["Product A"] 83 store_names ["Product A"]
82 assert_kind_of Hash, Product.search("product", load: false, includes: [:store]).first 84 assert_kind_of Hash, Product.search("product", load: false, includes: [:store]).first
  85 + assert_kind_of Hash, Product.search("product", relation: true).load(false).includes(:store).first
83 end 86 end
84 87
85 def test_load_false_nested_object 88 def test_load_false_nested_object
86 aisle = {"id" => 1, "name" => "Frozen"} 89 aisle = {"id" => 1, "name" => "Frozen"}
87 store [{name: "Product A", aisle: aisle}] 90 store [{name: "Product A", aisle: aisle}]
88 assert_equal aisle, Product.search("product", load: false).first.aisle.to_hash 91 assert_equal aisle, Product.search("product", load: false).first.aisle.to_hash
  92 + assert_equal aisle, Product.search("product", relation: true).load(false).first.aisle.to_hash
89 end 93 end
90 94
91 # select 95 # select