rogerkk
rogerkk

Reputation: 5725

DRY-ing up ActiveAdmin

I've got multiple resources in my ActiveAdmin installation that share quite a lot of the same traits, like:

What is the best way to avoid duplicating this functionality across the different resources?

I have set up decorators to avoid duplicating functionality in the index view, but I'm not sure if (and how?) this could be used in the other cases.

Upvotes: 0

Views: 931

Answers (2)

Kukunin
Kukunin

Reputation: 883

You can also extend your module. For example:

module AccountManageable
  def has_manageable_account 
    permit_params :name, :email, :role, :avatar

    filter :name, as: :string
    filter :email, as: :string

    # ... other DSL methods
  end
end

and then in your admin

ActiveAdmin.register Admin do
  extend AccountManageable

  has_manageable_account
end

Upvotes: 7

nistvan
nistvan

Reputation: 2970

You need to extend the DSL with monkey patch:

module ActiveAdmin
  # This is the class where all the register blocks are evaluated.
  class ResourceDSL < DSL
    def your_custom_method attr
      #common code
    end
  end
end

Now you can use your_custom_method in your registered resource file.

https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_dsl.rb

Upvotes: 3

Related Questions