rails_band 0.12.0 → 0.13.0

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: 0b46032c94cc57bc9ae8bb0208502853a98bfca87530fd28cd5d356272489f2a
4
- data.tar.gz: 962cf4666c9925b5f1dab87088224ba223021aded56739b72582a1a8ebf4234f
3
+ metadata.gz: 25195f352d9f1105a0dc497aff31a087310185a09133674516ec77e1f44b00ab
4
+ data.tar.gz: 62a96059041df354354cbb871997bc367c0fd5132e526c3888ac955796b326df
5
5
  SHA512:
6
- metadata.gz: 14a38b8df1ef166a7bc62c52c3408a0e054768892bc97f6bd547d8b2f486e60c5dad7fd5aefaa7f41416de3e0e48cacf3994140b0bc355b25439501845c8b6e9
7
- data.tar.gz: 30fd719ea30ddec606e8bc298ada082e38a79062b84e923054f19276f121484e9ca0bd9b0a56b6dbf531c1a7b258cbd988abe54f709dde91659a4516548c7860
6
+ metadata.gz: a4518ad9565c64461de711c8d73e6567046e81385ed5b05fb17a63e35495b767b02d6177ca6952a6496a7152e6c75a82351d805fdcbbc9e977cd348dffe14db9
7
+ data.tar.gz: d8b7090706903ba8dc95bd7e1ff7823c1354577483407829fc94ed5194d6e9443b6f1c36de106998641be1549834fdb2c135002b5981f4fa0e83b19bd97bd521
data/README.md CHANGED
@@ -89,12 +89,13 @@ These are Rails Instrumentation API hooks supported by this gem so far.
89
89
 
90
90
  ### Active Record
91
91
 
92
- | Event name | Supported |
93
- | ------------------------------------------------------------------------------------------------------------------------------- | --------- |
94
- | `strict_loading_violation.active_record` (Not yet documented. See the configuration of `action_on_strict_loading_violation`) | ✅ |
95
- | [`sql.active_record`](https://guides.rubyonrails.org/active_support_instrumentation.html#sql-active-record) | ✅ |
96
- | [`instantiation.active_record`](https://guides.rubyonrails.org/active_support_instrumentation.html#instantiation-active-record) | ✅ |
97
- | [`transaction.active_record`](https://edgeguides.rubyonrails.org/active_support_instrumentation.html#transaction-active-record) | |
92
+ | Event name | Supported |
93
+ | ------------------------------------------------------------------------------------------------------------------------------------------- | --------- |
94
+ | `strict_loading_violation.active_record` (Not yet documented. See the configuration of `action_on_strict_loading_violation`) | ✅ |
95
+ | [`sql.active_record`](https://guides.rubyonrails.org/active_support_instrumentation.html#sql-active-record) | ✅ |
96
+ | [`instantiation.active_record`](https://guides.rubyonrails.org/active_support_instrumentation.html#instantiation-active-record) | ✅ |
97
+ | [`start_transaction.active_record`](https://edgeguides.rubyonrails.org/active_support_instrumentation.html#start-transaction-active-record) ||
98
+ | [`transaction.active_record`](https://edgeguides.rubyonrails.org/active_support_instrumentation.html#transaction-active-record) | |
98
99
 
99
100
  ### Action Mailer
100
101
 
@@ -176,9 +177,9 @@ These are Rails Instrumentation API hooks supported by this gem so far.
176
177
 
177
178
  ### Railties
178
179
 
179
- | Event name | Supported |
180
- | ----------------------------------------------------------------------------------------------------------------------------------------- | --------- |
181
- | [`load_config_initializer.railties`](https://guides.rubyonrails.org/active_support_instrumentation.html#load-config-initializer-railties) | |
180
+ | Event name | Supported |
181
+ | --------------------------------------------------------------------------------------------------------------------------------------------- | --------- |
182
+ | [`load_config_initializer.railties`](https://edgeguides.rubyonrails.org/active_support_instrumentation.html#load-config-initializer-railties) | |
182
183
 
183
184
  ### Rails
184
185
 
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsBand
4
+ module ActionController
5
+ module Event
6
+ # A wrapper for the event that is passed to `rate_limit.action_controller`.
7
+ class RateLimit < BaseEvent
8
+ def request
9
+ @request ||= @event.payload.fetch(:request)
10
+ end
11
+
12
+ # @note The following attributes are only available in Rails 8.1+
13
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('8.1.0.alpha')
14
+ define_method(:count) do
15
+ @count ||= @event.payload.fetch(:count)
16
+ end
17
+
18
+ define_method(:to) do
19
+ @to ||= @event.payload.fetch(:to)
20
+ end
21
+
22
+ define_method(:within) do
23
+ @within ||= @event.payload.fetch(:within)
24
+ end
25
+
26
+ define_method(:by) do
27
+ @by ||= @event.payload.fetch(:by)
28
+ end
29
+
30
+ # @note This method is renamed in order to avoid conflicts with BaseEvent#name.
31
+ define_method(:rate_limit_name) do
32
+ return @rate_limit_name if defined? @rate_limit_name
33
+
34
+ @rate_limit_name = @event.payload[:name]
35
+ end
36
+
37
+ define_method(:scope) do
38
+ @scope ||= @event.payload.fetch(:scope)
39
+ end
40
+
41
+ define_method(:cache_key) do
42
+ @cache_key ||= @event.payload.fetch(:cache_key)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -12,6 +12,7 @@ require 'rails_band/action_controller/event/send_data'
12
12
  require 'rails_band/action_controller/event/redirect_to'
13
13
  require 'rails_band/action_controller/event/halted_callback'
14
14
  require 'rails_band/action_controller/event/unpermitted_parameters'
15
+ require 'rails_band/action_controller/event/rate_limit'
15
16
 
16
17
  module RailsBand
17
18
  module ActionController
@@ -71,6 +72,10 @@ module RailsBand
71
72
  consumer_of(__method__)&.call(Event::UnpermittedParameters.new(event))
72
73
  end
73
74
 
75
+ def rate_limit(event)
76
+ consumer_of(__method__)&.call(Event::RateLimit.new(event))
77
+ end
78
+
74
79
  private
75
80
 
76
81
  def consumer_of(sub_event)
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsBand
4
+ module ActiveRecord
5
+ module Event
6
+ # A wrapper for the event that is passed to `start_transaction.active_record`.
7
+ class StartTransaction < BaseEvent
8
+ def transaction
9
+ @transaction ||= @event.payload.fetch(:transaction)
10
+ end
11
+
12
+ def connection
13
+ @connection ||= @event.payload.fetch(:connection)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -3,6 +3,7 @@
3
3
  require 'rails_band/active_record/event/sql'
4
4
  require 'rails_band/active_record/event/instantiation'
5
5
  require 'rails_band/active_record/event/strict_loading_violation'
6
+ require 'rails_band/active_record/event/start_transaction'
6
7
 
7
8
  module RailsBand
8
9
  module ActiveRecord
@@ -22,6 +23,10 @@ module RailsBand
22
23
  consumer_of(__method__)&.call(Event::Instantiation.new(event))
23
24
  end
24
25
 
26
+ def start_transaction(event)
27
+ consumer_of(__method__)&.call(Event::StartTransaction.new(event))
28
+ end
29
+
25
30
  private
26
31
 
27
32
  def consumer_of(sub_event)
@@ -12,7 +12,9 @@ module RailsBand
12
12
  # NOTE: `ActionDispatch::MiddlewareStack::InstrumentationProxy` will be called
13
13
  # only when `ActionDispatch::MiddlewareStack#build` detects `process_middleware.action_dispatch`
14
14
  # is listened to. So, `attach_to` must be called before Rack middlewares will be loaded.
15
- ::ActionDispatch::LogSubscriber.detach_from :action_dispatch if defined?(::ActionDispatch::LogSubscriber)
15
+ if defined?(::ActionDispatch::LogSubscriber)
16
+ detach_log_subscriber(::ActionDispatch::LogSubscriber, :action_dispatch)
17
+ end
16
18
  RailsBand::ActionDispatch::LogSubscriber.attach_to :action_dispatch
17
19
  end
18
20
 
@@ -20,7 +22,7 @@ module RailsBand
20
22
  consumers = app.config.rails_band.consumers
21
23
 
22
24
  swap = lambda { |old_class, new_class, namespace|
23
- old_class.detach_from namespace
25
+ detach_log_subscriber(old_class, namespace)
24
26
  new_class.consumers = consumers
25
27
  new_class.attach_to namespace
26
28
  }
@@ -72,5 +74,19 @@ module RailsBand
72
74
  RailsBand::ActionMailbox::LogSubscriber.attach_to :action_mailbox
73
75
  end
74
76
  end
77
+
78
+ # Helper method to detach log subscribers in a way that's compatible with both
79
+ # Rails < 8.1 (using detach_from) and Rails >= 8.1 (using unsubscribe)
80
+ def self.detach_log_subscriber(log_subscriber_class, namespace)
81
+ if log_subscriber_class.respond_to?(:detach_from)
82
+ # Rails < 8.1: use detach_from method
83
+ log_subscriber_class.detach_from namespace
84
+ elsif log_subscriber_class.respond_to?(:unsubscribe)
85
+ # Rails >= 8.1: use unsubscribe method
86
+ log_subscriber_class.unsubscribe
87
+ end
88
+ end
89
+
90
+ private_class_method :detach_log_subscriber
75
91
  end
76
92
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsBand
4
- VERSION = '0.12.0'
4
+ VERSION = '0.13.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_band
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yutaka Kamei
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-10-02 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rails
@@ -51,6 +50,7 @@ files:
51
50
  - lib/rails_band/action_controller/event/expire_fragment.rb
52
51
  - lib/rails_band/action_controller/event/halted_callback.rb
53
52
  - lib/rails_band/action_controller/event/process_action.rb
53
+ - lib/rails_band/action_controller/event/rate_limit.rb
54
54
  - lib/rails_band/action_controller/event/read_fragment.rb
55
55
  - lib/rails_band/action_controller/event/redirect_to.rb
56
56
  - lib/rails_band/action_controller/event/send_data.rb
@@ -86,6 +86,7 @@ files:
86
86
  - lib/rails_band/active_job/log_subscriber.rb
87
87
  - lib/rails_band/active_record/event/instantiation.rb
88
88
  - lib/rails_band/active_record/event/sql.rb
89
+ - lib/rails_band/active_record/event/start_transaction.rb
89
90
  - lib/rails_band/active_record/event/strict_loading_violation.rb
90
91
  - lib/rails_band/active_record/log_subscriber.rb
91
92
  - lib/rails_band/active_storage/event/analyze.rb
@@ -129,7 +130,6 @@ metadata:
129
130
  homepage_uri: https://github.com/yykamei/rails_band
130
131
  source_code_uri: https://github.com/yykamei/rails_band
131
132
  rubygems_mfa_required: 'true'
132
- post_install_message:
133
133
  rdoc_options: []
134
134
  require_paths:
135
135
  - lib
@@ -144,8 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  - !ruby/object:Gem::Version
145
145
  version: '0'
146
146
  requirements: []
147
- rubygems_version: 3.5.16
148
- signing_key:
147
+ rubygems_version: 3.6.9
149
148
  specification_version: 4
150
149
  summary: Easy-to-use Rails Instrumentation API
151
150
  test_files: []