lib/stance/event.rb in stance-0.8.1 vs lib/stance/event.rb in stance-0.8.2
- old
+ new
@@ -6,17 +6,28 @@
define_callbacks :create
attr_reader :record, :options, :name
class << self
+ attr_accessor :callback_methods
+
def before_create(*methods, &block)
set_callback :create, :before, *methods, &block
end
def after_create(*methods, &block)
set_callback :create, :after, *methods, &block
end
+
+ def method_added(method_name)
+ super
+
+ return if self == Stance::Event
+
+ self.callback_methods ||= []
+ self.callback_methods << method_name
+ end
end
def initialize(name, subject, metadata, options)
@subject = subject
@name = name
@@ -42,13 +53,13 @@
Stance::EventRecord.transaction do
run_callbacks :create do
# Call each public method of the Event class if a custom class.
if self.class.name != 'Stance::Event'
- Rails.logger.info "Event: #{full_name}"
+ Rails.logger.debug "Event: #{full_name}"
- callback_methods.each { |method| send method }
+ self.class.callback_methods&.each { |method| send method }
end
record.save if @options[:record]
end
end
@@ -63,13 +74,9 @@
def subject
try(:record)&.subject || @subject
end
private
-
- def callback_methods
- public_methods(false) - Stance::Event.instance_methods(false)
- end
# Event is a singleton and already exists.
def singleton_exists?
options[:singleton] && subject.events.active.exists?(name: name)
end