From 29fb877407db794e78d0cdb94a310f212662aa5c Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 17 Mar 2020 17:49:16 -0700 Subject: [PATCH] Added relation class --- lib/searchkick.rb | 15 ++++++++++----- lib/searchkick/relation.rb | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/relation_test.rb | 13 +++++++++++++ 3 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 lib/searchkick/relation.rb create mode 100644 test/relation_test.rb diff --git a/lib/searchkick.rb b/lib/searchkick.rb index dfce437..0e0a3f8 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -14,6 +14,7 @@ require "searchkick/query" require "searchkick/reindex_queue" require "searchkick/record_data" require "searchkick/record_indexer" +require "searchkick/relation" require "searchkick/results" require "searchkick/version" @@ -86,7 +87,7 @@ module Searchkick @server_below7 end - def self.search(term = "*", model: nil, **options, &block) + def self.search(term = "*", model: nil, relation: false, **options, &block) options = options.dup klass = model @@ -112,11 +113,15 @@ module Searchkick end options = options.merge(block: block) if block - query = Searchkick::Query.new(klass, term, **options) - if options[:execute] == false - query + if relation + Searchkick::Relation.new(klass, term, **options) else - query.execute + query = Searchkick::Query.new(klass, term, **options) + if options[:execute] == false + query + else + query.execute + end end end diff --git a/lib/searchkick/relation.rb b/lib/searchkick/relation.rb new file mode 100644 index 0000000..8723a86 --- /dev/null +++ b/lib/searchkick/relation.rb @@ -0,0 +1,66 @@ +module Searchkick + class Relation + extend Forwardable + + attr_reader :klass, :term, :options + + def_delegators :execute, :map, :each, :any?, :empty?, :size, :length, :slice, :[], :to_ary, + :records, :results, :suggestions, :each_with_hit, :with_details, :aggregations, :aggs, + :took, :error, :model_name, :entry_name, :total_count, :total_entries, + :current_page, :per_page, :limit_value, :padding, :total_pages, :num_pages, + :offset_value, :offset, :previous_page, :prev_page, :next_page, :first_page?, :last_page?, + :out_of_range?, :hits, :response, :to_a, :first, :scroll + + def initialize(klass, term = "*", **options) + unknown_keywords = options.keys - [:aggs, :block, :body, :body_options, :boost, + :boost_by, :boost_by_distance, :boost_by_recency, :boost_where, :conversions, :conversions_term, :debug, :emoji, :exclude, :execute, :explain, + :fields, :highlight, :includes, :index_name, :indices_boost, :limit, :load, + :match, :misspellings, :models, :model_includes, :offset, :operator, :order, :padding, :page, :per_page, :profile, + :request_params, :routing, :scope_results, :scroll, :select, :similar, :smart_aggs, :suggest, :total_entries, :track, :type, :where] + raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? + + @klass = klass + @term = term + @options = options + end + + def where(opts) + spawn.where!(opts) + end + + def where!(opts) + if options[:where] + options[:where] = [{_and: [options[:where], opts]}] + else + options[:where] = opts + end + self + end + + def limit(value) + spawn.limit!(value) + end + + def limit!(value) + options[:limit] = value + self + end + + # same as Active Record + def inspect + entries = results.first(11).map!(&:inspect) + entries[10] = "..." if entries.size == 11 + "#<#{self.class.name} [#{entries.join(', ')}]>" + end + + private + + def execute + Query.new(klass, term, options).execute + end + + def spawn + Relation.new(klass, term, options.deep_dup) + end + end +end diff --git a/test/relation_test.rb b/test/relation_test.rb new file mode 100644 index 0000000..31e019f --- /dev/null +++ b/test/relation_test.rb @@ -0,0 +1,13 @@ +require_relative "test_helper" + +class RelationTest < Minitest::Test + def test_works + store_names ["Product A", "Product B"] + p Product.search("product", relation: true).where(name: "Product A").limit(1) + end + + def test_no_term + store_names ["Product A"] + p Product.search(relation: true) + end +end -- libgit2 0.21.0