Module: ActivityStream::ActiveRecord::ClassMethods

Defined in:
lib/activity_stream.rb

Instance Method Summary collapse

Instance Method Details

#activity_triggersObject



32
33
34
# File 'lib/activity_stream.rb', line 32

def activity_triggers
  read_inheritable_attribute(:activity_triggers) || {}
end

#acts_as_actorObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/activity_stream.rb', line 78

def acts_as_actor
  class_eval do
    def log_activity(options = {}, &block)
      raise ArgumentError unless block_given?
      
      context = ActivityStream::Context.new options.merge(:actor => self)
      context.call(&block)
    end
    
    def log_activity_indirectly_for(record, options = {}, &block)
      log_activity options.merge(:indirect_object => record), &block
    end
  end

  has_many :activities, :as => :actor
end

#log_activities(*args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/activity_stream.rb', line 36

def log_activities(*args, &block)
  args = [:create, :update, :destroy] if args.length == 0

  activity_triggers = self.activity_triggers
  callback = block_given?? block : true
  args.each { |verb| activity_triggers["after_#{verb}".intern] = callback }
  write_inheritable_attribute :activity_triggers, activity_triggers
  
  unless defined? @_activity_observed
    has_many :activities, :as => :object do
      def all(options = {})
        klass = proxy_owner.klass
        Scope.new scope_for_reflection(:find => {:conditions => ['object_type = ? OR indirect_object_type = ?', klass.name, klass.name]}), :find => options
      end
      
      def indirectly(options = {})
        klass = proxy_owner.klass
        Scope.new scope_for_reflection(:find => {:conditions => {:indirect_object_type => klass.name}}), :find => options
      end
      
      def directly(options = {})
        klass = proxy_owner.klass
        Scope.new scope_for_reflection(:find => {:conditions => {:object_type => klass.name}}), :find => options
      end
      
    private
      def scope_for_reflection(options)
        Scope.new proxy_reflection.klass, options
      end
    end
    
    add_observer ActivityObserver.instance
    # to ensure we don't add the observer multiple times
    @_activity_observed = true
  end
end

#skip_log_activity(*args) ⇒ Object



73
74
75
76
# File 'lib/activity_stream.rb', line 73

def skip_log_activity(*args)
  args = args.map(&:to_sym)
  write_inheritable_attribute :activity_triggers, activity_triggers.reject { |k, v| args.include?("after_#{k}".intern) }
end