Class: SimpleActivity::ActivityProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_activity/services/activity_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller, target = nil) ⇒ Object

This class is for internal usage. No need to initialize this manually, instead use controller methods provided.

When being used as automatical way in controller, e.g. as after_filter, supply the controller

ActivityProcessor.new(self)

If cache options needs to be attached, ensure the second argument

ActivityProcessor.new(self, nil, foo: 'bar')

When being used manually, normally the target would be provided

ActivityProcessor.new(self, @article, foo: 'bar')

Parameters:

  • controller (Object)

    The controller object, often self in controller

  • target (Object) (defaults to: nil)

    The target instance. If nil, it will be found based on controller.

    When supplied manually, target should be free of error, aka, after controller actiton(create, update etc) success

  • reference (Hash)

    When required, the second arg “target” should be there, either object or nil



40
41
42
43
44
45
46
47
48
# File 'lib/simple_activity/services/activity_processor.rb', line 40

def initialize(controller, target=nil)
  @controller   = controller
  @target       = target ? target : get_target
  @target_type  = @target.class.to_s
  @actor        = get_actor
  @actor_type   = @actor.class.to_s
  @action_key   = @controller.action_name
  @cache        = get_cache
end

Instance Method Details

#saveObject

Return nil at this level, but not at #initialize. The reason is not to throw error on ‘nil.create_activity`, for validation error of target is common case. SimpleActivity should let it pass.



53
54
55
56
57
58
59
60
61
# File 'lib/simple_activity/services/activity_processor.rb', line 53

def save
  if validate_attrs
    Activity.create(activity_attrs).tap do |activity|
      Callbacks.run_callbacks(activity)
    end
  else
    warning
  end
end

#warningObject



63
64
65
66
# File 'lib/simple_activity/services/activity_processor.rb', line 63

def warning
  Rails.logger.warn "SimpleActivity: failed to create activity"
  nil
end