Git Psuh
Git Psuh

Reputation: 322

Active Admin's before_filter applied to a given namespace

I have my main app living under the namespace /namespace1 and an engine living under /myengine. Both are using the Active Admin engine.

I want to make sure a before_filter is executed before people are allowed inside the /myengine namespace.

I gave a try at :

https://viget.com/extend/8-insanely-useful-activeadmin-customizations

This solution does not work because it extends BaseController thus causing this restriction in /myengine to leak out and affect the main app's namespace, /namespace1

It works if I duplicate the code in all my admin controllers but, hey, code duplication sucks big time, we all know that !

if defined?(ActiveAdmin)
      # https://github.com/activeadmin/activeadmin/wiki/Define-a-resource-inside-an-engine

      ActiveAdmin.register MyResource, namespace: :myengine do
          before_filter :myfilter

          def myfilter
               # if user is not allowed within /myengine
               # kick his a** back to /namespace1 with a message saying he's not allowed in !
          end
end

I'm just looking a nice and clean way to tell ActiveAdmin "please use this filter for the given engine namespace and do not apply this to the main app's namespace" ! I'm sure there is :)

I tried to play around with the config.before_filter in my engine's Active Admin initializer but it's the same old story, this applies to AA's global settings, affecting my main app's namespace and not just the engine...

Thanks for your help !

Upvotes: 0

Views: 1116

Answers (2)

Ivo Dancet
Ivo Dancet

Reputation: 191

Just got past this by using this in AA initializer:

config.before_filter :namespace1_filter, 
  if: proc{ request.path.split("/")[1] == "namespace1" }

config.before_filter :namespace2_filter, 
  if: proc{ request.path.split("/")[1] == "namespace2" }

Wish there's a better way to get the current AA namespace. Couldn't find it immediately.

Upvotes: 2

Charles Maresh
Charles Maresh

Reputation: 3363

Within ActiveAdmin controllers it is possible to access the configuration which includes ActiveAdmin::Namespace object. Accessing the namespace is done via the #active_admin_config method. The filter could then act on the value of Namespace#name:

ActiveAdmin.register MyResource do

  before_filter :authorize_access_to_myengine

  controller do

    private

    def authorize_access_to_myengine
      if active_admin_namespace.name == :myengine
        # check user's authorization
      end
    end

    def active_admin_namespace
      active_admin_config.namespace
    end
  end
end

Upvotes: 0

Related Questions