Question

Is it possible to retrieve all the registered resources for Active Admin interface?

I have registered a lot of resources in active admin and I want to have a list of registered resources so that it can put into my dropdown box.

Possible to do that?

Was it helpful?

Solution

i guarantee there must be a cleaner way, but you can access your Models that have been added as to AA by accessing the resources collection in the namespace you have defined. For example, if you have AA defined in the :admin namespace

 > ActiveAdmin.application.namespace(:admin).resources.select {|r| r.respond_to? :resource_class_name}.map(&:resource_class_name) 

will produce and array of class names..

=> ["::Activity", "::ActivityType", "::AdminUser"]

that gives you a list of items you can massage into a dropdown.

OTHER TIPS

# I came up with this to generate a hash of
# :namespace => array of arrays containing 2 elements
# [title, link]
#
# With this <strikethrough>it should be trivial to</strikethrough> you can
# create a clickable select.

Hash[*
  ActiveAdmin.
  application.
  namespaces.
  collect{ |ns,v| [  
    ns, 
    v.resources.
        keys.
        select { |k|
            r = ActiveAdmin.application.namespaces[ns].resources.find_by_key(k)
            r.respond_to?(:resource_class_name) && !r.belongs_to?
        }.

        collect{|k|
          [
            k, 
            begin
              Rails.application.routes.url_helpers.send(
                ( 
                  "#{ns}_" + 
                  k.underscore.pluralize.gsub('/','_') + 
                  "_index_path"
                ).to_sym
              ) 
            rescue
              Rails.application.routes.url_helpers.send(
                ( 
                  "#{ns}_" + 
                  k.underscore.pluralize.gsub('/','_') + 
                  "_path"
                ).to_sym
              )
            end
          ]
        } 
  ]}.
  flatten(1)
]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top