To ActiveAdmin or not to ActiveAdmin

From FVue
Jump to: navigation, search

Problem

Couldn't agree more with this Reddit comment, after looking up yet another Domain-Specific Language (DSL) ActiveAdmin tweak:

"I used ActiveAdmin for a few years. It was nice at first. Over time, I felt 
like I was writing more ActiveAdmin code with their specific DSL (and the 
templating library arbre) than Rails code. To that end, it wasn't enjoyable 
anymore. YMMV."
From: https://www.reddit.com/r/rails/comments/lv2gtp/rails_admin_or_active_admin/

Environment

  • activeadmin-2.14.0
  • rails-7.0

Solution

Abandon ActiveAdmin. Use plain Ruby on Rails, or maybe this "Administrate" works out.

Workarounds

Arbre template

Old. Use Slim instead: https://github.com/slim-template/slim although this isn't easy because ActiveAdmin is relying heavily on Arbre.

belongs_to

See: https://activeadmin.info/2-resource-customization.html#belongs-to

Old. Is lacking for instance the :shallow parameter.

permitted_params

See: https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters

The `permitted_params' DSL of AA is lacking features like permit and require.

Solution 1: Use Pundit::permitted_attributes. This way you can permit parameters per role.

Solution 2: You can omit AA's permitted_params like this:

ActiveAdmin.register Foo do
  controller do
    def foo_params
      params.require(:foo).permit(:bar, :cee, :dee)
    end
 
    def create
      # Bad, ActiveAdmin DSL
      @foo = Foo.new(permitted_params[:foo])
      # Good
      @foo = Foo.new(foo_params)
      # Instance variables (@) end up as local variables (without @) in form below
    end
  end
 
  form do |_f0|
    active_admin_form_for [:admin, cart], url: admin_order_foos_url(order_id: order.id) do |f|
      fieldset(class: :inputs) do
        legend do
          span 'Foo'
        end
        f.semantic_errors(*f.object.errors.attribute_names)
        # Just to be sure, show (hidden) errors of related objects
        f.semantic_errors(*foo.errors.attribute_names) if foo.errors.count.positive? && f.object.errors.count.zero?
        ol do
          f.input :cee, collection: cee.dees
        end
      end
 
      f.submit 'Create foo'
    end
  end
end

hotwire

ActiveAdmin is lacking support for Hotwire.

helpers

Helpers are not working out of the box, e.g. when you want to use a helper function in the show method.

Solution: Specify helper in the ActiveAdmin controller extension:

# admin/person.rb
ActiveAdmin.register Person do
  controller do
    helper Admin::FooBarHelper
  end
end

with a helper:

# helpers/admin/foo_bar_helper.rb
module Admin
  module FooBarHelper
    def foo_bar(text)
      "foobar#{text}"
    end
  end
end

Comments

blog comments powered by Disqus