-
Notifications
You must be signed in to change notification settings - Fork 55
Customizing admin_data for a Rails 3 application
admin_data allows you to configure certain features. Create a file under config/initializers named admin_data.rb such the full path to the file is config/initializers/admin_data.rb . This is the file were all the configure settings will be stored. Configuration options are listed below.
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 do |config|
config.number_of_records_per_page = 50
}
##Customize column header name in the list##
By default admin_data puts the column name as the column header in the search result. Here is how you can configure that
AdminData.config do |config|
config.column_headers = {'City' => {:id => 'ID', :name => 'City Name', :data => 'City Info'}}
end
##Add additional column to display extra information##
Let's say I have a User model which has_many Phone_numbers. In the search list I also want to see all the phone number of the given user. This is how that can be accomplished.
config.columns_order = { 'User' => [:id, :phone_numbers]) }
config.column_settings = {'User' => { :phone_numbers => lambda { |model|
model.phone_numbers.map {|r| r.number}.join(', ') } } }
First line is saying that in the columns_order phone_numbers will come after id. The second line is saying how to calculate the value for that column.
I have a model called City which is defined like this.
class City < ActiveRecord::Base
def to_param
self.permanent_name
end
end
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 do |config|
config.find_conditions = {'City' => lambda {|params| {:conditions => ["permanent_name = ?", params[:id] ] } } }
}
I have a Car model.
class Car < ActiveRecord::Base
serialize :data
end
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:
colorblackyear2000
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 do |config|
config.column_settings = { 'Car' => { :data => lambda{|model| model.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.
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 do |config|
config.drop_down_for_associations = false
})
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 do |config|
config.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 http://twitter.com/user_name . 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'
If you find this conflicts with the item view path (/admin_data/klass/:class/:id) then rename the helper like this
match '/admin_data', :to => 'admin_data/home#index', :as => 'admin_data_root'