module Stenotype::Frameworks::Rails::ActionControllerExtension::ClassMethods

Class methods to be injected into classes inherited from [ActionController::Base]

Public Instance Methods

track_all_views() click to toggle source

@note This action will only define a symmetric difference of the tracked actions and the ones not tracked yet. @see track_view

@example Emitting an event before all actions in controller

class UsersController < ApplicationController
  track_all_views # Emits an event before all actions in a controller

  def index
    # do something
  end

  def show
    # do something
  end

  def create
    # do something
  end
end
# File lib/stenotype/frameworks/rails/action_controller.rb, line 104
def track_all_views
  actions = action_methods

  # A symmetric difference of two sets.
  # This prevents accidental duplicating of events
  delta = ((_tracked_actions - actions) | (actions - _tracked_actions))

  return if delta.empty?

  before_action only: delta.to_a do
    _record_freshly_event("view")
  end

  # merge is a mutating op
  _tracked_actions.merge(delta)
end
track_view(*actions) click to toggle source

Adds a before_action to each action from the passed list. A before action is emitting a {Stenotype::Event}. Please note that in case track_view is used several times, it will keep track of the actions which emit events.

@note Each time a new track_view is called it will find a symmetric difference

of two sets: set of already tracked actions and a set passed to `track_view`.

@example Tracking multiple actions with track_view

Stenotype.configure do |config|
  config.enable_action_controller_extension = true
  config.enable_active_job_extension = true
end

class MyController < ActionController::Base
  track_view :index, :show # Emits an event upon calling index and show actions,
                           # but does not trigger an event on create

  def index
    # do_something
  end

  def show
    # do something
  end

  # Not covered by track_view
  def update
    # do something
  end
end

@param actions {Array<Symbol>} a list of tracked controller actions

# File lib/stenotype/frameworks/rails/action_controller.rb, line 68
def track_view(*actions)
  # A symmetric difference of two sets.
  # This prevents accidental duplicating of events
  delta = (_tracked_actions - Set[*actions]) | (Set[*actions] - _tracked_actions)

  return if delta.empty?

  before_action only: delta do
    _record_freshly_event("view")
  end

  _tracked_actions.merge(delta)
end

Private Instance Methods

_tracked_actions() click to toggle source

@return {Set<Symbol>} a set of tracked actions

# File lib/stenotype/frameworks/rails/action_controller.rb, line 126
def _tracked_actions
  @_tracked_actions ||= Set.new
end