elasticsearch-model-extensions 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +7 -0
- data/lib/elasticsearch/model/extensions/all.rb +5 -0
- data/lib/elasticsearch/model/extensions/configuration.rb +11 -8
- data/lib/elasticsearch/model/extensions/outer_document_updating.rb +13 -7
- data/lib/elasticsearch/model/extensions/version.rb +1 -1
- data/spec/integration_spec.rb +46 -0
- data/spec/spec_helper.rb +35 -0
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 187c414c19b2a65be386cf8c75eb4ee039440b2f
|
4
|
+
data.tar.gz: 18ef824732bed168ee717707eba28d5717418cde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c8f14d33a0eb2e91ac6c8aaaa3de81a598e3593fc016690c99ec74e21acde23bdf82d14626ce8829f3aa05361f2398a04e0fee068001a4a5b22537524c8c6bf
|
7
|
+
data.tar.gz: 69cc264beccef5a22492fe67597a843af325db385e56303739e4a09a51fc896f6a22b73a7799115b692370914a8d2bd1d660986ada30a8f3de7c1d2a8fdbce43
|
data/Gemfile
CHANGED
@@ -4,13 +4,14 @@ module Elasticsearch
|
|
4
4
|
class Configuration
|
5
5
|
attr_reader :delayed
|
6
6
|
|
7
|
-
def initialize(active_record_class, parent_class: parent_class, delayed:,
|
7
|
+
def initialize(active_record_class, parent_class: parent_class, delayed:, only_if: -> r { true }, records_to_update_documents: nil, field_to_update: nil)
|
8
8
|
@delayed = @delayed
|
9
9
|
|
10
10
|
@active_record_class = active_record_class
|
11
11
|
@parent_class = parent_class
|
12
|
-
@if = binding.local_variable_get(:
|
12
|
+
@if = binding.local_variable_get(:only_if)
|
13
13
|
@records_to_update_documents = records_to_update_documents
|
14
|
+
@field_to_update = field_to_update
|
14
15
|
end
|
15
16
|
|
16
17
|
def to_hash
|
@@ -38,13 +39,15 @@ module Elasticsearch
|
|
38
39
|
def build_hash
|
39
40
|
child_class = @active_record_class
|
40
41
|
|
41
|
-
|
42
|
-
|
42
|
+
field_to_update = -> {
|
43
|
+
path = child_class.path_from(@parent_class)
|
44
|
+
parent_to_child_path = path.map(&:name)
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
# a has_a b has_a cという関係のとき、cが更新されたらaのフィールドbをupdateする必要がある。
|
47
|
+
# そのとき、
|
48
|
+
# 親aから子cへのパスが[:b, :c]だったら、bだけをupdateすればよいので
|
49
|
+
parent_to_child_path.first
|
50
|
+
}.call || @field_to_update
|
48
51
|
|
49
52
|
puts "#{child_class.name} updates #{@parent_class.name}'s #{field_to_update}"
|
50
53
|
|
@@ -104,20 +104,26 @@ module Elasticsearch
|
|
104
104
|
first
|
105
105
|
end
|
106
106
|
|
107
|
-
def initialize_active_record!(active_record_class, parent_class: parent_class, delayed:,
|
108
|
-
config = Elasticsearch::Model::Extensions::Configuration.new(active_record_class, parent_class: parent_class, delayed: delayed,
|
107
|
+
def initialize_active_record!(active_record_class, parent_class: parent_class, delayed:, only_if: -> r { true }, records_to_update_documents: nil, field_to_update: nil)
|
108
|
+
config = Elasticsearch::Model::Extensions::Configuration.new(active_record_class, parent_class: parent_class, delayed: delayed, only_if: binding.local_variable_get(:only_if), records_to_update_documents: records_to_update_documents,
|
109
|
+
field_to_update: field_to_update)
|
109
110
|
|
110
111
|
active_record_class.after_commit Elasticsearch::Model::Extensions::UpdateCallback.new(config)
|
111
112
|
active_record_class.after_commit Elasticsearch::Model::Extensions::DestroyCallback.new(config), on: :destroy
|
112
113
|
end
|
113
114
|
|
114
|
-
def partially_updates_document_of(parent_class,
|
115
|
+
def partially_updates_document_of(parent_class, options)
|
116
|
+
options ||= {}
|
117
|
+
delayed = options[:delayed] || nil
|
118
|
+
only_if = options[:if] || (-> r { true })
|
119
|
+
records_to_update_documents = options[:records_to_update_documents] || nil
|
120
|
+
|
115
121
|
initialize_active_record!(
|
116
122
|
self,
|
117
|
-
parent_class
|
118
|
-
delayed
|
119
|
-
|
120
|
-
records_to_update_documents
|
123
|
+
:parent_class => parent_class,
|
124
|
+
:delayed => delayed,
|
125
|
+
:only_if => only_if,
|
126
|
+
:records_to_update_documents => records_to_update_documents
|
121
127
|
)
|
122
128
|
end
|
123
129
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'elasticsearch/model/extensions/all'
|
2
|
+
|
3
|
+
RSpec.describe 'example' do
|
4
|
+
before :all do
|
5
|
+
ActiveRecord::Schema.define(:version => 1) do
|
6
|
+
create_table :articles do |t|
|
7
|
+
t.string :title
|
8
|
+
t.datetime :created_at, :default => 'NOW()'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ::Article < ActiveRecord::Base
|
13
|
+
include Elasticsearch::Model
|
14
|
+
include Elasticsearch::Model::Callbacks
|
15
|
+
include Elasticsearch::Model::Extensions::IndexOperations
|
16
|
+
include Elasticsearch::Model::Extensions::BatchUpdating
|
17
|
+
include Elasticsearch::Model::Extensions::PartialUpdating
|
18
|
+
|
19
|
+
DEPENDENT_CUSTOM_ATTRIBUTES = {}
|
20
|
+
|
21
|
+
include Elasticsearch::Model::Extensions::DependencyTracking
|
22
|
+
|
23
|
+
settings index: {number_of_shards: 1, number_of_replicas: 0} do
|
24
|
+
mapping do
|
25
|
+
indexes :title, type: 'string', analyzer: 'snowball'
|
26
|
+
indexes :created_at, type: 'date'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Article.delete_all
|
32
|
+
Article.__elasticsearch__.create_index! force: true
|
33
|
+
|
34
|
+
::Article.create! title: 'Test'
|
35
|
+
::Article.create! title: 'Testing Coding'
|
36
|
+
::Article.create! title: 'Coding'
|
37
|
+
|
38
|
+
Article.__elasticsearch__.refresh_index!
|
39
|
+
end
|
40
|
+
|
41
|
+
subject {
|
42
|
+
::Article.create(title: 'foo', created_at: Time.now)
|
43
|
+
}
|
44
|
+
|
45
|
+
it { is_expected.not_to be_nil }
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
config.expect_with :rspec do |expectations|
|
3
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
4
|
+
end
|
5
|
+
|
6
|
+
config.mock_with :rspec do |mocks|
|
7
|
+
mocks.verify_partial_doubles = true
|
8
|
+
end
|
9
|
+
|
10
|
+
config.profile_examples = 10
|
11
|
+
|
12
|
+
config.order = :random
|
13
|
+
|
14
|
+
Kernel.srand config.seed
|
15
|
+
|
16
|
+
config.before(:all) do
|
17
|
+
require 'active_record'
|
18
|
+
require 'logger'
|
19
|
+
require 'elasticsearch/model'
|
20
|
+
|
21
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:")
|
22
|
+
logger = ::Logger.new(STDERR)
|
23
|
+
logger.formatter = lambda { |s, d, p, m| "\e[2;36m#{m}\e[0m\n" }
|
24
|
+
ActiveRecord::Base.logger = logger unless ENV['QUIET']
|
25
|
+
|
26
|
+
ActiveRecord::LogSubscriber.colorize_logging = false
|
27
|
+
ActiveRecord::Migration.verbose = false
|
28
|
+
|
29
|
+
tracer = ::Logger.new(STDERR)
|
30
|
+
tracer.formatter = lambda { |s, d, p, m| "#{m.gsub(/^.*$/) { |n| ' ' + n }}\n" }
|
31
|
+
|
32
|
+
Elasticsearch::Model.client = Elasticsearch::Client.new host: "localhost:#{(ENV['TEST_CLUSTER_PORT'] || 9250)}",
|
33
|
+
tracer: (ENV['QUIET'] ? nil : tracer)
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elasticsearch-model-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yusuke KUOKA
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- Rakefile
|
54
54
|
- elasticsearch-model-extensions.gemspec
|
55
55
|
- lib/elasticsearch/model/extensions.rb
|
56
|
+
- lib/elasticsearch/model/extensions/all.rb
|
56
57
|
- lib/elasticsearch/model/extensions/batch_updating.rb
|
57
58
|
- lib/elasticsearch/model/extensions/callback.rb
|
58
59
|
- lib/elasticsearch/model/extensions/configuration.rb
|
@@ -66,6 +67,8 @@ files:
|
|
66
67
|
- lib/elasticsearch/model/extensions/shortest_path.rb
|
67
68
|
- lib/elasticsearch/model/extensions/update_callback.rb
|
68
69
|
- lib/elasticsearch/model/extensions/version.rb
|
70
|
+
- spec/integration_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
69
72
|
homepage: https://github.com/crowdworks/elasticsearch-model-extensions
|
70
73
|
licenses:
|
71
74
|
- MIT
|
@@ -92,4 +95,6 @@ specification_version: 4
|
|
92
95
|
summary: A set of extensions for elasticsearch-model which aims to ease the burden
|
93
96
|
of things like re-indexing, verbose/complex mapping that you may face once you started
|
94
97
|
using elasticsearch seriously.
|
95
|
-
test_files:
|
98
|
+
test_files:
|
99
|
+
- spec/integration_spec.rb
|
100
|
+
- spec/spec_helper.rb
|