Module: MongoidActivityTracker::Tracker::ClassMethods

Defined in:
lib/mongoid_activity_tracker/tracker.rb

Instance Method Summary collapse

Instance Method Details

#track(actor, action, options = {}) ⇒ Object


9
10
11
# File 'lib/mongoid_activity_tracker/tracker.rb', line 9

def track(actor, action, options = {})
  create({ action: action, actor: actor }.merge(options))
end

#track!(actor, action, options = {}) ⇒ Object


13
14
15
# File 'lib/mongoid_activity_tracker/tracker.rb', line 13

def track!(actor, action, options = {})
  create!({ action: action, actor: actor }.merge(options))
end

#tracks(relation_name, cache_methods: %i(to_s))) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongoid_activity_tracker/tracker.rb', line 19

def tracks(relation_name, cache_methods: %i(to_s))
  field_name = [relation_name, 'cache'].join('_')
  accessor_name = [relation_name, 'cache_methods'].join('_')
  instance_variable_name = ['@', accessor_name].join

  field field_name, type: Hash, default: {}

  belongs_to relation_name, polymorphic: true, optional: true

  attr_accessor accessor_name

  define_method accessor_name do
    instance_variable_set(instance_variable_name, cache_methods) unless instance_variable_get(instance_variable_name)
    instance_variable_get(instance_variable_name)
  end

  define_method [relation_name, 'cache_object'].join('_') do
    OpenStruct.new(send(field_name))
  end

  before_save -> { set_cache(relation_name) }
end