Customizing admin_data for Rails 2.3.x application

mahemoff edited this page Jun 20, 2012 · 3 revisions

Customizing admin_data for Rails 2.3.x application

admin_data allows you to configure certain features. Create a file under config/initializers named admin_data.rb . This is the file were all the configure settings will be stored. Configuration options are listed below.

Number of records per page

By default admin_data leaves it upto will_paginate to decided how many records should be shown in a page. You can control how many records should be shown on a page.

AdminData::Config.set = {
  :will_paginate_per_page => 50

Override the default find condition

I have a model called City which is defined like this.

class City < ActiveRecord::Base
  def to_param

For show action admin_data will generate url which could look like /admin_data/city/miami .

The controller will execute the query assuming that id is miami and the record will not be found.

Here is how that can be fixed.

AdminData::Config.set = {
 :find_conditions =>  {'City' => lambda {|params| {:conditions =>   ["permanent_name = ?", params[:id] ] } } }  

Override the default behaviour of a column

I have a Car model.

class Car < ActiveRecord::Base
  serialize :data

I am creating a new car record.

Car.create(:data => { :color => ‘black’, :year => ‘2000’})

If you look at this record using admin_data then for the column ‘data’ will have following value:


This is not very useful. If I could ask plugin to perform inspect on hash then output will be much more easy to read. Given below is how I can ask plugin to perform inspect method on data column.

AdminData::Config.set = ({
  :column_settings => { 'Car' => { :data => lambda{|model| model.send(:data).inspect}}}

admin_data passes the model instanct to proc and the user can customize the value for that model as per his/her taste.

Replace drop down associations with input text

Let’s say that I have two tables: users table and phone_numbers. If I edit a phone number then I would be presented with the drop down of all the user ids. If I have 50 thousand user records then page will be really slow .

To get around this problem you can configure admin_data so that instead of select field you are given input text.

AdminData::Config.set = ({
 :drop_down_for_associations => false

Custom order of columns in listing

I have an Article model which ,by default, lists columns in following order: id, body, title, author_name, published_at .

If I want order of columns to be changed then I need configure like this.

AdminData::Config.set = {
  :columns_order => {'Article' => [:id, :title, :body, :published_at, :author_name]}  

Explicitly defining route to get around catch-all issue

I have defined a catch-all route so that users can have twitter like url . I have defined catch-all route as given below.

get ':username' => 'users#show'

Now when I try to go to http://localhost:3000/admin_data, I am directed to users controller. How do I get around to this issue.

To get around this issue explictly define the admin_data routes like this

match '/admin_data', :to => 'admin_data/home#index'

Ignore column limit

Ilya brought to my attention this rails ticket #876 . If the input field for a column is shorter than it should be then most likely you have run into this problem. One way to get around to this problem is to configure admin_data to not to use column.limit . In this way all text fields will have width of 60 . You can configure like this

AdminData::Config.set = {
  :ignore_column_limit => true