Module: Stenotype::Frameworks::Rails::ActionControllerExtension::ClassMethods

Defined in:
lib/stenotype/frameworks/rails/action_controller.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#track_all_viewsObject

Note:

This action will only define a symmetric difference of

the tracked actions and the ones not tracked yet.

Examples:

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

See Also:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object

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.

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

Examples:

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

Parameters:

  • actions (Array<Symbol>)

    a list of tracked controller actions



68
69
70
71
72
73
74
75
76
77
78
79
80
# 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