esse-active_record 0.3.6 → 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: d6ec661a171ddf9df565e1da2c3ca67bc34fc18ec957e47867e50b043ce0ee44
4
- data.tar.gz: 29e7acc6af3e77bbe52dde14038de3ad82532964dbc7293cdc77f2a3ae4764a3
3
+ metadata.gz: 412017405772bf674a32c43510c0fdd15c0d1ac9de09357e523aa87c7d8bb0ab
4
+ data.tar.gz: 869613dcb7b78c03097989dbeb2d565dc790b119043d7a1a7e7183ccdca1ba5f
5
5
  SHA512:
6
- metadata.gz: cc76722e48a2ea6e002b08dd9f29b53bed75cf4f857254dde166bfc30090fc16482e3b70fe47dbc7409e970dfb5d47b5dcefcc5bf413d56382f5e9a909e98837
7
- data.tar.gz: 76dcbec4c950921945faa13785273e523d82644d3757d3b27ee91f352f155371b218f9f1ff40b079cd04ad722acedf8c873b01532dc9143249b0616636b98b0e
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.6)
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.6)
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.6)
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.6)
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.6)
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.6)
4
+ esse-active_record (0.3.7)
5
5
  activerecord (>= 4.2, < 8)
6
6
  esse (>= 0.3.0)
7
7
 
@@ -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.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.6
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-08-02 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