Commit b023f23c1db461adec7637b3062ef1b7c96a6052

Authored by Steven Eksteen
1 parent b60462c9

Nobrainer, ORM for RethinkDB, support

.travis.yml
... ... @@ -6,6 +6,12 @@ services:
6 6 - mongodb
7 7 before_install:
8 8 - wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.3.2.deb && sudo dpkg -i elasticsearch-1.3.2.deb && sudo service elasticsearch restart
  9 + - source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
  10 + - wget -qO- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -
  11 + - sudo apt-get update -q
  12 + - sudo apt-get install rethinkdb
  13 + - sudo cp /etc/rethinkdb/default.conf.sample /etc/rethinkdb/instances.d/instance1.conf
  14 + - sudo service rethinkdb restart
9 15 script: bundle exec rake test
10 16 before_script:
11 17 - psql -c 'create database searchkick_test;' -U postgres
... ... @@ -21,3 +27,4 @@ gemfile:
21 27 - gemfiles/mongoid2.gemfile
22 28 - gemfiles/mongoid3.gemfile
23 29 - gemfiles/mongoid4.gemfile
  30 + - gemfiles/nobrainer.gemfile
... ...
gemfiles/nobrainer.gemfile 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +source 'https://rubygems.org'
  2 +
  3 +# Specify your gem's dependencies in searchkick.gemspec
  4 +gemspec path: "../"
  5 +
  6 +gem "nobrainer"
... ...
lib/searchkick/results.rb
... ... @@ -24,7 +24,11 @@ module Searchkick
24 24 hits.group_by{|hit, i| hit["_type"] }.each do |type, grouped_hits|
25 25 records = type.camelize.constantize
26 26 if options[:includes]
27   - records = records.includes(options[:includes])
  27 + if records.respond_to?(:preload)
  28 + records = records.preload(options[:includes])
  29 + else
  30 + records = records.includes(options[:includes])
  31 + end
28 32 end
29 33 results[type] = results_query(records, grouped_hits)
30 34 end
... ... @@ -143,6 +147,9 @@ module Searchkick
143 147 elsif records.respond_to?(:queryable)
144 148 # Mongoid 3+
145 149 records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
  150 + elsif records.respond_to?(:unscoped) and records.all.respond_to?(:preload)
  151 + # Nobrainer
  152 + records.unscoped.where(:id.in => grouped_hits.map{|hit| hit["_id"] }).to_a
146 153 else
147 154 raise "Not sure how to load records"
148 155 end
... ...
test/sql_test.rb
... ... @@ -312,7 +312,7 @@ class TestSql < Minitest::Test
312 312 end
313 313  
314 314 # TODO see if Mongoid is loaded
315   - if !defined?(Mongoid)
  315 + unless defined?(Mongoid) or defined?(NoBrainer)
316 316 def test_include
317 317 store_names ["Product A"]
318 318 assert Product.search("product", include: [:store]).first.association(:store).loaded?
... ...
test/test_helper.rb
... ... @@ -73,6 +73,55 @@ if defined?(Mongoid)
73 73  
74 74 class Cat < Animal
75 75 end
  76 +elsif defined?(NoBrainer)
  77 + #
  78 + # Aliasing create bang.
  79 + #
  80 + module NoBrainer::Document::Persistance
  81 + extend ActiveSupport::Concern
  82 +
  83 + module ClassMethods
  84 + alias :create! :create
  85 + end
  86 + end
  87 +
  88 + NoBrainer.configure do |config|
  89 + config.rethinkdb_url = 'rethinkdb://localhost/searchkick_test'
  90 + end
  91 +
  92 + class Product
  93 + include NoBrainer::Document
  94 + include NoBrainer::Document::Timestamps
  95 +
  96 + field :name, type: String
  97 + field :store_id, type: Integer
  98 + field :in_stock, type: Boolean
  99 + field :backordered, type: Boolean
  100 + field :orders_count, type: Integer
  101 + field :price, type: Integer
  102 + field :color, type: String
  103 + field :latitude
  104 + field :longitude
  105 + field :description, type: String
  106 + end
  107 +
  108 + class Store
  109 + include NoBrainer::Document
  110 +
  111 + field :name, type: String
  112 + end
  113 +
  114 + class Animal
  115 + include NoBrainer::Document
  116 +
  117 + field :name, type: String
  118 + end
  119 +
  120 + class Dog < Animal
  121 + end
  122 +
  123 + class Cat < Animal
  124 + end
76 125 else
77 126 require "active_record"
78 127  
... ...