esse-active_record 0.3.5 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: edf541425801e7e18e816e6a0169c021f52017829ada9643f959452ba207a658
4
- data.tar.gz: 8cbc35bd232d9c6fde1cc6ef79aba08feffd00db04dfd6238cf60f4058457bd5
3
+ metadata.gz: 412017405772bf674a32c43510c0fdd15c0d1ac9de09357e523aa87c7d8bb0ab
4
+ data.tar.gz: 869613dcb7b78c03097989dbeb2d565dc790b119043d7a1a7e7183ccdca1ba5f
5
5
  SHA512:
6
- metadata.gz: 3125eb0d88c23be59bdb5a889005357833b50cde303be94ed9e2fce2fc008bb59e2c636d7c08f14b9f028a84aad6f35d43151824669210916067dfa2e42f6991
7
- data.tar.gz: 217f00f691e17cc13402a63e799220a94807194205fd1439ebe426f1e36bcef73511793cd71a8a3931c0b1e975cb7b3fa2ada1b92eac8de8151f2f70016a5168
6
+ metadata.gz: b3247f133ad930ea4ac1e69d3a242113c7ac26bc8de33737f350321fbe3b9de01ec21648310ff1b7c7ba326bca763107b4c313c663ae82686bb24da7d47e1cd3
7
+ data.tar.gz: 63593435b2cea9284e530351398960f5b73d2aceaad6cb336b30cec90e11cb419b4ad7a55a80265a91cb5b2b9c7704572758129d135ddc5f611e0f98f1be0de6
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## 0.3.7 - 2024-08-05
8
+ * Add `connected_to` to the collection for custom connection handling
9
+
10
+ ## 0.0.1
11
+ The first release of the esse-active_record plugin
12
+ * Added: Initial implementation of the plugin
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- esse-active_record (0.3.5)
4
+ esse-active_record (0.3.7)
5
5
  activerecord (>= 4.2, < 8)
6
6
  esse (>= 0.3.0)
7
7
 
data/README.md CHANGED
@@ -210,6 +210,35 @@ User.without_indexing(AccountsIndex) do
210
210
  end
211
211
  ```
212
212
 
213
+ ### Asynchronous Indexing
214
+
215
+ If you are using a background job processor like Sidekiq or Faktory, you may be interested in indexing documents asynchronously. For this, you can use the [esse-async_indexing](https://github.com/marcosgz/esse-async_indexing) gem.
216
+
217
+ Add the `esse-async_indexing` gem to your Gemfile and require the `esse/async_indexing/active_record` file in your application initialization. Make sure to setup the gem configurationg according to the [esse-async_indexing documentation](https://github.com/marcosgz/esse-async_indexing).
218
+
219
+
220
+ ```ruby
221
+ require 'esse/async_indexing/active_record'
222
+ ```
223
+
224
+ Then, you can use the `async_index_callback` or `async_update_lazy_attribute_callback` methods to push the indexing job to the background job processor.
225
+
226
+ ```diff
227
+ class City < ApplicationRecord
228
+ include Esse::ActiveRecord::Model
229
+ - include Esse::ActiveRecord::Model
230
+ + include Esse::AsyncIndexing::ActiveRecord::Model
231
+
232
+ belongs_to :state, optional: true
233
+
234
+
235
+ async_indexing_callback('geos_index:city') { id }
236
+ - index_callback('geos_index:city') { id }
237
+ - update_lazy_attribute_callback('geos_index:state', 'cities_count', if: :state_id?) { state_id }
238
+ + async_index_callback('geos_index:city', service_name: :sidekiq) { id }
239
+ + async_update_lazy_attribute_callback('geos_index:state', 'cities_count', if: :state_id?, service_name: :sidekiq) { state_id }
240
+ end
241
+ ```
213
242
 
214
243
  ## Development
215
244
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- esse-active_record (0.3.5)
4
+ esse-active_record (0.3.7)
5
5
  activerecord (>= 4.2, < 8)
6
6
  esse (>= 0.3.0)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- esse-active_record (0.3.5)
4
+ esse-active_record (0.3.7)
5
5
  activerecord (>= 4.2, < 8)
6
6
  esse (>= 0.3.0)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- esse-active_record (0.3.5)
4
+ esse-active_record (0.3.7)
5
5
  activerecord (>= 4.2, < 8)
6
6
  esse (>= 0.3.0)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- esse-active_record (0.3.5)
4
+ esse-active_record (0.3.7)
5
5
  activerecord (>= 4.2, < 8)
6
6
  esse (>= 0.3.0)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- esse-active_record (0.3.5)
4
+ esse-active_record (0.3.7)
5
5
  activerecord (>= 4.2, < 8)
6
6
  esse (>= 0.3.0)
7
7
 
@@ -3,13 +3,6 @@
3
3
  module Esse::ActiveRecord
4
4
  module Callbacks
5
5
  class IndexingOnUpdate < Callback
6
- attr_reader :update_with
7
-
8
- def initialize(with: :index, **kwargs, &block)
9
- @update_with = with
10
- super(**kwargs, &block)
11
- end
12
-
13
6
  def call(model)
14
7
  record = block_result || model
15
8
 
@@ -43,7 +36,7 @@ module Esse::ActiveRecord
43
36
  def update_document(document)
44
37
  return if document.ignore_on_index?
45
38
 
46
- if update_with == :update
39
+ if @with == :update || (@with.nil? && repo.lazy_document_attributes.any?)
47
40
  begin
48
41
  repo.index.update(document, **options)
49
42
  rescue Esse::Transport::NotFoundError
@@ -5,8 +5,9 @@ module Esse
5
5
  class Callback
6
6
  attr_reader :repo, :options, :block_result
7
7
 
8
- def initialize(repo:, block_result: nil, **kwargs)
8
+ def initialize(repo:, block_result: nil, with: nil, **kwargs)
9
9
  @repo = repo
10
+ @with = with
10
11
  @options = kwargs
11
12
  @block_result = block_result
12
13
  end
@@ -21,6 +21,12 @@ module Esse
21
21
  class_attribute :batch_contexts
22
22
  self.batch_contexts = {}
23
23
 
24
+ # Connects to a database or role (ex writing, reading, or another custom role) for the collection query
25
+ # @param [Symbol] role The role to connect to
26
+ # @param [Symbol] shard The shard to connect to
27
+ class_attribute :connect_with
28
+ self.connect_with = nil
29
+
24
30
  class << self
25
31
  def inspect
26
32
  return super unless self < Esse::ActiveRecord::Collection
@@ -40,6 +46,7 @@ module Esse
40
46
 
41
47
  subclass.scopes = scopes.dup
42
48
  subclass.batch_contexts = batch_contexts.dup
49
+ subclass.connect_with = connect_with&.dup
43
50
  end
44
51
 
45
52
  def scope(name, proc = nil, override: false, &block)
@@ -57,6 +64,10 @@ module Esse
57
64
 
58
65
  batch_contexts[name.to_sym] = proc
59
66
  end
67
+
68
+ def connected_to(**kwargs)
69
+ self.connect_with = kwargs
70
+ end
60
71
  end
61
72
 
62
73
  attr_reader :start, :finish, :batch_size, :params
@@ -74,23 +85,29 @@ module Esse
74
85
  end
75
86
 
76
87
  def each
77
- dataset.find_in_batches(**batch_options) do |rows|
78
- kwargs = params.dup
79
- self.class.batch_contexts.each do |name, proc|
80
- kwargs[name] = proc.call(rows, **params)
88
+ with_connection do
89
+ dataset.find_in_batches(**batch_options) do |rows|
90
+ kwargs = params.dup
91
+ self.class.batch_contexts.each do |name, proc|
92
+ kwargs[name] = proc.call(rows, **params)
93
+ end
94
+ yield(rows, **kwargs)
81
95
  end
82
- yield(rows, **kwargs)
83
96
  end
84
97
  end
85
98
 
86
99
  def each_batch_ids
87
- dataset.select(:id).except(:includes, :preload, :eager_load).find_in_batches(**batch_options) do |rows|
88
- yield(rows.map(&:id))
100
+ with_connection do
101
+ dataset.select(:id).except(:includes, :preload, :eager_load).find_in_batches(**batch_options) do |rows|
102
+ yield(rows.map(&:id))
103
+ end
89
104
  end
90
105
  end
91
106
 
92
107
  def count
93
- dataset.except(:includes, :preload, :eager_load).count
108
+ with_connection do
109
+ dataset.except(:includes, :preload, :eager_load, :group, :order, :limit, :offset).count
110
+ end
94
111
  end
95
112
  alias_method :size, :count
96
113
 
@@ -127,6 +144,16 @@ module Esse
127
144
 
128
145
  protected
129
146
 
147
+ def with_connection
148
+ if self.class.connect_with&.any?
149
+ ::ActiveRecord::Base.connected_to(**self.class.connect_with) do
150
+ yield
151
+ end
152
+ else
153
+ yield
154
+ end
155
+ end
156
+
130
157
  def batch_options
131
158
  {
132
159
  batch_size: batch_size
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Esse
4
4
  module ActiveRecord
5
- VERSION = '0.3.5'
5
+ VERSION = '0.3.7'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esse-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos G. Zimmermann
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-29 00:00:00.000000000 Z
11
+ date: 2024-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: esse
@@ -206,6 +206,7 @@ extensions: []
206
206
  extra_rdoc_files: []
207
207
  files:
208
208
  - ".rubocop.yml"
209
+ - CHANGELOG.md
209
210
  - Gemfile
210
211
  - Gemfile.lock
211
212
  - LICENSE.txt