Class: ActivityEngine::ActivitySweeper

Inherits:
ActionController::Caching::Sweeper
  • Object
show all
Defined in:
lib/activity_engine/activity_sweeper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#records_processedObject

Returns the value of attribute records_processed.



15
16
17
# File 'lib/activity_engine/activity_sweeper.rb', line 15

def records_processed
  @records_processed
end

Class Method Details

.observe(*models) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/activity_engine/activity_sweeper.rb', line 4

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

Instance Method Details

#after(controller) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/activity_engine/activity_sweeper.rb', line 17

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

#after_save(record) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/activity_engine/activity_sweeper.rb', line 26

def after_save(record)
  self.records_processed ||= []
  # Because the sweeper is now attached as an observer I don't want every
  # save of the record to trigger this behavior.
  return true unless controller
  return true if self.records_processed.include?(record)

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