18

Question pretty much says it all.

I've got an existing site that supports a well defined API.

I need to build up a nice management interface for it, and, since I've used ActiveAdmin to create effect before, I'd love to use it here to.

However, I can't find any details on whether doing so is even possible.

I've read comments saying that AA support ActiveModel type objects, that don't have to be based on ActiveRecord, but I've had no long trying that myself, or finding any examples of such.

Anyone know if this is even possible?

DarinH
  • 4,868
  • 2
  • 22
  • 32
  • Looks like the AA guys are looking to integrate mongo support directly for 1.0.0..... https://github.com/gregbell/active_admin/issues/2714 – DarinH Jul 26 '14 at 16:34

5 Answers5

20

I was able to achieve this by using an ActiveAdmin custom page.

For our example we have a model called MailingList. This is a Ruby class that includes some ActiveModel abilities.

# app/models/mailing_list.rb
class MailingList
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name
  validates_presence_of :name

   def initialize(args)
     # Set up instance variables
   end

   def self.all
     # Use API to retrieve list of records
   end

   def save(args)
     # Use API to save record
   end

   def id
     # Unique identifier from API
   end

   def persisted?
     false
   end
end

To use this API driven model in ActiveAdmin we create a page like so.

# app/admin/mailing_list.rb
ActiveAdmin.register_page 'Mailing Lists' do

  action_item do
     link_to 'New Mailing List', admin_mailing_lists_new_path
  end

  content do
    redirect_to :index
  end

  page_action :index do
    @mailing_lists = MailingList.all
    render :index, :layout => 'active_admin'
  end

  page_action :show do
    render :show, :layout => 'active_admin'
  end

  page_action :new do
    @mailing_list = MailingList.new
    render :new, :layout => 'active_admin'
  end

  page_action :create, :method => :post do
    @mailing_list = MailingList.new(params[:mailing_list])
    if @mailing_list.save
      redirect_to admin_mailing_list_path(@mailing_list.id)
    else
      render :new, :layout => 'active_admin'
    end
  end

end

The page_action methods act as controller actions.

To get this to work as expected I had to add the following routes

# config/routes.rb
post '/admin/mailing_lists' => 'admin/mailing_lists#create'
get '/admin/mailing_lists/new' => 'admin/mailing_lists#new', :as => :admin_new_mailing_list
get '/admin/mailing_lists/:id' => 'admin/mailing_lists#show', :as => :admin_mailing_list

You will also need some views in

app/views/admin/mailing_lists/index.html.erb app/views/admin/mailing_lists/show.html.erb app/views/admin/mailing_lists/new.html.erb

bennick
  • 1,879
  • 15
  • 21
  • 1
    Nice explanation. I was hoping to avoid having to redo a lot of what AA does for you, but that might have to be the case for now. However, now it looks like the AA guys are doing this full on for the 1.0.0 release. CHeck here https://github.com/gregbell/active_admin/issues/2714 – DarinH Jul 26 '14 at 16:34
  • @drventure Thanks and good news that AA will support more than just AR objects. If you are happy with this answer please accept it. – bennick Jul 30 '14 at 21:38
  • While this question is old, it still works for the Rails 4 compatible branch (gem version 1.0.0.pre2). Note that routes are automatically generated for any page_actions you define, so you shouldn't need to define those in your config/routes.rb anymore. – MrDerp Mar 29 '17 at 13:23
  • I know this is old but is there any chance that newer updates of ActiveAdmin removed the `redirect_to` method from their custom pages? Because now I'm getting `undefined method 'redirect_to' for # – Gerry Hernandez Jun 21 '17 at 20:12
1

ActiveAdmin is written to work with ActiveRecord, and by default it doesn't have support for other ORMs. However, there are some plugins available to add support for some. Here are two that I found:

Michael Frederick
  • 16,664
  • 3
  • 43
  • 58
1

There useful answer for this question here. Shortly say it is need to see sources sources of rails db adapters (currently at path rails/activerecord/lib/active_record/connection_adapters/) possible at this link where ActiveRecord adapters is. And especially to see and take abstract_adapter.rb to implement.

Community
  • 1
  • 1
fl-web
  • 462
  • 5
  • 16
0

I haven't used yet, but it could be a starting-point: https://github.com/elia/activeadmin-mongoid

nistvan
  • 2,940
  • 1
  • 16
  • 20
0

When we say, using any gem over cross ORMs, the most important part is whether that particular gem supports other ORMs or not.

In case of Active Admin, what I can guess is, they planned to support ActiveRecord and Mongoid by design. From code base itself, you can see it https://github.com/gregbell/active_admin/tree/master/lib/active_admin/orm. But somehow they were not able to complete it.

Also, if you planned to use, Mongoid as your primary ORM (with mongoDB), there are some other options for admin frameworks as well.

But for answer to your question, NO you can't use ActiveAdmin with Mongoid. Instead, you can look into https://github.com/elia/activeadmin-mongoid (as mentioned in comment above)

r3bo0t
  • 462
  • 4
  • 11