Method: PublicActivity::Common#create_activity

Defined in:
lib/public_activity/common.rb

#create_activity(action, options = {}) ⇒ Model? #create_activity(options = {}) ⇒ Model?

Directly creates activity record in the database, based on supplied options.

It’s meant for creating custom activities while preserving all configuration defined before. If you fire up the simplest of options:

current_user.create_activity(:avatar_changed)

It will still gather data from any procs or symbols you passed as params to Tracked::ClassMethods#tracked. It will ask the hooks you defined whether to really save this activity.

But you can also overwrite instance and global settings with your options:

@article.activity :owner => proc {|controller| controller.current_user }
@article.create_activity(:commented_on, :owner => @user)

And it’s smart! It won’t execute your proc, since you’ve chosen to overwrite instance parameter :owner with @user.

:key

The key will be generated from either:

  • the first parameter you pass that is not a hash (action)

  • the :action option in the options hash (action)

  • the :key option in the options hash (it has to be a full key, including model name)

When you pass an action (first two options above), they will be added to parameterized model name:

Given Article model and instance: @article,

@article.create_activity :commented_on
@article.activities.last.key # => "article.commented_on"

For other parameters, see Tracked#activity, and “Instance options” accessors at Tracked, information on hooks is available at Tracked::ClassMethods#tracked.

Overloads:

  • #create_activity(action, options = {}) ⇒ Model?

    Parameters:

    • action (Symbol, String)

      Name of the action

    • options (Hash) (defaults to: {})

      Options with quality higher than instance options set in Tracked#activity

    Options Hash (options):

  • #create_activity(options = {}) ⇒ Model?

    Parameters:

    • options (Hash) (defaults to: {})

      Options with quality higher than instance options set in Tracked#activity

    Options Hash (options):

Returns:

  • (Model, nil)

    If created successfully, new activity

See Also:

Since:

  • 0.4.0



248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/public_activity/common.rb', line 248

def create_activity(*args)
  return unless public_activity_enabled?

  options = prepare_settings(*args)

  if call_hook_safe(options[:key].split('.').last)
    reset_activity_instance_options
    return PublicActivity::Adapter.create_activity(self, options)
  end

  nil
end