Commit aaf41f0e4b0e100aae6993b1c28076d615d8f164
1 parent
006fb543
Exists in
master
and in
19 other branches
Moved more logic out of model [skip ci]
Showing
3 changed files
with
65 additions
and
49 deletions
Show diff stats
lib/searchkick.rb
... | ... | @@ -13,6 +13,7 @@ require "searchkick/multi_search" |
13 | 13 | require "searchkick/query" |
14 | 14 | require "searchkick/reindex_queue" |
15 | 15 | require "searchkick/record_data" |
16 | +require "searchkick/record_indexer" | |
16 | 17 | require "searchkick/results" |
17 | 18 | require "searchkick/version" |
18 | 19 | ... | ... |
lib/searchkick/model.rb
... | ... | @@ -14,6 +14,11 @@ module Searchkick |
14 | 14 | |
15 | 15 | options[:_type] ||= -> { searchkick_index.klass_document_type(self, true) } |
16 | 16 | |
17 | + callbacks = options.key?(:callbacks) ? options[:callbacks] : true | |
18 | + unless [true, false, :async, :queue].include?(callbacks) | |
19 | + raise ArgumentError, "Invalid value for callbacks" | |
20 | + end | |
21 | + | |
17 | 22 | class_eval do |
18 | 23 | cattr_reader :searchkick_options, :searchkick_klass |
19 | 24 | |
... | ... | @@ -49,11 +54,6 @@ module Searchkick |
49 | 54 | end |
50 | 55 | end |
51 | 56 | |
52 | - callbacks = options.key?(:callbacks) ? options[:callbacks] : true | |
53 | - unless [true, false, :async, :queue].include?(callbacks) | |
54 | - raise ArgumentError, "Invalid value for callbacks" | |
55 | - end | |
56 | - | |
57 | 57 | if callbacks |
58 | 58 | if respond_to?(:after_commit) |
59 | 59 | after_commit :reindex |
... | ... | @@ -63,50 +63,8 @@ module Searchkick |
63 | 63 | end |
64 | 64 | end |
65 | 65 | |
66 | - def reindex(method_name = nil, refresh: false, mode: nil) | |
67 | - return unless Searchkick.callbacks? | |
68 | - | |
69 | - unless [true, nil, :async, :queue].include?(mode) | |
70 | - raise ArgumentError, "Invalid value for mode" | |
71 | - end | |
72 | - | |
73 | - klass_options = self.class.searchkick_index.options | |
74 | - | |
75 | - if mode.nil? | |
76 | - mode = | |
77 | - if Searchkick.callbacks_value | |
78 | - Searchkick.callbacks_value | |
79 | - else | |
80 | - klass_options[:callbacks] | |
81 | - end | |
82 | - end | |
83 | - | |
84 | - case mode | |
85 | - when :queue | |
86 | - if method_name | |
87 | - raise Searchkick::Error, "Partial reindex not supported with queue option" | |
88 | - else | |
89 | - self.class.searchkick_index.reindex_queue.push(id.to_s) | |
90 | - end | |
91 | - when :async | |
92 | - if method_name | |
93 | - # TODO support Mongoid and NoBrainer and non-id primary keys | |
94 | - Searchkick::BulkReindexJob.perform_later( | |
95 | - class_name: self.class.name, | |
96 | - record_ids: [id.to_s], | |
97 | - method_name: method_name ? method_name.to_s : nil | |
98 | - ) | |
99 | - else | |
100 | - self.class.searchkick_index.reindex_record_async(self) | |
101 | - end | |
102 | - else | |
103 | - if method_name | |
104 | - self.class.searchkick_index.update_record(self, method_name) | |
105 | - else | |
106 | - self.class.searchkick_index.reindex_record(self) | |
107 | - end | |
108 | - self.class.searchkick_index.refresh if refresh | |
109 | - end | |
66 | + def reindex(method_name = nil, **options) | |
67 | + RecordIndexer.new(self).reindex(method_name, **options) | |
110 | 68 | end unless method_defined?(:reindex) |
111 | 69 | |
112 | 70 | def similar(options = {}) | ... | ... |
... | ... | @@ -0,0 +1,57 @@ |
1 | +module Searchkick | |
2 | + class RecordIndexer | |
3 | + attr_reader :record | |
4 | + | |
5 | + def initialize(record) | |
6 | + @record = record | |
7 | + end | |
8 | + | |
9 | + def reindex(method_name = nil, refresh: false, mode: nil) | |
10 | + return unless Searchkick.callbacks? | |
11 | + | |
12 | + unless [true, nil, :async, :queue].include?(mode) | |
13 | + raise ArgumentError, "Invalid value for mode" | |
14 | + end | |
15 | + | |
16 | + index = record.class.searchkick_index | |
17 | + | |
18 | + klass_options = index.options | |
19 | + | |
20 | + if mode.nil? | |
21 | + mode = | |
22 | + if Searchkick.callbacks_value | |
23 | + Searchkick.callbacks_value | |
24 | + else | |
25 | + klass_options[:callbacks] | |
26 | + end | |
27 | + end | |
28 | + | |
29 | + case mode | |
30 | + when :queue | |
31 | + if method_name | |
32 | + raise Searchkick::Error, "Partial reindex not supported with queue option" | |
33 | + else | |
34 | + index.reindex_queue.push(record.id.to_s) | |
35 | + end | |
36 | + when :async | |
37 | + if method_name | |
38 | + # TODO support Mongoid and NoBrainer and non-id primary keys | |
39 | + Searchkick::BulkReindexJob.perform_later( | |
40 | + class_name: record.class.name, | |
41 | + record_ids: [record.id.to_s], | |
42 | + method_name: method_name ? method_name.to_s : nil | |
43 | + ) | |
44 | + else | |
45 | + index.reindex_record_async(record) | |
46 | + end | |
47 | + else | |
48 | + if method_name | |
49 | + index.update_record(record, method_name) | |
50 | + else | |
51 | + index.reindex_record(record) | |
52 | + end | |
53 | + index.refresh if refresh | |
54 | + end | |
55 | + end | |
56 | + end | |
57 | +end | ... | ... |