Sha256: 279066a39ec21b34fd213e93a5fa2a6afbfbc9fc04d82ec02ad31c28d299ddb8

Contents?: true

Size: 1.69 KB

Versions: 6

Compression:

Stored size: 1.69 KB

Contents

require 'active_support/concern'
require 'new_relic/agent'
require 'roo_on_rails/routemaster/publishers'

module RooOnRails
  module Routemaster
    module LifecycleEvents
      extend ActiveSupport::Concern

      ACTIVE_RECORD_TO_ROUTEMASTER_EVENT_MAP = {
        create: :created,
        update: :updated,
        destroy: :deleted,
        noop: :noop
      }.freeze
      private_constant :ACTIVE_RECORD_TO_ROUTEMASTER_EVENT_MAP

      def publish_lifecycle_event(event)
        publish_event(event, force_publish: false)
      end

      def publish_lifecycle_event!(event)
        publish_event(event, force_publish: true)
      end

      private

      def publish_event(event, force_publish:)
        publishers = Routemaster::Publishers.for(self, routemaster_event_type(event))
        publishers.each do |publisher|
          begin
            publisher.publish!(force_publish: force_publish)
          rescue => e
            NewRelic::Agent.notice_error(e)
          end
        end
      end

      def routemaster_event_type(event)
        ACTIVE_RECORD_TO_ROUTEMASTER_EVENT_MAP[event].tap do |type|
          raise "invalid lifecycle event '#{event}'" unless type
        end
      end

      %i(create update destroy noop).each do |event|
        define_method("publish_lifecycle_event_on_#{event}") do
          publish_lifecycle_event(event)
        end
      end

      module ClassMethods
        def publish_lifecycle_events(*events)
          events = events.any? ? events : %i(create update destroy)
          events.each do |event|
            after_commit(
              :"publish_lifecycle_event_on_#{event}",
              on: event
            )
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
roo_on_rails-1.22.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.21.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.20.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.19.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.18.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.17.0 lib/roo_on_rails/routemaster/lifecycle_events.rb