Skip to content

Commit

Permalink
Add pay on_load hook
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed May 13, 2023
1 parent c7aa98f commit 83f11c2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

### Unreleased

* Introduce `:pay` load hook - @excid3
Rails no longer allows autoloading during the initialization process. The on_load hook allows you to run code after autoloading, so we can register webhook listeners once autoloading is available.

To use this, wrap your webhook subscribe calls in the `on_load(:pay)` hook:

```ruby
ActiveSupport.on_load(:pay) do
Pay::Webhooks.delegator.subscribe "stripe.charge.succeded", ChargeSucceeded
end
```

### 6.3.4

* Import Stimulus.js from dist folder on unpkg
Expand Down
4 changes: 3 additions & 1 deletion docs/7_webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ class MyChargeSucceededProcessor
end
end

Pay::Webhooks.delegator.subscribe "stripe.charge.succeeded", MyChargeSucceededProcessor.new
ActiveSupport.on_load(:pay) do
Pay::Webhooks.delegator.subscribe "stripe.charge.succeeded", MyChargeSucceededProcessor.new
end
```

### Unsubscribing from a webhook listener
Expand Down
4 changes: 4 additions & 0 deletions lib/pay/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Engine < ::Rails::Engine
end
end

config.after_initialize do
ActiveSupport.run_load_hooks(:pay, Pay)
end

# Add webhook subscribers before app initializers define extras
# This keeps the processing in order so that changes have happened before user-defined webhook processors
config.before_initialize do
Expand Down
5 changes: 5 additions & 0 deletions test/dummy/app/webhooks/charge_succeeded.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChargeSucceeded
def call(event)
Rails.logger.debug event
end
end
4 changes: 4 additions & 0 deletions test/dummy/config/initializers/pay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
config.application_name = "My App"
config.support_email = "support@example.org"
end

ActiveSupport.on_load(:pay) do
Pay::Webhooks.delegator.subscribe "stripe.charge.succeeded", ChargeSucceeded.new
end

0 comments on commit 83f11c2

Please sign in to comment.