summaryrefslogtreecommitdiff
path: root/app/models/solidus_subscriptions/subscription_event.rb
blob: 93552fd67a5c704b7b03abbde2fe6e14f684ef81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module SolidusSubscriptions
  class SubscriptionEvent < ApplicationRecord
    belongs_to :subscription, class_name: 'SolidusSubscriptions::Subscription', inverse_of: :events

    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