How to change how ActiveAdmin displays time (every time)

Tom Prats picture Tom Prats · Jul 25, 2013 · Viewed 8.4k times · Source

Since the default time in the database is in utc, I wanted to be able to still display it in the users correct time. To do this I had to take column :created_at and change it into this:

index do
...
  column :created_at, :sortable => :created_at do |obj|
    obj.created_at.localtime.strftime("%B %d, %Y %H:%M)
  end
...
end

Seems pretty easy to do once or twice, but when you need to override every index and show method, the process get's a little taxing.

Is there a way to override how ActiveAdmin displays time without having to override each occurrence?

I know I could create a function or probably use the functions provided for time better, but I'd still have to use it each time I want to display time. I want to override it without worrying I missed one.

Answer

Melinda Weathers picture Melinda Weathers · Apr 1, 2014

You can target just ActiveAdmin by telling it to always use a particular filter in config/initializers/active_admin.rb , by adding a line like this:

config.before_action :set_admin_timezone

(or config.before_filter :set_admin_timezone for versions of Rails before Rails 4)

Then in your ApplicationController, you can just define the method set_admin_timezone, and ActiveAdmin will use it. For example:

def set_admin_timezone
  Time.zone = 'Eastern Time (US & Canada)'
end

You should be able look up the current admin user in that method (in order to get their particular timezone), but I haven't tried that.