Sha256: 9ecd251dfa4018a6b9d05e6eb1ef65c11593a938576461b4e475ed48c49fbe59

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module ActivityEngine
  class ActivitySweeper < ActionController::Caching::Sweeper
    class << self
      def observe(*models)
        models.flatten!
        models.collect! { |model| model.respond_to?(:to_sym) ? model.to_s.camelize.constantize : model }
        models.each {|model|
          model.send(:include, ActiveModel::Observing) unless model.included_modules.include?(ActiveModel::Observing)
        }
        models_to_observe = (observed_classes + models).uniq
        singleton_class.redefine_method(:observed_classes) { models_to_observe }
      end
    end

    attr_accessor :records_processed

    def after(controller)
      self.controller = controller
      self.records_processed = []
      callback(:after)
      self.records_processed = []
      self.controller = nil
      true
    end

    def after_save(record)
      self.records_processed ||= []
      if ! self.records_processed.include?(record)
        self.records_processed << record
        ActivityEngine::Activity.new.tap {|activity|
          activity.subject = record
          activity.user = current_user
          activity.activity_type = "#{controller_name}##{action_name}"
        }.save!
      end
      true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activity_engine-0.0.7 lib/activity_engine/activity_sweeper.rb
activity_engine-0.0.6 lib/activity_engine/activity_sweeper.rb