Getting ActiveAdmin To Work With Mongoid On Rails 3.2.x

February 15th 2012

I am working on an app where I wanted to use ActiveAdmin. The interesting part is we are using Mongo for the database and chose Mongoid as the ORM. We aren’t budging on that decision and ActiveAdmin is not natively ORM agnostic. I found this Github Repository that has an example app where neccesary patching is done to allow ActiveAdmin and Mongoid to not just coexist but work cohesively to deliver an admin interface. Based on our Git history and my memory (which is rarely accurate) the most important part is the patch in initializers. There may have also been some tweaks made in registering models in app/admin.

Recently to easier merge with a hosting environment as well as keep the app fresh and up to date, we put the app on Rack 1.4.1 and Rails 3.2.x. This required upgrading meta_search gem and activeadmin gem. Afterwards some of the patching that had been done started whipping nasty errors around.

superclass mismatch for class ResourceController (TypeError)

At this point our config/initializers/active_admin_mongoid_patches.rb looked like:

module ActiveAdmin
  class Namespace
    # Disable comments
    def comments?
      false
    end
  end

  class Resource
    def resource_table_name
      resource.collection_name
    end

    # Disable filters
    def add_default_sidebar_sections
    end
  end

  class ResourceController < ::InheritedResources::Base
    # Use #desc and #asc for sorting.
    def sort_order(chain)
      params[:order] ||= active_admin_config.sort_order
      table_name = active_admin_config.resource_table_name
      if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
        chain.send($2, $1)
      else
        chain # just return the chain
      end
    end

    # Disable filters
    def search(chain)
      chain
    end
  end
end

Fixing our little dilemma is really quite simple, our superclass exception is occurring because we are re-opening the ActiveAdmin::ResourceController class, but the original one as it is defined does not inherit from ::InheritedResources::Base (anymore) instead it now inherits from ActiveAdmin::Base. So lets fix that.

module ActiveAdmin
  class Namespace
    # Disable comments
    def comments?
      false
    end
  end

  class Resource
    def resource_table_name
      resource.collection_name
    end

    # Disable filters
    def add_default_sidebar_sections
    end
  end

  class ResourceController < ActiveAdmin::BaseController
    # Use #desc and #asc for sorting.
    def sort_order(chain)
      params[:order] ||= active_admin_config.sort_order
      table_name = active_admin_config.resource_table_name
      if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
        chain.send($2, $1)
      else
        chain # just return the chain
      end
    end

    # Disable filters
    def search(chain)
      chain
    end
  end
end

Great now everyone is happy!