Sha256: 1bb81e491ae57ae4c2820b76a423ceae0749c10f024751fc8de5c79c7921238d

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module ActsAsActiveAdminController
  extend ActiveSupport::Concern

  module ActionController
    def acts_as_active_admin_controller(key)
      @active_admin_resource_element_lookup_key = key
      include ::ActsAsActiveAdminController
    end
  end

  included do
    include ::ActiveAdmin::BaseController::Menu
    include ::ActiveAdmin::BaseController::Authorization

    helper ::ActiveAdmin::ViewHelpers
    helper_method :active_admin_config, :active_admin_namespace, :current_active_admin_user

    resource_key = @active_admin_resource_element_lookup_key.to_s
    self.send(:define_method, :active_admin_resource_element_key) { resource_key }

    resource_ivar = '@' + resource_key.singularize
    self.send(:define_method, :resource) { instance_variable_get(resource_ivar) }

    resource_path = "effective_orders.#{resource_key.singularize}_path"

    if resource_path == 'effective_orders.cart_path'
      self.send(:define_method, :resource_path) { |resource| effective_orders.cart_path }
    else
      self.send(:define_method, :resource_path) { |resource| public_send(resource_path, resource) }
    end

    helper_method :resource, :resource_path

  end

  module ClassMethods
  end

  def active_admin_namespace
    ActiveAdmin.application.namespaces[EffectiveOrders.active_admin_namespace || :root]
  end

  def active_admin_config
    active_admin_namespace.resources[active_admin_resource_key]
  end

  def active_admin_resource_key
    @active_admin_resource_key ||= begin
      namespace = ActiveAdmin.application.namespaces[EffectiveOrders.active_admin_namespace || :root]
      namespace.resources.keys.find { |resource| resource.element == active_admin_resource_element_key }
    end
  end

  def current_active_admin_user
    send(active_admin_namespace.current_user_method) if active_admin_namespace.current_user_method
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
effective_orders-1.4.4 app/controllers/concerns/acts_as_active_admin_controller.rb