Sha256: 3761dba4eda08961c65545c9cb34b210512b373b8ed18aa4ae2ceb386a3f9ac8

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

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)
        opts[:subject] = :self unless opts.has_key?(:subject)
        event_type_class = "#{event_type.to_s.camelcase}TimelineEvent"
        
        unless opts[:dependent] == :keep
          has_many event_type_class.underscore.pluralize, :as => :subject, :dependent => :destroy
        end

        method_name = :"fire_#{event_type}_after_#{opts[:on]}"
        define_method(method_name) do
          create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
            case opts[sym]
            when :self
              memo[sym] = self
            else
              memo[sym] = send(opts[sym]) if opts[sym]
            end
            memo
          end
          create_options[:event_type] = event_type.to_s
          t = TimelineEvent.new(create_options)
          t.type = event_type_class
          t.save!
        end

        send(:"after_#{opts[:on]}", method_name, :if => opts[:if])
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
seamusabshere-timeline_fu-0.1.3 lib/timeline_fu/fires.rb