Skip to content

Commit

Permalink
Fixed event triggering record callback enable/disable (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
vovimayhem authored Aug 21, 2019
1 parent 729915d commit 828a8fe
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module EventTriggeringRecord

included do
mattr_reader :delayed_icalia_event_publishing, default: true
mattr_reader :icalia_event_publishing_enabled, default: true

after_commit :publish_create_to_icalia_events, on: :create
after_commit :publish_update_to_icalia_events, on: :update
Expand All @@ -22,6 +23,7 @@ module EventTriggeringRecord
:icalia_event_publisher_class,
:icalia_events_publisher_method,
:delayed_icalia_event_publishing?,
:icalia_event_publishing_enabled?,
to: :class
end

Expand All @@ -44,29 +46,28 @@ def publish_to_icalia_events_later!
end

def icalia_event_publishing_enabled?
:publish_to_icalia_events.in? self
._commit_callbacks
.select { |callback| callback.kind.eql?(:after) }
.map(&:filter)
icalia_event_publishing_enabled == true
end

def icalia_event_publishing_disabled?
!icalia_event_publishing_enabled?
end

def disable_icalia_event_publishing
skip_callback(:commit, :after, :publish_to_icalia_events)
class_variable_set :@@icalia_event_publishing_enabled, false
end

def enable_icalia_event_publishing
set_callback(:commit, :after, :publish_to_icalia_events)
class_variable_set :@@icalia_event_publishing_enabled, true
end

def without_icalia_event_publishing(&_block)
disable_icalia_event_publishing if icalia_event_publishing_enabled?
result = yield self
enable_icalia_event_publishing if icalia_event_publishing_disabled?
result
ensure
enable_icalia_event_publishing if icalia_event_publishing_disabled?
end

def icalia_event_class_name
Expand All @@ -86,6 +87,7 @@ def icalia_event_publisher_class
private

def publish_to_icalia_events(action)
return unless icalia_event_publishing_enabled?
publisher_class = icalia_event_publisher_class
return unless publisher_class.present?
publisher_class.send icalia_events_publisher_method, action.to_s, self
Expand Down

0 comments on commit 828a8fe

Please sign in to comment.