如何获取定义ActiveAdmin资源类的所有member_action方法的列表

时间:2014-05-02 09:49:00

标签: ruby-on-rails activeadmin ruby-on-rails-4.1

我在rails 4.1中创建了一个脚本,它正在读取所有ActiveAdmin资源类及其member_action方法,但是如何在类中获取其他已定义的方法? 该脚本的目标是使用类中定义的方法获取所有资源,以便在数据库中设置管理员用户权限

def get_permissions
    skip_resources = [ 'dashboard.rb' ];
    default_actions = [ 'create', 'read', 'update', 'delete' ];
    resources = Dir.new("#{Rails.root}/app/admin").entries

    all_resources = {}
    resources.each do |resource|
        if resource =~ /.rb/ && (not skip_resources.include? resource)
            resource_class = resource.gsub(".rb","")
            # resource_obj = resource_class.camelize.constantize.new
            all_resources[resource_class] = default_actions #TODO: merge with the defined in class methods
        end
    end
    all_resources
end

1 个答案:

答案 0 :(得分:0)

此rake任务输出资源和页面的所有已定义操作,但它确实需要加载Rails和ActiveAdmin配置。

也可作为要点:active_admin_actions.rake

task :active_admin_actions => :environment do
    skip_resources = [ 'Dashboard' ]

    namespace = ActiveAdmin.application.namespace(:admin)

    pages     = namespace.resources.select { |r| r.is_a? ActiveAdmin::Page }
    resources = namespace.resources.select { |r| r.respond_to? :resource_class }

    resource_actions =
      resources.each_with_object({}) do |resource, actions|
        resource_name = resource.resource_class.name

        if !skip_resources.include? resource_name
          actions[resource_name] = resource.defined_actions
          actions[resource_name].concat resource.member_actions.map { |action| action.name }
          actions[resource_name].concat resource.collection_actions.map { |action| action.name }
        end
      end

    puts resource_actions.inspect

    page_actions =
      pages.each_with_object({}) do |page, actions|
        page_name = page.name

        if !skip_resources.include? page_name
          actions[page_name] = page.page_actions + [:index]
        end
      end

    puts page_actions.inspect
end