esse-active_record 0.3.6 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6ec661a171ddf9df565e1da2c3ca67bc34fc18ec957e47867e50b043ce0ee44
4
- data.tar.gz: 29e7acc6af3e77bbe52dde14038de3ad82532964dbc7293cdc77f2a3ae4764a3
3
+ metadata.gz: 9baa0f1dad618ca3520ad5cfefe4fb22d42e1411c56b7ff168e4503a1d720e75
4
+ data.tar.gz: 2d7c3aa59292b675600b556ddb07537bde6f5d833fc54c0e582f2886fc9f9ecc
5
5
  SHA512:
6
- metadata.gz: cc76722e48a2ea6e002b08dd9f29b53bed75cf4f857254dde166bfc30090fc16482e3b70fe47dbc7409e970dfb5d47b5dcefcc5bf413d56382f5e9a909e98837
7
- data.tar.gz: 76dcbec4c950921945faa13785273e523d82644d3757d3b27ee91f352f155371b218f9f1ff40b079cd04ad722acedf8c873b01532dc9143249b0616636b98b0e
6
+ metadata.gz: a61ef5cd41fc8a4f2b86819f101c47a88544f7b446467ab1a5fed06e77809684cc04a784ad4699fc36ab3feb4553c0879d0237954db423cfe1b3ac0067647fa0
7
+ data.tar.gz: 4984a7e20c4fd4d359682864d24b543a6e12759e463cbee048f658b3abdf3a27e8d5a8ae434ddd28820f45e699c05c4138de33e86bbae9c2683954005601a4ce
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
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.8 - 2024-08-08
8
+ * Add `connect_with:` option to the collection definition.
9
+ * Adjust UpdateLazyAttribute callback to not use keyword arguments for better compatibility with older versions of Ruby.
10
+
11
+ ## 0.3.7 - 2024-08-05
12
+ * Add `connected_to` to the collection for custom connection handling
13
+
14
+ ## 0.0.1
15
+ The first release of the esse-active_record plugin
16
+ * 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.6)
4
+ esse-active_record (0.3.8)
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.6)
4
+ esse-active_record (0.3.8)
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.6)
4
+ esse-active_record (0.3.8)
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.6)
4
+ esse-active_record (0.3.8)
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.6)
4
+ esse-active_record (0.3.8)
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.6)
4
+ esse-active_record (0.3.8)
5
5
  activerecord (>= 4.2, < 8)
6
6
  esse (>= 0.3.0)
7
7
 
@@ -11,10 +11,10 @@ module Esse::ActiveRecord
11
11
  end
12
12
 
13
13
  def call(model)
14
- related_ids = Array(block_result || model.id)
14
+ related_ids = Esse::ArrayUtils.wrap(block_result || model.id)
15
15
  return true if related_ids.empty?
16
16
 
17
- repo.update_documents_attribute(attribute_name, *related_ids, **options)
17
+ repo.update_documents_attribute(attribute_name, related_ids, options)
18
18
 
19
19
  true
20
20
  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, :group, :order, :limit, :offset).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.6'
5
+ VERSION = '0.3.8'
6
6
  end
7
7
  end
@@ -18,6 +18,7 @@ module Esse
18
18
  repo = Class.new(Esse::ActiveRecord::Collection)
19
19
  repo.base_scope = -> { model_class }
20
20
  repo.batch_size = kwargs.delete(:batch_size) if kwargs.key?(:batch_size)
21
+ repo.connect_with = kwargs.delete(:connect_with) if kwargs.key?(:connect_with)
21
22
  repo.class_eval(&block) if block
22
23
 
23
24
  super(repo, *args, **kwargs)
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.6
4
+ version: 0.3.8
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-08-02 00:00:00.000000000 Z
11
+ date: 2024-08-08 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