Abhi On Rails

Ruby On Rails - What I learned recently

Active Admin - Custom Pages, Views and Actions

Active Admin is a Ruby on Rails plugin to generate Admin console for the application. It has a lots of functionalities implemented out of the box where we just need to integrate it and use it. Most of the time you don’t need to customize the plugin.

But the main Advantage of Active Admin is that you can customize almost all the built-in functionalities. The main functionalities are: Navigation, User Authentication, Scopes, Action Items, Index styles, Filters, Sidebar sections, API and Downloads.

By default Active Admin will generate the pages to CRUD a model when you generate the resource file for the model using rails generator:

    $> rails generate active_admin:resource [MyModelName]
  

But what if in your application you need an Admin page where you want to show some calculated data of a model or multiple models which is obviously not a resource file. You can create custom pages:

      # app/admin/calendar.rb
      ActiveAdmin.register_page "Calendar" do
        content do
          render partial: 'calendar', :locals => {:transactions => Transaction.all}
        end
      end
    

Now you can put your view/html content in the partial:

# App/views/admin/calendar/_calendar.html.arb <table> <tr> <th>Date</th> <th>From</th> </tr> <% transactions.each do |transaction| %> <tr> <td><%= transaction[:date] %></td> <td><%= transaction[:type] %></td> </tr> <% end %> </table> <%= paginate transactions %>

This is enough to create a simple custom page which will show the data in a customized view. But in some situation you need a button to perform some operations like download the data. In those situations you need custom action and action item. Modified custom page:

      # app/admin/calendar.rb
      ActiveAdmin.register_page "Calendar" do
        content do
          render partial: 'calendar', :locals => {:transactions => Transaction.all}
        end
        
        page_action :csv, method: :get do
          transactions = Transaction.all.page(params[:page]).per(Transaction::ADMIN_PAGE_LIMIT)
          csv = CSV.generate( encoding: 'Windows-1251' ) do |csv|
            # add headers
            csv << [ "Date", "From"]
            transactions.each do |transaction|
              csv << [ transaction[:date], transaction[:type]]
            end     
          end
          # send file to user
          send_data csv.encode('Windows-1251'), type: 'text/csv; charset=windows-1251; header=present', disposition: "attachment; filename=transactions_#{DateTime.now.to_s}.csv"
        end
        
        action_item :add do
          link_to "Export to CSV", admin_calendar_csv_path, method: :get
        end
      end
    

You can also add custom filters to filter the data. You just need to add the following line to the custom page:

      sidebar :filter, partial: 'filter'
    

and create a view for the filter partial

# app/views/admin/calendar/_filter.html.arb <%= form_tag("/admin/calendar", method: "get", class: "filter_form") do %> <div class="filter_form_field"> <%= label_tag(:type, "From") %> <%= select_tag(:type, options_for_select([['All', 'all'], ['Type1', 'type1'], ['Type2', 'type2']], params[:type])) %> </div> <div class="filter_form_field"> <%= label_tag(:date, "Date") %> <%= date_field_tag(:date, params[:date]) %> </div> <div class="buttons"> <input type="submit" name="commit" value="Filter"> <a class="clear_filters_btn" href="/admin/Calendar">Clear Filters</a> </div> <% end %>

You need to use the filter params in your custom page to filter the data:

      Transaction.where(:type => /.*#{params[:type]}.*/) 
    

As I mentioned before, this is the greatest advantage of using Active Admin. You can customize almost all the functionalities :)

blog comments powered by Disqus
← Integrating FriendlyID, Inh...