Module: ActivityTracker::ActMethods

Defined in:
lib/activity_tracker/activity_tracker.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_activity(actor, options = {}) ⇒ Object

Arguments:

<tt>:actor</tt> - the user model that owns this object. In most cases this will be :user. Required.
<tt>:options</tt> - hash of options.

Options:

<tt>:if</tt> - a Proc that determines if the activity should be tracked.

Examples:

acts_as_activity :user
acts_as_activity :author
acts_as_activity :user, :if => Proc.new{|record| record.post.length > 100 } - will only track the activity if the length of the post is more than 100

21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/activity_tracker/activity_tracker.rb', line 21

def acts_as_activity(actor, options = {})
  unless included_modules.include? InstanceMethods
    after_create do |record|
      unless options[:if].kind_of?(Proc) and not options[:if].call(record)
        record.create_activity_from_self 
      end
    end

    has_many :activities, :as => :item, :dependent => :destroy
    class_attribute :activity_options
    include InstanceMethods
  end      
  self.activity_options = {:actor => actor}
end

#tracks_unlinked_activities(actions = []) ⇒ Object

This adds a helper method to the model which makes it easy to track actions that can't be associated with an object in the database. Options:

<tt>:actions</tt> - An array of actions that are accepted to be tracked.

Examples:

tracks_unlinked_activities [:logged_in, :invited_friends] - class.track_activity(:logged_in)

43
44
45
46
47
48
49
50
# File 'lib/activity_tracker/activity_tracker.rb', line 43

def tracks_unlinked_activities(actions = [])
  unless included_modules.include? InstanceMethods
    class_attribute :activity_options
    include InstanceMethods
  end
  self.activity_options = {:actions => actions}    
  after_destroy { |record| Activity.destroy_all(:user_id => record.id) }
end