Skip to content

Commit

Permalink
Only use the newer Solidus event system
Browse files Browse the repository at this point in the history
The old one has been removed on v4.0.

We'll need a major release after this change is merged.

See solidusio/solidus#5024
  • Loading branch information
waiting-for-dev committed May 3, 2023
1 parent d3a9d98 commit c05810b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 47 deletions.
17 changes: 8 additions & 9 deletions app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# frozen_string_literal: true

module SolidusSubscriptions
module ChurnBusterSubscriber
include ::Spree::Event::Subscriber
include ::SolidusSupport::LegacyEventCompat::Subscriber

event_action :report_subscription_cancellation, event_name: 'solidus_subscriptions.subscription_canceled'
event_action :report_subscription_ending, event_name: 'solidus_subscriptions.subscription_ended'
event_action :report_payment_success, event_name: 'solidus_subscriptions.installment_succeeded'
event_action :report_payment_failure, event_name: 'solidus_subscriptions.installment_failed_payment'
event_action :report_payment_method_change, event_name: 'solidus_subscriptions.subscription_payment_method_changed'
class ChurnBusterSubscriber
include Omnes::Subscriber

handle :"solidus_subscriptions.subscription_canceled", with: :report_subscription_cancellation
handle :"solidus_subscriptions.subscription_ended", with: :report_subscription_ending
handle :"solidus_subscriptions.installment_succeeded", with: :report_payment_success
handle :"solidus_subscriptions.installment_failed_payment", with: :report_payment_failure
handle :"solidus_subscriptions.subscription_payment_method_changed", with: :report_payment_method_change

def report_subscription_cancellation(event)
churn_buster&.report_subscription_cancellation(event.payload.fetch(:subscription))
Expand Down
19 changes: 9 additions & 10 deletions app/subscribers/solidus_subscriptions/event_storage_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# frozen_string_literal: true

module SolidusSubscriptions
module EventStorageSubscriber
include ::Spree::Event::Subscriber
include ::SolidusSupport::LegacyEventCompat::Subscriber
class EventStorageSubscriber
include Omnes::Subscriber

event_action :track_subscription_created, event_name: 'solidus_subscriptions.subscription_created'
event_action :track_subscription_activated, event_name: 'solidus_subscriptions.subscription_activated'
event_action :track_subscription_canceled, event_name: 'solidus_subscriptions.subscription_canceled'
event_action :track_subscription_ended, event_name: 'solidus_subscriptions.subscription_ended'
event_action :track_subscription_shipping_address_changed, event_name: 'solidus_subscriptions.subscription_shipping_address_changed'
event_action :track_subscription_billing_address_changed, event_name: 'solidus_subscriptions.subscription_billing_address_changed'
event_action :track_subscription_frequency_changed, event_name: 'solidus_subscriptions.subscription_frequency_changed'
handle :"solidus_subscriptions.subscription_created", with: :track_subscription_created
handle :"solidus_subscriptions.subscription_activated", with: :track_subscription_activated
handle :"solidus_subscriptions.subscription_canceled", with: :track_subscription_canceled
handle :"solidus_subscriptions.subscription_ended", with: :track_subscription_ended
handle :"solidus_subscriptions.subscription_shipping_address_changed", with: :track_subscription_shipping_address_changed
handle :"solidus_subscriptions.subscription_billing_address_changed", with: :track_subscription_billing_address_changed
handle :"solidus_subscriptions.subscription_frequency_changed", with: :track_subscription_frequency_changed

def track_subscription_created(event)
event.payload.fetch(:subscription).events.create!(
Expand Down
9 changes: 3 additions & 6 deletions app/subscribers/solidus_subscriptions/order_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# frozen_string_literal: true

module SolidusSubscriptions
module OrderSubscriber
include ::Spree::Event::Subscriber
include ::SolidusSupport::LegacyEventCompat::Subscriber
class OrderSubscriber
include Omnes::Subscriber

event_action :create_subscription, event_name: 'order_finalized'

private
handle :order_finalized, with: :create_subscription

def create_subscription(event)
SolidusSubscriptions::CreateSubscriptionJob.perform_later(event.payload[:order])
Expand Down
42 changes: 20 additions & 22 deletions lib/solidus_subscriptions/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,27 @@ class Engine < Rails::Engine
end

initializer 'solidus_subscriptions.pub_sub' do |app|
unless SolidusSupport::LegacyEventCompat.using_legacy?
app.reloader.to_prepare do
%i[
subscription_created
subscription_activated
subscription_canceled
subscription_ended
subscription_skipped
subscription_resumed
subscription_paused
subscription_frequency_changed
subscription_shipping_address_changed
subscription_billing_address_changed
installment_succeeded
installment_failed_payment
subscription_payment_method_changed
].each do |event_name|
::Spree::Bus.register(:"solidus_subscriptions.#{event_name}")
end
SolidusSubscriptions::ChurnBusterSubscriber.omnes_subscriber.subscribe_to(::Spree::Bus)
SolidusSubscriptions::EventStorageSubscriber.omnes_subscriber.subscribe_to(::Spree::Bus)
SolidusSubscriptions::OrderSubscriber.omnes_subscriber.subscribe_to(::Spree::Bus)
app.reloader.to_prepare do
%i[
subscription_created
subscription_activated
subscription_canceled
subscription_ended
subscription_skipped
subscription_resumed
subscription_paused
subscription_frequency_changed
subscription_shipping_address_changed
subscription_billing_address_changed
installment_succeeded
installment_failed_payment
subscription_payment_method_changed
].each do |event_name|
::Spree::Bus.register(:"solidus_subscriptions.#{event_name}")
end
SolidusSubscriptions::ChurnBusterSubscriber.new.subscribe_to(::Spree::Bus)
SolidusSubscriptions::EventStorageSubscriber.new.subscribe_to(::Spree::Bus)
SolidusSubscriptions::OrderSubscriber.new.subscribe_to(::Spree::Bus)
end
end
end
Expand Down

0 comments on commit c05810b

Please sign in to comment.