Sha256: 8e0b40112ac8a3e6673598c271de94ba9c5429cbc836d90f9e68d13e1797e360

Contents?: true

Size: 1.45 KB

Versions: 12

Compression:

Stored size: 1.45 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)
        publishers = Routemaster::Publishers.for(self, routemaster_event_type(event))
        publishers.each do |publisher|
          begin
            publisher.publish!
          rescue => e
            NewRelic::Agent.notice_error(e)
          end
        end
      end

      private

      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

12 entries across 12 versions & 1 rubygems

Version Path
roo_on_rails-1.16.2 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.16.1 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.16.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.15.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.14.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.13.1 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.13.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.12.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.11.1 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.11.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.10.0 lib/roo_on_rails/routemaster/lifecycle_events.rb
roo_on_rails-1.9.0 lib/roo_on_rails/routemaster/lifecycle_events.rb