chewy 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +22 -0
- data/lib/chewy.rb +65 -21
- data/lib/chewy/config.rb +1 -5
- data/lib/chewy/index.rb +1 -1
- data/lib/chewy/query.rb +4 -4
- data/lib/chewy/type.rb +49 -14
- data/lib/chewy/type/actions.rb +1 -1
- data/lib/chewy/type/adapter/active_record.rb +2 -2
- data/lib/chewy/type/adapter/base.rb +1 -1
- data/lib/chewy/type/adapter/object.rb +2 -2
- data/lib/chewy/type/import.rb +1 -1
- data/lib/chewy/type/mapping.rb +1 -1
- data/lib/chewy/type/observe.rb +1 -1
- data/lib/chewy/type/wrapper.rb +2 -2
- data/lib/chewy/version.rb +1 -1
- data/spec/chewy/index_spec.rb +29 -1
- data/spec/chewy/query_spec.rb +6 -9
- data/spec/chewy/type/adapter/active_record_spec.rb +3 -0
- data/spec/chewy/type/adapter/object_spec.rb +2 -0
- data/spec/chewy/type/mapping_spec.rb +1 -1
- data/spec/chewy/type_spec.rb +0 -60
- data/spec/chewy_spec.rb +81 -0
- metadata +2 -3
- data/lib/chewy/type/base.rb +0 -43
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 434f4c2db17b4906fcb08963bd2e539869179ee6
|
4
|
+
data.tar.gz: 827aba062a02a1911666162159f48671a1ed3634
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1cf88693d5554070dc6bf5a6ce402e67c30afb4c2d9eb0018b2c8c61d774b1944099bf4d2d043c550743a973eb1fe83dba87e0f6443bec896b58f9a977cad6e6
|
7
|
+
data.tar.gz: 5846df79cf664b56123a77cbfe3c6b3b2d030e5a475919d1500bcbe1d72671c942e440cbf5e95cb6f4091d68f832fb46198e7257a745360daebf10867f1489e0
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# master
|
2
2
|
|
3
|
+
# Version 0.5.2
|
4
|
+
|
5
|
+
## Changes
|
6
|
+
|
7
|
+
* `Chewy.massacre` aliased to `Chewy.delete_all` method deletes all the indexes with current prefix
|
8
|
+
|
9
|
+
## Incompatible changes:
|
10
|
+
|
11
|
+
* `Chewy::Type::Base` removed in favour of using `Chewy::Type` as a base class for all types
|
12
|
+
|
13
|
+
## Bugfixes:
|
14
|
+
|
15
|
+
* Advanced type classes resolving (@inbeom)
|
16
|
+
|
17
|
+
* `import` ignores nil
|
18
|
+
|
3
19
|
# Version 0.5.1
|
4
20
|
|
5
21
|
## Changes:
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
[![Build Status](https://travis-ci.org/toptal/chewy.png)](https://travis-ci.org/toptal/chewy)
|
2
2
|
[![Code Climate](https://codeclimate.com/github/toptal/chewy.png)](https://codeclimate.com/github/toptal/chewy)
|
3
|
+
[![Inline docs](http://inch-ci.org/github/toptal/chewy.svg?branch=master)](http://inch-ci.org/github/toptal/chewy)
|
3
4
|
|
4
5
|
# Chewy
|
5
6
|
|
@@ -279,6 +280,10 @@ end
|
|
279
280
|
|
280
281
|
Also atomic blocks might be nested and don't affect each other.
|
281
282
|
|
283
|
+
### Async reindexing
|
284
|
+
|
285
|
+
Chewy is not support async index update, but it's planned. Until you can use third-party solutions, such as [https://github.com/averell23/chewy_kiqqer](https://github.com/averell23/chewy_kiqqer)
|
286
|
+
|
282
287
|
### Index querying
|
283
288
|
|
284
289
|
```ruby
|
@@ -635,6 +640,22 @@ When the response comes back, it will have the ```:facets``` sidechannel include
|
|
635
640
|
< { ... ,"facets":{"countries":{"_type":"terms","missing":?,"total":?,"other":?,"terms":[{"term":"USA","count":?},{"term":"Brazil","count":?}, ...}}
|
636
641
|
```
|
637
642
|
|
643
|
+
### Script scoring
|
644
|
+
|
645
|
+
Script scoring is used to score the search results. All scores are added to the search request and combined according to boost mode and score mode. This can be useful if, for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered set of documents. For example, you might want to multiply the score by another numeric field in the doc:
|
646
|
+
|
647
|
+
```ruby
|
648
|
+
UsersIndex.script_score("_score * doc['my_numeric_field'].value")
|
649
|
+
```
|
650
|
+
|
651
|
+
### Boost Factor
|
652
|
+
|
653
|
+
Boost factors are a way to add a boost to a query where documents match the filter. If you have some users who are experts and some are regular users, you might want to give the experts a higher score and boost to the top of the search results. You can accomplish this by using the #boost_factor method and adding a boost score for an expert user of 5:
|
654
|
+
|
655
|
+
```ruby
|
656
|
+
UsersIndex.boost_factor(5, filter: {term: {type: 'Expert'}})
|
657
|
+
```
|
658
|
+
|
638
659
|
### Objects loading
|
639
660
|
|
640
661
|
It is possible to load source objects from database for every search result:
|
@@ -745,6 +766,7 @@ See [update_index.rb](lib/chewy/rspec/update_index.rb) for more details.
|
|
745
766
|
* update_all support
|
746
767
|
* Other than ActiveRecord ORMs support (Mongoid)
|
747
768
|
* Maybe, closer ORM/ODM integration, creating index classes implicitly
|
769
|
+
* Asunc indexes updating
|
748
770
|
|
749
771
|
## Contributing
|
750
772
|
|
data/lib/chewy.rb
CHANGED
@@ -30,29 +30,73 @@ ActiveSupport.on_load(:active_record) do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
module Chewy
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
index
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
33
|
+
class << self
|
34
|
+
# Derives type from string `index#type` representation:
|
35
|
+
#
|
36
|
+
# Chewy.derive_type('users#user') # => UsersIndex::User
|
37
|
+
#
|
38
|
+
# If index has only one type - it is possible to derive it without specification:
|
39
|
+
#
|
40
|
+
# Chewy.derive_type('users') # => UsersIndex::User
|
41
|
+
#
|
42
|
+
# If index has more then one type - it raises Chewy::UnderivableType.
|
43
|
+
#
|
44
|
+
def derive_type name
|
45
|
+
return name if name.is_a?(Class) && name < Chewy::Type
|
46
|
+
|
47
|
+
index_name, type_name = name.split('#', 2)
|
48
|
+
class_name = "#{index_name.camelize}Index"
|
49
|
+
index = class_name.safe_constantize
|
50
|
+
raise Chewy::UnderivableType.new("Can not find index named `#{class_name}`") unless index && index < Chewy::Index
|
51
|
+
type = if type_name.present?
|
52
|
+
index.type_hash[type_name] or raise Chewy::UnderivableType.new("Index `#{class_name}` doesn`t have type named `#{type_name}`")
|
53
|
+
elsif index.types.one?
|
54
|
+
index.types.first
|
55
|
+
else
|
56
|
+
raise Chewy::UnderivableType.new("Index `#{class_name}` has more than one type, please specify type via `#{index_name}#type_name`")
|
57
|
+
end
|
46
58
|
end
|
47
|
-
end
|
48
59
|
|
49
|
-
|
50
|
-
|
51
|
-
|
60
|
+
# Creates Chewy::Type ancestor defining index and adapter methods.
|
61
|
+
#
|
62
|
+
def create_type index, target, options = {}, &block
|
63
|
+
type = Class.new(Chewy::Type)
|
52
64
|
|
53
|
-
|
54
|
-
|
55
|
-
|
65
|
+
adapter = if (target.is_a?(Class) && target < ActiveRecord::Base) || target.is_a?(::ActiveRecord::Relation)
|
66
|
+
Chewy::Type::Adapter::ActiveRecord.new(target, options)
|
67
|
+
else
|
68
|
+
Chewy::Type::Adapter::Object.new(target, options)
|
69
|
+
end
|
70
|
+
|
71
|
+
index.const_set(adapter.name, type)
|
72
|
+
type.send(:define_singleton_method, :index) { index }
|
73
|
+
type.send(:define_singleton_method, :adapter) { adapter }
|
74
|
+
|
75
|
+
type.class_eval &block if block
|
76
|
+
type
|
77
|
+
end
|
56
78
|
|
57
|
-
|
79
|
+
# Sends wait_for_status request to ElasticSearch with status
|
80
|
+
# defined in configuration.
|
81
|
+
#
|
82
|
+
# Does nothing in case of config `wait_for_status` is undefined.
|
83
|
+
#
|
84
|
+
def wait_for_status
|
85
|
+
client.cluster.health wait_for_status: Chewy.configuration[:wait_for_status] if Chewy.configuration[:wait_for_status].present?
|
86
|
+
end
|
87
|
+
|
88
|
+
# Deletes all corresponding indexes with current prefix from ElasticSearch.
|
89
|
+
# Be careful, if current prefix is blank, this will destroy all the indexes.
|
90
|
+
#
|
91
|
+
def massacre
|
92
|
+
Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].delete_if(&:blank?).join(?_))
|
93
|
+
Chewy.wait_for_status
|
94
|
+
end
|
95
|
+
alias_method :delete_all, :massacre
|
96
|
+
|
97
|
+
def config
|
98
|
+
Chewy::Config.instance
|
99
|
+
end
|
100
|
+
delegate *Chewy::Config.delegated, to: :config
|
101
|
+
end
|
58
102
|
end
|
data/lib/chewy/config.rb
CHANGED
@@ -139,10 +139,6 @@ module Chewy
|
|
139
139
|
options
|
140
140
|
end
|
141
141
|
|
142
|
-
def client?
|
143
|
-
!!Thread.current[:chewy_client]
|
144
|
-
end
|
145
|
-
|
146
142
|
def client
|
147
143
|
Thread.current[:chewy_client] ||= ::Elasticsearch::Client.new configuration
|
148
144
|
end
|
@@ -161,7 +157,7 @@ module Chewy
|
|
161
157
|
def stash *args
|
162
158
|
if args.any?
|
163
159
|
type, ids = *args
|
164
|
-
raise ArgumentError.new('Only Chewy::Type
|
160
|
+
raise ArgumentError.new('Only Chewy::Type accepted as the first argument') unless type < Chewy::Type
|
165
161
|
stash.last[type] ||= []
|
166
162
|
stash.last[type] |= ids
|
167
163
|
else
|
data/lib/chewy/index.rb
CHANGED
@@ -77,7 +77,7 @@ module Chewy
|
|
77
77
|
# passed.
|
78
78
|
#
|
79
79
|
def self.define_type(target, options = {}, &block)
|
80
|
-
type_class = Chewy
|
80
|
+
type_class = Chewy.create_type(self, target, options, &block)
|
81
81
|
self.type_hash = type_hash.merge(type_class.type_name => type_class)
|
82
82
|
|
83
83
|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
data/lib/chewy/query.rb
CHANGED
@@ -290,7 +290,7 @@ module Chewy
|
|
290
290
|
# added to the search request and combinded according to
|
291
291
|
# <tt>boost_mode</tt> and <tt>score_mode</tt>
|
292
292
|
#
|
293
|
-
# UsersIndex.script_score("doc['boost'].value", filter: { foo: :bar})
|
293
|
+
# UsersIndex.script_score("doc['boost'].value", filter: { term: {foo: :bar} })
|
294
294
|
# # => {body:
|
295
295
|
# query: {
|
296
296
|
# function_score: {
|
@@ -299,7 +299,7 @@ module Chewy
|
|
299
299
|
# script_score: {
|
300
300
|
# script: "doc['boost'].value"
|
301
301
|
# },
|
302
|
-
# filter: { foo: :bar }
|
302
|
+
# filter: { term: { foo: :bar } }
|
303
303
|
# }
|
304
304
|
# }]
|
305
305
|
# } } }
|
@@ -315,14 +315,14 @@ module Chewy
|
|
315
315
|
# This probably only makes sense if you specifiy a filter
|
316
316
|
# for the boost factor as well
|
317
317
|
#
|
318
|
-
# UsersIndex.boost_factor(23, filter: { foo: :bar})
|
318
|
+
# UsersIndex.boost_factor(23, filter: { term: { foo: :bar} })
|
319
319
|
# # => {body:
|
320
320
|
# query: {
|
321
321
|
# function_score: {
|
322
322
|
# query: { ...},
|
323
323
|
# functions: [{
|
324
324
|
# boost_factor: 23,
|
325
|
-
# filter: { foo: :bar }
|
325
|
+
# filter: { term: { foo: :bar } }
|
326
326
|
# }]
|
327
327
|
# } } }
|
328
328
|
def boost_factor(factor, options = {})
|
data/lib/chewy/type.rb
CHANGED
@@ -1,22 +1,57 @@
|
|
1
|
-
require 'chewy/
|
1
|
+
require 'chewy/index/search'
|
2
|
+
require 'chewy/type/mapping'
|
3
|
+
require 'chewy/type/wrapper'
|
4
|
+
require 'chewy/type/observe'
|
5
|
+
require 'chewy/type/actions'
|
6
|
+
require 'chewy/type/import'
|
7
|
+
require 'chewy/type/adapter/object'
|
8
|
+
require 'chewy/type/adapter/active_record'
|
2
9
|
|
3
10
|
module Chewy
|
4
|
-
|
5
|
-
|
6
|
-
|
11
|
+
class Type
|
12
|
+
include Chewy::Index::Search
|
13
|
+
include Mapping
|
14
|
+
include Wrapper
|
15
|
+
include Observe
|
16
|
+
include Actions
|
17
|
+
include Import
|
7
18
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
19
|
+
singleton_class.delegate :client, to: :index
|
20
|
+
|
21
|
+
def self.index
|
22
|
+
raise NotImplementedError
|
23
|
+
end
|
13
24
|
|
14
|
-
|
15
|
-
|
16
|
-
|
25
|
+
def self.adapter
|
26
|
+
raise NotImplementedError
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.type_name
|
30
|
+
adapter.type_name
|
31
|
+
end
|
17
32
|
|
18
|
-
|
19
|
-
|
33
|
+
def self.search_index
|
34
|
+
index
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.search_type
|
38
|
+
type_name
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.const_missing(name)
|
42
|
+
to_resolve = "#{self.to_s}::#{name}"
|
43
|
+
to_resolve[index.to_s] = ''
|
44
|
+
|
45
|
+
@__resolved_constants ||= {}
|
46
|
+
|
47
|
+
if to_resolve.empty? || @__resolved_constants[to_resolve]
|
48
|
+
super
|
49
|
+
else
|
50
|
+
@__resolved_constants[to_resolve] = true
|
51
|
+
to_resolve.constantize
|
52
|
+
end
|
53
|
+
rescue NotImplementedError
|
54
|
+
super
|
20
55
|
end
|
21
56
|
end
|
22
57
|
end
|
data/lib/chewy/type/actions.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'chewy/type/adapter/base'
|
2
2
|
|
3
3
|
module Chewy
|
4
|
-
|
4
|
+
class Type
|
5
5
|
module Adapter
|
6
6
|
class ActiveRecord < Base
|
7
7
|
def initialize *args
|
@@ -62,7 +62,7 @@ module Chewy
|
|
62
62
|
import_options = args.extract_options!
|
63
63
|
import_options[:batch_size] ||= BATCH_SIZE
|
64
64
|
collection = args.none? ? model_all :
|
65
|
-
(args.one? && args.first.is_a?(::ActiveRecord::Relation) ? args.first : args.flatten)
|
65
|
+
(args.one? && args.first.is_a?(::ActiveRecord::Relation) ? args.first : args.flatten.compact)
|
66
66
|
|
67
67
|
if collection.is_a?(::ActiveRecord::Relation)
|
68
68
|
result = true
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'chewy/type/adapter/base'
|
2
2
|
|
3
3
|
module Chewy
|
4
|
-
|
4
|
+
class Type
|
5
5
|
module Adapter
|
6
6
|
class Object < Base
|
7
7
|
def initialize *args
|
@@ -31,7 +31,7 @@ module Chewy
|
|
31
31
|
def import *args, &block
|
32
32
|
import_options = args.extract_options!
|
33
33
|
batch_size = import_options.delete(:batch_size) || BATCH_SIZE
|
34
|
-
objects = args.flatten
|
34
|
+
objects = args.flatten.compact
|
35
35
|
|
36
36
|
objects.in_groups_of(batch_size, false).map do |group|
|
37
37
|
action_groups = group.group_by do |object|
|
data/lib/chewy/type/import.rb
CHANGED
data/lib/chewy/type/mapping.rb
CHANGED
data/lib/chewy/type/observe.rb
CHANGED
data/lib/chewy/type/wrapper.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Chewy
|
2
|
-
|
2
|
+
class Type
|
3
3
|
module Wrapper
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
@@ -10,7 +10,7 @@ module Chewy
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def ==(other)
|
13
|
-
if other.is_a?(Chewy::Type
|
13
|
+
if other.is_a?(Chewy::Type)
|
14
14
|
self.class == other.class && (respond_to?(:id) ? id == other.id : attributes == other.attributes)
|
15
15
|
elsif other.respond_to?(:id)
|
16
16
|
id.to_s == other.id.to_s
|
data/lib/chewy/version.rb
CHANGED
data/spec/chewy/index_spec.rb
CHANGED
@@ -57,12 +57,40 @@ describe Chewy::Index do
|
|
57
57
|
before { stub_index(:dummies) { define_type City, name: :country } }
|
58
58
|
specify { DummiesIndex.type_hash['country'].should == DummiesIndex::Country }
|
59
59
|
end
|
60
|
+
|
61
|
+
|
62
|
+
context do
|
63
|
+
before { stub_model('City') }
|
64
|
+
before { stub_model('City::District', City) }
|
65
|
+
|
66
|
+
specify do
|
67
|
+
expect {
|
68
|
+
Kernel.eval <<-DUMMY_CITY_INDEX
|
69
|
+
class DummyCityIndex < Chewy::Index
|
70
|
+
define_type City
|
71
|
+
define_type City::District
|
72
|
+
end
|
73
|
+
DUMMY_CITY_INDEX
|
74
|
+
}.not_to raise_error
|
75
|
+
end
|
76
|
+
|
77
|
+
specify do
|
78
|
+
expect {
|
79
|
+
Kernel.eval <<-DUMMY_CITY_INDEX
|
80
|
+
class DummyCityIndex2 < Chewy::Index
|
81
|
+
define_type City
|
82
|
+
define_type City::Nothing
|
83
|
+
end
|
84
|
+
DUMMY_CITY_INDEX
|
85
|
+
}.to raise_error(NameError)
|
86
|
+
end
|
87
|
+
end
|
60
88
|
end
|
61
89
|
|
62
90
|
describe '.type_hash' do
|
63
91
|
specify { DummiesIndex.type_hash['dummy'].should == DummiesIndex::Dummy }
|
64
92
|
specify { DummiesIndex.type_hash.should have_key 'dummy' }
|
65
|
-
specify { DummiesIndex.type_hash['dummy'].should be < Chewy::Type
|
93
|
+
specify { DummiesIndex.type_hash['dummy'].should be < Chewy::Type }
|
66
94
|
specify { DummiesIndex.type_hash['dummy'].type_name.should == 'dummy' }
|
67
95
|
end
|
68
96
|
|
data/spec/chewy/query_spec.rb
CHANGED
@@ -227,15 +227,12 @@ describe Chewy::Query do
|
|
227
227
|
before { CitiesIndex::City.import! cities }
|
228
228
|
|
229
229
|
specify { CitiesIndex.aggregations.should == {} }
|
230
|
-
specify { CitiesIndex.aggregations(ratings: {terms: {field: 'rating'}})
|
231
|
-
'ratings'
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
]
|
237
|
-
}
|
238
|
-
} }
|
230
|
+
specify { CitiesIndex.aggregations(ratings: {terms: {field: 'rating'}})
|
231
|
+
.aggregations['ratings']['buckets'].map { |h| h.slice('key', 'doc_count') }.should == [
|
232
|
+
{ 'key' => 0, 'doc_count' => 4 },
|
233
|
+
{ 'key' => 1, 'doc_count' => 3 },
|
234
|
+
{ 'key' => 2, 'doc_count' => 3 }
|
235
|
+
] }
|
239
236
|
end
|
240
237
|
end
|
241
238
|
end
|
@@ -68,6 +68,9 @@ describe Chewy::Type::Adapter::ActiveRecord do
|
|
68
68
|
{delete: deleted.first(2).map(&:id)},
|
69
69
|
{delete: deleted.last(1).map(&:id)}] }
|
70
70
|
|
71
|
+
specify { import(cities.first, nil).should == [{index: [cities.first]}] }
|
72
|
+
specify { import(cities.first.id, nil).should == [{index: [cities.first]}] }
|
73
|
+
|
71
74
|
context do
|
72
75
|
before { deleted.map { |object| object.stub(delete_from_index?: true, destroyed?: true) } }
|
73
76
|
specify { import(deleted).should == [{delete: deleted}] }
|
@@ -54,6 +54,8 @@ describe Chewy::Type::Adapter::Object do
|
|
54
54
|
{index: objects.last(1), delete: deleted.first(1)},
|
55
55
|
{delete: deleted.last(1)}] }
|
56
56
|
|
57
|
+
specify { import(objects.first, nil).should == [{index: [objects.first]}] }
|
58
|
+
|
57
59
|
context do
|
58
60
|
let(:deleted) { 2.times.map { |i| double(delete_from_index?: true, destroyed?: true) } }
|
59
61
|
specify { import(deleted).should == [{delete: deleted}] }
|
@@ -31,7 +31,7 @@ describe Chewy::Type::Mapping do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
describe '.mappings_hash' do
|
34
|
-
specify { Class.new(Chewy::Type
|
34
|
+
specify { Class.new(Chewy::Type).mappings_hash.should == {} }
|
35
35
|
specify { product.mappings_hash.should == product.root_object.mappings_hash }
|
36
36
|
|
37
37
|
context 'parent-child relationship' do
|
data/spec/chewy/type_spec.rb
CHANGED
@@ -1,64 +1,4 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Chewy::Type do
|
4
|
-
describe '.new' do
|
5
|
-
before { stub_index(:cities) }
|
6
|
-
|
7
|
-
context 'Symbol' do
|
8
|
-
subject { described_class.new(CitiesIndex, :city) }
|
9
|
-
|
10
|
-
it { should be_a Class }
|
11
|
-
it { should be < Chewy::Type::Base }
|
12
|
-
its(:name) { should == 'CitiesIndex::City' }
|
13
|
-
its(:index) { should == CitiesIndex }
|
14
|
-
its(:type_name) { should == 'city' }
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'ActiveRecord model' do
|
18
|
-
before { stub_model(:city) }
|
19
|
-
subject { described_class.new(CitiesIndex, City) }
|
20
|
-
|
21
|
-
it { should be_a Class }
|
22
|
-
it { should be < Chewy::Type::Base }
|
23
|
-
its(:name) { should == 'CitiesIndex::City' }
|
24
|
-
its(:index) { should == CitiesIndex }
|
25
|
-
its(:type_name) { should == 'city' }
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'ActiveRecord scope' do
|
29
|
-
before { stub_model(:city) }
|
30
|
-
subject { described_class.new(CitiesIndex, City.includes(:country)) }
|
31
|
-
|
32
|
-
it { should be_a Class }
|
33
|
-
it { should be < Chewy::Type::Base }
|
34
|
-
its(:name) { should == 'CitiesIndex::City' }
|
35
|
-
its(:index) { should == CitiesIndex }
|
36
|
-
its(:type_name) { should == 'city' }
|
37
|
-
end
|
38
|
-
|
39
|
-
context 'Namespaced index' do
|
40
|
-
before { stub_model(:city) }
|
41
|
-
before { stub_index('namespace/cities') }
|
42
|
-
|
43
|
-
subject { described_class.new(Namespace::CitiesIndex, City) }
|
44
|
-
|
45
|
-
it { should be_a Class }
|
46
|
-
it { should be < Chewy::Type::Base }
|
47
|
-
its(:name) { should == 'Namespace::CitiesIndex::City' }
|
48
|
-
its(:index) { should == Namespace::CitiesIndex }
|
49
|
-
its(:type_name) { should == 'city' }
|
50
|
-
end
|
51
|
-
|
52
|
-
context 'Namespaced model' do
|
53
|
-
before { stub_model('namespace/city') }
|
54
|
-
|
55
|
-
subject { described_class.new(CitiesIndex, Namespace::City) }
|
56
|
-
|
57
|
-
it { should be_a Class }
|
58
|
-
it { should be < Chewy::Type::Base }
|
59
|
-
its(:name) { should == 'CitiesIndex::City' }
|
60
|
-
its(:index) { should == CitiesIndex }
|
61
|
-
its(:type_name) { should == 'city' }
|
62
|
-
end
|
63
|
-
end
|
64
4
|
end
|
data/spec/chewy_spec.rb
CHANGED
@@ -31,4 +31,85 @@ describe Chewy do
|
|
31
31
|
specify { described_class.derive_type('namespace/autocomplete#developer').should == Namespace::AutocompleteIndex.developer }
|
32
32
|
specify { described_class.derive_type('namespace/autocomplete#company').should == Namespace::AutocompleteIndex.company }
|
33
33
|
end
|
34
|
+
|
35
|
+
describe '.create_type' do
|
36
|
+
before { stub_index(:cities) }
|
37
|
+
|
38
|
+
context 'Symbol' do
|
39
|
+
subject { described_class.create_type(CitiesIndex, :city) }
|
40
|
+
|
41
|
+
it { should be_a Class }
|
42
|
+
it { should be < Chewy::Type }
|
43
|
+
its(:name) { should == 'CitiesIndex::City' }
|
44
|
+
its(:index) { should == CitiesIndex }
|
45
|
+
its(:type_name) { should == 'city' }
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'ActiveRecord model' do
|
49
|
+
before { stub_model(:city) }
|
50
|
+
subject { described_class.create_type(CitiesIndex, City) }
|
51
|
+
|
52
|
+
it { should be_a Class }
|
53
|
+
it { should be < Chewy::Type }
|
54
|
+
its(:name) { should == 'CitiesIndex::City' }
|
55
|
+
its(:index) { should == CitiesIndex }
|
56
|
+
its(:type_name) { should == 'city' }
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'ActiveRecord scope' do
|
60
|
+
before { stub_model(:city) }
|
61
|
+
subject { described_class.create_type(CitiesIndex, City.includes(:country)) }
|
62
|
+
|
63
|
+
it { should be_a Class }
|
64
|
+
it { should be < Chewy::Type }
|
65
|
+
its(:name) { should == 'CitiesIndex::City' }
|
66
|
+
its(:index) { should == CitiesIndex }
|
67
|
+
its(:type_name) { should == 'city' }
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'Namespaced index' do
|
71
|
+
before { stub_model(:city) }
|
72
|
+
before { stub_index('namespace/cities') }
|
73
|
+
|
74
|
+
subject { described_class.create_type(Namespace::CitiesIndex, City) }
|
75
|
+
|
76
|
+
it { should be_a Class }
|
77
|
+
it { should be < Chewy::Type }
|
78
|
+
its(:name) { should == 'Namespace::CitiesIndex::City' }
|
79
|
+
its(:index) { should == Namespace::CitiesIndex }
|
80
|
+
its(:type_name) { should == 'city' }
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'Namespaced model' do
|
84
|
+
before { stub_model('namespace/city') }
|
85
|
+
|
86
|
+
subject { described_class.create_type(CitiesIndex, Namespace::City) }
|
87
|
+
|
88
|
+
it { should be_a Class }
|
89
|
+
it { should be < Chewy::Type }
|
90
|
+
its(:name) { should == 'CitiesIndex::City' }
|
91
|
+
its(:index) { should == CitiesIndex }
|
92
|
+
its(:type_name) { should == 'city' }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '.massacre' do
|
97
|
+
before do
|
98
|
+
Chewy.client.indices.delete index: '*'
|
99
|
+
end
|
100
|
+
|
101
|
+
before do
|
102
|
+
Chewy.configuration = Chewy.configuration.merge(prefix: 'prefix1')
|
103
|
+
stub_index(:admins).create!
|
104
|
+
Chewy.configuration = Chewy.configuration.merge(prefix: 'prefix2')
|
105
|
+
stub_index(:developers).create!
|
106
|
+
stub_index(:companies).create!
|
107
|
+
|
108
|
+
Chewy.massacre
|
109
|
+
end
|
110
|
+
|
111
|
+
specify { AdminsIndex.exists?.should == true }
|
112
|
+
specify { DevelopersIndex.exists?.should == false }
|
113
|
+
specify { CompaniesIndex.exists?.should == false }
|
114
|
+
end
|
34
115
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chewy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pyromaniac
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -220,7 +220,6 @@ files:
|
|
220
220
|
- lib/chewy/type/adapter/active_record.rb
|
221
221
|
- lib/chewy/type/adapter/base.rb
|
222
222
|
- lib/chewy/type/adapter/object.rb
|
223
|
-
- lib/chewy/type/base.rb
|
224
223
|
- lib/chewy/type/import.rb
|
225
224
|
- lib/chewy/type/mapping.rb
|
226
225
|
- lib/chewy/type/observe.rb
|
data/lib/chewy/type/base.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'chewy/index/search'
|
2
|
-
require 'chewy/type/mapping'
|
3
|
-
require 'chewy/type/wrapper'
|
4
|
-
require 'chewy/type/observe'
|
5
|
-
require 'chewy/type/actions'
|
6
|
-
require 'chewy/type/import'
|
7
|
-
require 'chewy/type/adapter/object'
|
8
|
-
require 'chewy/type/adapter/active_record'
|
9
|
-
|
10
|
-
module Chewy
|
11
|
-
module Type
|
12
|
-
class Base
|
13
|
-
include Chewy::Index::Search
|
14
|
-
include Mapping
|
15
|
-
include Wrapper
|
16
|
-
include Observe
|
17
|
-
include Actions
|
18
|
-
include Import
|
19
|
-
|
20
|
-
singleton_class.delegate :client, to: :index
|
21
|
-
|
22
|
-
def self.index
|
23
|
-
raise NotImplementedError
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.adapter
|
27
|
-
raise NotImplementedError
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.type_name
|
31
|
-
adapter.type_name
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.search_index
|
35
|
-
index
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.search_type
|
39
|
-
type_name
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|