module Recorder::Rails::ControllerConcern

Extensions to rails controllers. Provides convenient ways to pass certain information to the model layer, with `recorder_meta`.

Public Class Methods

included(base) click to toggle source
# File lib/recorder/rails/controller_concern.rb, line 8
def self.included(base)
  base.before_action :set_recorder_info
  base.before_action :set_recorder_meta
end

Protected Instance Methods

recorder_info() click to toggle source
# File lib/recorder/rails/controller_concern.rb, line 28
def recorder_info
  {
    user_id: recorder_user_id,
    ip: request.remote_ip,
    action_date: Date.current,
    meta: recorder_meta
  }
end
recorder_meta() click to toggle source

Override this method in your controller to return a hash of any information you need.

# File lib/recorder/rails/controller_concern.rb, line 39
def recorder_meta
  nil
end
recorder_user_id() click to toggle source

Returns the user who is responsible for any changes that occur. By default this calls `current_user` and returns the result.

Override this method in your controller to call a different method, e.g. `current_person`, or anything you like.

# File lib/recorder/rails/controller_concern.rb, line 20
def recorder_user_id
  return unless defined?(current_user)

  current_user.try!(:id)
rescue NoMethodError
  current_user
end
set_recorder_info() click to toggle source

Tells Recorder any information from the controller you want to store alongside any changes that occur.

# File lib/recorder/rails/controller_concern.rb, line 45
def set_recorder_info
  ::Recorder.info = recorder_info
end
set_recorder_meta() click to toggle source
# File lib/recorder/rails/controller_concern.rb, line 49
def set_recorder_meta
  ::Recorder.meta = recorder_meta
end