Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 3c01e849b5
Fetching contributors…

Cannot retrieve contributors at this time

46 lines (40 sloc) 1.442 kB
module TimelineFu
module Fires
def self.included(klass)
klass.send(:extend, ClassMethods)
end
module ClassMethods
def fires(event_type, opts)
raise ArgumentError, "Argument :on is mandatory" unless opts.has_key?(:on)
# Array provided, set multiple callbacks
if opts[:on].kind_of?(Array)
opts[:on].each { |on| fires(event_type, opts.merge({:on => on})) }
return
end
opts[:subject] = :self unless opts.has_key?(:subject)
method_name = :"fire_#{event_type}_after_#{opts[:on]}"
define_method(method_name) do
create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
if opts[sym]
if opts[sym].respond_to?(:call)
memo[sym] = opts[sym].call(self)
elsif opts[sym] == :self
memo[sym] = self
else
memo[sym] = send(opts[sym])
end
end
memo
end
create_options[:event_type] = event_type.to_s
if create_options[:actor].kind_of?(Array)
create_options[:actor].each { |actor| TimelineEvent.create!(create_options.merge({:actor => actor})) }
else
TimelineEvent.create!(create_options)
end
end
send(:"after_#{opts[:on]}", method_name, :if => opts[:if])
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.