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 118 self
119 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 139 # same as Active Record
122 140 def inspect
123 141 entries = results.first(11).map!(&:inspect)
... ...
test/sql_test.rb
... ... @@ -70,22 +70,26 @@ class SqlTest < Minitest::Test
70 70 def test_load_false
71 71 store_names ["Product A"]
72 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 74 end
74 75  
75 76 def test_load_false_methods
76 77 store_names ["Product A"]
77 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 80 end
79 81  
80 82 def test_load_false_with_includes
81 83 store_names ["Product A"]
82 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 86 end
84 87  
85 88 def test_load_false_nested_object
86 89 aisle = {"id" => 1, "name" => "Frozen"}
87 90 store [{name: "Product A", aisle: aisle}]
88 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 93 end
90 94  
91 95 # select
... ...