Module: PublicActivity::Common

Extended by:
ActiveSupport::Concern
Defined in:
lib/public_activity/common.rb

Overview

Common methods shared across the gem.

Defined Under Namespace

Modules: ClassMethods

Global options collapse

Instance Method Summary collapse

Instance Attribute Details

#activity_hooksHash<Symbol, Proc>

Hooks/functions that will be used to decide if the activity should get created.

The supported keys are:

  • :create
  • :update
  • :destroy

Returns:

  • (Hash<Symbol, Proc>)

# File 'lib/public_activity/common.rb', line 48


#activity_owner_globalModel

Global version of activity owner

Returns:

See Also:

  • #activity_owner

# File 'lib/public_activity/common.rb', line 33


#activity_parameters_globalHash<Symbol, Object>

Global version of activity parameters

Returns:

  • (Hash<Symbol, Object>)

See Also:

  • #activity_parameters

# File 'lib/public_activity/common.rb', line 43


#activity_recipient_globalModel

Global version of activity recipient

Returns:

See Also:

  • #activity_recipient

# File 'lib/public_activity/common.rb', line 38


Instance Method Details

#call_hook_safe(key) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls hook safely. If a hook for given action exists, calls it with model (self) and controller (if available, see StoreController)

Returns:

  • (Boolean)

    if hook exists, it's decision, if there's no hook, true

Since:

  • 0.4.0


113
114
115
116
117
118
119
120
121
# File 'lib/public_activity/common.rb', line 113

def call_hook_safe(key)
  hook = self.class.get_hook(key)
  if hook
    # provides hook with model and controller
    hook.call(self, PublicActivity.get_controller)
  else
    true
  end
end

#create_activity(action, options = {}) ⇒ PublicActivity::Activity? #create_activity(options = {}) ⇒ PublicActivity::Activity?

Note:

This method won't create the activity if hooks reject creation.

Note:

Options passed in to this method will take precedence over defaults configured in Tracked.

Records activity in the database, based on supplied options and configuration in Tracked.

If Tracked is used and configured for this model, create_activity will also gather data as defined in Tracked. If any parameters passed here conflict with options defined in Tracked, they get precedence over options defined in Tracked::ClassMethods#tracked.

Whether or not Tracked is used, you can provide objects, symbols and procs as values for all parameters of activity. See PublicActivity.resolve_value for available value types.

If Tracked is used and hooks are provided, they will be called upon to decide if this method should really record an activity. To discard defined hooks and create the activity unconditionally, use Activity directly.

Examples

current_user.create_activity(:avatar_changed)
@article.create_activity(action: :commented_on, :owner => current_user)
@post.create_activity(key: 'blog_post.published', parameters: {words_count: 50})

Activity 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 ( full key )

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

Example:

@article.create_activity :commented_on               #=> #<Activity key: 'article.commented_on' ...>
@article.create_activity action: :commented_on       #=> #<Activity key: 'article.commented_on' ...>
# note the prefix when passing in `key`
@article.create_activity key: 'article.commented_on' #=> #<Activity key: 'article.commented_on' ...>

Options

Besides :action and :key covered above, you can pass options such as :owner, :parameters, :recipient. In addition, if you've configured any custom fields, you can pass them in here too.

Example

@article.create_activity :commented_on, weather_outside: :sunny

Overloads:

  • #create_activity(action, options = {}) ⇒ PublicActivity::Activity?

    Parameters:

    • action (Symbol, String)

      Name of the action, will be prefixed

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

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

    Options Hash (options):

  • #create_activity(options = {}) ⇒ PublicActivity::Activity?

    Parameters:

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

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

    Options Hash (options):

    • :action (Symbol, String)

      Name of the action, will be prefixed

    • :key (String)

      Full key, won't be prefixed

    • :owner (Activist)

      Owner

    • :recipient (Activist)

      Recipient

    • :parameters (Hash)

Returns:

Since:

  • 0.4.0


194
195
196
197
198
199
200
201
202
203
# File 'lib/public_activity/common.rb', line 194

def create_activity(*args)
  return unless self.public_activity_enabled?
  options = prepare_settings(*args)

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

  nil
end

#prepare_custom_fields(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepares and resolves custom fields users can pass to tracked method


236
237
238
239
240
241
242
# File 'lib/public_activity/common.rb', line 236

def prepare_custom_fields(options)
  customs = self.class.activity_custom_fields_global.clone
  customs.merge!(options)
  customs.each do  |k, v|
    customs[k] = PublicActivity.resolve_value(self, v)
  end
end

#prepare_key(action, options = {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method to serialize class name into relevant key

Parameters:

  • action (Symbol | String)

    the name of the operation to be done on class

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

    to be used on key generation, defaults to {}

Returns:

  • (String)

    the resulted key


268
269
270
271
272
273
# File 'lib/public_activity/common.rb', line 268

def prepare_key(action, options = {})
  (
    options[:key] ||
    ((self.class.name.underscore.gsub('/', '_') + "." + action.to_s) if action)
  ).try(:to_s)
end

#prepare_parameters(parameters) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepares i18n parameters that will be serialized into the Activity#parameters column


247
248
249
250
251
252
# File 'lib/public_activity/common.rb', line 247

def prepare_parameters(parameters)
  params = {}
  params.merge!(self.class.activity_parameters_global)
    .merge!(parameters || {})
  params.each { |k, v| params[k] = PublicActivity.resolve_value(self, v) }
end

#prepare_relation(name, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepares relation to be saved to Activity. Can be :recipient or :owner


257
258
259
260
261
# File 'lib/public_activity/common.rb', line 257

def prepare_relation(name, options)
  PublicActivity.resolve_value(self,
    (options.has_key?(name) ? options[name] : self.class.send("activity_#{name}_global"))
  )
end

#prepare_settings(action, options = {}) ⇒ Hash #prepare_settings(options = {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepares settings used during creation of Activity record. parameters passed directly to tracked model have priority over settings specified in tracked() method

Overloads:

Returns:

  • (Hash)

    Settings with preserved options that were passed

Raises:

See Also:


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/public_activity/common.rb', line 216

def prepare_settings(*args)
  raw_options = args.extract_options!
  action      = [args.first, raw_options.delete(:action)].compact.first
  key         = prepare_key(action, raw_options)

  raise NoKeyProvided, "No key provided for #{self.class.name}" unless key

  prepare_custom_fields(raw_options.except(:parameters, :params)).merge(
    {
      key:        key,
      owner:      prepare_relation(:owner,     raw_options),
      recipient:  prepare_relation(:recipient, raw_options),
      parameters: prepare_parameters(raw_options.delete(:parameters)),
    }
  )
end

#public_activity_enabled?Boolean

Note:

This method gets overwritten in Deactivatable#public_activity_enabled?

Returns true if PublicActivity is enabled globally.

Returns:

  • (Boolean)

See Also:

  • PublicActivity::Common.{Deactivatable{Deactivatable#public_activity_enabled?}

Since:

  • 0.5.0


102
103
104
# File 'lib/public_activity/common.rb', line 102

def public_activity_enabled?
  PublicActivity.enabled?
end