Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline event emission and tracking #154

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 5 additions & 27 deletions app/models/solidus_subscriptions/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class LineItem < ApplicationRecord
validates :quantity, numericality: { greater_than: 0 }
validates :interval_length, numericality: { greater_than: 0 }, unless: -> { subscription }

after_create :track_creation_event
after_update :track_update_event
after_destroy :track_destroy_event
after_create :emit_event_for_repopulation
after_update :emit_event_for_repopulation
after_destroy :emit_event_for_repopulation

def as_json(**options)
options[:methods] ||= [:dummy_line_item]
Expand Down Expand Up @@ -72,32 +72,10 @@ def dummy_order
order.freeze
end

def as_json_for_event
as_json.with_indifferent_access.except(
:dummy_line_item,
:interval_units,
:interval_length,
:end_date,
:spree_line_item_id,
)
end

def track_creation_event
return unless subscription

subscription.events.create!(event_type: 'line_item_created', details: as_json_for_event)
end

def track_update_event
return unless subscription

subscription.events.create!(event_type: 'line_item_updated', details: as_json_for_event)
end

def track_destroy_event
def emit_event_for_repopulation
return unless subscription

subscription.events.create!(event_type: 'line_item_destroyed', details: as_json_for_event)
::Spree::Event.fire('solidus_subscriptions.subscription_repopulated', subscription: subscription)
end
end
end
22 changes: 12 additions & 10 deletions app/models/solidus_subscriptions/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Subscription < ApplicationRecord
accepts_nested_attributes_for :line_items, allow_destroy: true, reject_if: ->(p) { p[:quantity].blank? }

before_validation :set_payment_method
after_create :track_creation_event
after_create :emit_event_for_creation
before_update :update_actionable_date_if_interval_changed

# Find all subscriptions that are "actionable"; that is, ones that have an
Expand Down Expand Up @@ -113,7 +113,7 @@ def self.processing_states
end

after_transition to: :active, do: :advance_actionable_date
after_transition do: :track_transition_event
after_transition do: :emit_event_for_transition
end

# This method determines if a subscription may be canceled. Canceled
Expand Down Expand Up @@ -274,23 +274,25 @@ def set_payment_method
end
end

def as_json_for_event
as_json
def emit_event_for_creation
::Spree::Event.fire(
'solidus_subscriptions.subscription_created',
subscription: self,
)
end

def track_creation_event
events.create!(event_type: 'subscription_created', details: as_json_for_event)
end

def track_transition_event
def emit_event_for_transition
event_type = {
active: 'subscription_activated',
canceled: 'subscription_canceled',
pending_cancellation: 'subscription_canceled',
inactive: 'subscription_ended',
}[state.to_sym]

events.create!(event_type: event_type, details: as_json_for_event)
::Spree::Event.fire(
"solidus_subscriptions.#{event_type}",
subscription: self,
)
end
end
end
13 changes: 0 additions & 13 deletions app/models/solidus_subscriptions/subscription_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,5 @@ class SubscriptionEvent < ApplicationRecord
after_initialize do
self.details ||= {}
end

after_create :emit_event

private

def emit_event
return unless defined?(::Spree::Event)

::Spree::Event.fire(
"solidus_subscriptions.#{event_type}",
details.deep_symbolize_keys.merge(subscription: subscription),
)
end
end
end
48 changes: 48 additions & 0 deletions app/subscribers/solidus_subscriptions/event_storage_subscriber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module SolidusSubscriptions
module EventStorageSubscriber
include ::Spree::Event::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_repopulated, event_name: 'solidus_subscriptions.subscription_repopulated'

def track_subscription_created(event)
event.payload.fetch(:subscription).events.create!(
event_type: 'subscription_created',
details: event.payload.fetch(:subscription).as_json,
)
end

def track_subscription_activated(event)
event.payload.fetch(:subscription).events.create!(
event_type: 'subscription_activated',
details: event.payload.fetch(:subscription).as_json,
)
end

def track_subscription_canceled(event)
event.payload.fetch(:subscription).events.create!(
event_type: 'subscription_canceled',
details: event.payload.fetch(:subscription).as_json,
)
end

def track_subscription_ended(event)
event.payload.fetch(:subscription).events.create!(
event_type: 'subscription_ended',
details: event.payload.fetch(:subscription).as_json,
)
end

def track_subscription_repopulated(event)
event.payload.fetch(:subscription).events.create!(
event_type: 'subscription_repopulated',
details: event.payload.fetch(:subscription).as_json,
)
end
end
end
5 changes: 5 additions & 0 deletions config/initializers/subscribers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

Spree.config do |config|
config.events.subscribers << 'SolidusSubscriptions::EventStorageSubscriber'
end
12 changes: 6 additions & 6 deletions spec/models/solidus_subscriptions/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,41 @@

describe '#save!' do
context 'when the line item is new' do
it 'tracks a line_item_created event' do
it 'tracks a subscription_repopulated event' do
line_item = build(:subscription_line_item, :with_subscription)

line_item.save!

expect(line_item.subscription.events.last).to have_attributes(
event_type: 'line_item_created',
event_type: 'subscription_repopulated',
details: a_hash_including('id' => line_item.id),
)
end
end

context 'when the line item is persisted' do
it 'tracks a line_item_updated event' do
it 'tracks a subscription_repopulated event' do
line_item = create(:subscription_line_item, :with_subscription)

line_item.quantity = 2
line_item.save!

expect(line_item.subscription.events.last).to have_attributes(
event_type: 'line_item_updated',
event_type: 'subscription_repopulated',
details: a_hash_including('id' => line_item.id),
)
end
end
end

describe '#destroy!' do
it 'tracks a line_item_destroyed event' do
it 'tracks a subscription_repopulated event' do
line_item = create(:subscription_line_item, :with_subscription)

line_item.destroy!

expect(line_item.subscription.events.last).to have_attributes(
event_type: 'line_item_destroyed',
event_type: 'subscription_repopulated',
details: a_hash_including('id' => line_item.id),
)
end
Expand Down
15 changes: 0 additions & 15 deletions spec/models/solidus_subscriptions/subscription_event_spec.rb

This file was deleted.