class RailsBase::Admin::ActionHelper

Constants

ACTIONS_KEY
CONTROLLER_ACTIONS_KEY
DEFAULT_ALLOWED_KLASSES

Attributes

action[RW]
controller[RW]
default[RW]
proc[RW]
title[RW]

Public Class Methods

actions() click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 47
def actions
  @actions
end
add(instance) click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 20
def add(instance)
  @actions ||= {}
  @all_actions ||= []
  @default_actions ||= []

  @all_actions << [instance]
  @default_actions << [instance]

  controller = instance.controller
  action = instance.action
  if controller.nil?
    @actions[ACTIONS_KEY] ||= []
    @actions[ACTIONS_KEY] << instance
    return
  end

  @actions[controller.to_s] ||= {}

  if action
     @actions[controller.to_s][action.to_s] ||= []
     @actions[controller.to_s][action.to_s] <<  instance
  else
    @actions[controller.to_s][CONTROLLER_ACTIONS_KEY] ||= []
    @actions[controller.to_s][CONTROLLER_ACTIONS_KEY] << instance
  end
end
add_inherited_klasses(klass) click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 11
def add_inherited_klasses(klass)
  @allowed_klasses ||= []
  @allowed_klasses << klass
end
allowed_inherited_klasses() click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 7
def allowed_inherited_klasses
  DEFAULT_ALLOWED_KLASSES + (@allowed_klasses || [])
end
clear_inherited_klasses!() click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 16
def clear_inherited_klasses!
  @allowed_klasses = nil
end
new(controller: nil, action: nil, title: nil, default: false, proc: nil) click to toggle source

controller is the controller class inherited by RailsBase::ApplicationController action is the method name on the controller title should be the AdminAction.action if proc available,

> |session, admin_user, user, title, struct|

> RailsBase::AdminStruct has methods original_attribute and new_attribute

> Expected return

> { admin_user, user, action, original_attribute, new_attribute, change_to }

# File lib/rails_base/admin/action_helper.rb, line 71
def initialize(controller: nil, action: nil, title: nil, default: false, proc: nil)
  @controller = controller
  @action = action
  @title = title
  @proc = proc
  valid_controller!
  valid_action!
  valid_title!
end
reset!() click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 51
def reset!
  @actions = nil
  @all_actions = nil
  @default_actions.each { |instance| add(instance) }
end

Public Instance Methods

add!() click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 81
def add!
  self.class.add(self)
end
call(req:, params:, admin_user:, user:, struct: nil) click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 85
def call(req:, params:, admin_user:, user:, struct: nil)
  # byebug
  if proc
    action_params = proc.call(req, params, admin_user, user, title, struct)
    return if action_params.nil?

    AdminAction.action(action_params)
  else
    default_call(request: request, admin_user: admin_user, user: user, struct: struct)
  end
rescue StandardError => e
  Rails.logger.error(e.message)
  Rails.logger.error(e.backtrace)
  Rails.logger.error("Trapping Error from AdminActionHelper.")
end

Private Instance Methods

default_call(session:, admin_user:, user:, struct: nil) click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 103
def default_call(session:, admin_user:, user:, struct: nil)
  action_params = {
    admin_user: admin_user,
    user: user,
    action: title,
    change_from: struct&.original_attribute,
    change_to: struct&.new_attribute,
  }
  AdminAction.action(action_params)
end
valid_action!() click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 120
def valid_action!
  return if action.nil?
  return if controller.instance_methods.map(&:to_s).include?(action.to_s)

  puts controller.instance_methods
  raise InvalidActionError, "#{controller} does not respond to #{action}"
end
valid_controller!() click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 114
def valid_controller!
  return if self.class.allowed_inherited_klasses.include?(controller.superclass)

  raise InvalidControllerError, "@controller does not inherit #{self.class.allowed_inherited_klasses}"
end
valid_title!() click to toggle source
# File lib/rails_base/admin/action_helper.rb, line 128
def valid_title!
  return unless title.nil? && proc.nil?

  raise InvalidTitleError, "Missing title and proc. 1 or the other needs to be present"
end