Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin Custom page with index table

I have created a custom page with ActiveAdmin as follows:

ActiveAdmin.register_page "message_list" do

  controller do
    def index
      @collection = client().account.messages.list.sort_by{ |message| Date.rfc2822(message.date_sent) }.reverse
      render :layout => 'active_admin'
    end
  end
end

I have created an index.html.erb file with a table that I want to display on this page. This however is not optimal. How do I use the active admin standard table layout that also comes with pagination and display it with my table info? I know that ActiveAdmin PageDSL Class does not include #index and therefore I can't simply do:

  index do
    selectable_column
    id_column
    column :to
    column :from
    default_actions
  end

In addition to achieving ActiveAdmin's table layout on a custom page, how do I change the Title of the page itself? As of now it is called "Index".

like image 888
stecd Avatar asked May 20 '14 01:05

stecd


2 Answers

An easier method would be to define an ActiveAdmin resource for your message class, Message, and limit the actions to only allow :index.

ActiveAdmin.register Message do
  actions :index

  index do
    selectable_column
    id_column
    column :to
    column :from
    default_actions
  end

  controller do
    def scoped_collection
      super.where(account_id: account.id).order(:date_sent)

      # Or provide a custom collection similar to the current implementation:
      # client().account.messages.list.sort_by{ |message| Date.rfc2822(message.date_sent) }.reverse
    end

  end
end

It is also possible to rename the resource if necessary by providing an :as option to the #register method:

ActiveAdmin.register Message, as: "Account Message" do
  # ...
end
like image 61
Charles Maresh Avatar answered Sep 19 '22 00:09

Charles Maresh


While the accepted answer works well if you can use an ActiveAdmin resource instead of a custom page, it is possible to get an index-style table on a custom page via Arbre:

<%=
  Arbre::Context.new({}, self) do
    table_for(client().account.messages, sortable: true, class: 'index_table') do
      column :id
      column :created_at
    end
  end
%>
like image 27
Josh Leitzel Avatar answered Sep 22 '22 00:09

Josh Leitzel