lib/reactor/models/concerns/publishable.rb in reactor-0.6.2 vs lib/reactor/models/concerns/publishable.rb in reactor-0.7.0
- old
+ new
@@ -1,10 +1,11 @@
module Reactor::Publishable
extend ActiveSupport::Concern
included do
- after_commit :schedule_events, if: :persisted?
+ after_commit :schedule_events, if: :persisted?, on: :create
+ after_commit :schedule_conditional_events, if: :persisted?, on: [:create, :update]
after_commit :reschedule_events, if: :persisted?, on: :update
end
def publish(name, data = {})
Reactor::Event.publish(name, data.merge(actor: self) )
@@ -22,24 +23,12 @@
private
def schedule_events
self.class.events.each do |name, data|
- event = data.merge(
- actor: ( data[:actor] ? send(data[:actor]) : self ),
- target: ( data[:target] ? self : nil),
- at: ( data[:at] ? send(data[:at]) : nil)
- ).except(:watch, :if)
- need_to_fire = case (ifarg = data[:if])
- when Proc
- instance_exec(&ifarg)
- when Symbol
- send(ifarg)
- else
- transaction_include_action?(:create)
- end
- Reactor::Event.publish name, event if need_to_fire
+ event = event_data_for_signature(data)
+ Reactor::Event.publish name, event
end
end
def reschedule_events
self.class.events.each do |name, data|
@@ -51,8 +40,29 @@
actor: ( data[:actor] ? send(data[:actor]) : self ),
target: ( data[:target] ? self : nil),
was: previous_changes[data[:at]].try(:first) || send("#{data[:at]}_was"))
end
end
+ end
+
+ def schedule_conditional_events
+ self.class.events.select { |k,v| v.has_key?(:if) }.each do |name, data|
+ event = event_data_for_signature(data)
+ need_to_fire = case (ifarg = data[:if])
+ when Proc
+ instance_exec(&ifarg)
+ when Symbol
+ send(ifarg)
+ end
+ Reactor::Event.publish name, event if need_to_fire
+ end
+ end
+
+ def event_data_for_signature(signature)
+ signature.merge(
+ actor: (signature[:actor] ? send(signature[:actor]) : self),
+ target: (signature[:target] ? self : nil),
+ at: (signature[:at] ? send(signature[:at]) : nil)
+ ).except(:watch, :if)
end
end