Skip to content

Commit 1e8abf1

Browse files
committed
add localize format to the configuration
1 parent f99081c commit 1e8abf1

File tree

6 files changed

+44
-2
lines changed

6 files changed

+44
-2
lines changed

docs/1-general-configuration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ to your application's `config/locales` folder and update it. We welcome new/upda
4848
so feel free to [contribute](https://github.com/activeadmin/activeadmin/blob/master/CONTRIBUTING.md)!
4949
To translate third party gems like devise, use for example devise-i18n.
5050

51+
## Localize Format For Dates and Times
52+
53+
Active Admin sets `:long` as default localize format for dates and times.
54+
If you want, you can customize it.
55+
56+
```ruby
57+
config.localize_format = :short
58+
```
59+
5160
## Namespaces
5261

5362
When registering resources in Active Admin, they are loaded into a namespace.

lib/active_admin/application.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ def initialize
105105
# Set flash message keys that shouldn't show in ActiveAdmin
106106
inheritable_setting :flash_keys_to_except, ['timedout']
107107

108+
# Set default localize format for Date/Time values
109+
inheritable_setting :localize_format, :long
110+
108111
# Active Admin makes educated guesses when displaying objects, this is
109112
# the list of methods it tries calling in order
110113
setting :display_name_methods, [ :display_name,

lib/active_admin/view_helpers/display_helper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def pretty_format(object)
4646
when String, Numeric, Symbol, Arbre::Element
4747
object.to_s
4848
when Date, Time
49-
localize object, format: :long
49+
localize object, format: active_admin_application.localize_format
5050
else
5151
if defined?(::ActiveRecord) && object.is_a?(ActiveRecord::Base) ||
5252
defined?(::Mongoid) && object.class.include?(Mongoid::Document)
@@ -56,7 +56,6 @@ def pretty_format(object)
5656
end
5757
end
5858
end
59-
6059
end
6160
end
6261
end

lib/generators/active_admin/install/templates/active_admin.rb.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ ActiveAdmin.setup do |config|
140140
#
141141
# config.before_filter :do_something_awesome
142142

143+
# == Localize Date/Time Format
144+
#
145+
# Set the localize format to display dates and times.
146+
# To understand how to localize your app with I18n, read more at
147+
# https://github.com/svenfuchs/i18n/blob/master/lib%2Fi18n%2Fbackend%2Fbase.rb#L52
148+
#
149+
config.localize_format = :long
150+
143151
# == Setting a Favicon
144152
#
145153
# config.favicon = 'favicon.ico'

spec/unit/application_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@
4949
expect(application.favicon).to eq false
5050
end
5151

52+
it "should return default localize format" do
53+
expect(application.localize_format).to eq :long
54+
end
55+
56+
it "should set localize format" do
57+
application.localize_format = :default
58+
expect(application.localize_format).to eq :default
59+
end
60+
5261
it "should set the site's favicon" do
5362
application.favicon = "/a/favicon.ico"
5463
expect(application.favicon).to eq "/a/favicon.ico"

spec/unit/pretty_format_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ def method_missing(*args, &block)
2929
expect(pretty_format(t)).to eq "February 28, 1985 20:15"
3030
end
3131

32+
context "apply custom localize format" do
33+
before do
34+
ActiveAdmin.application.localize_format = :short
35+
end
36+
after do
37+
ActiveAdmin.application = nil
38+
end
39+
it "should actually do the formatting" do
40+
t = Time.utc(1985, "feb", 28, 20, 15, 1)
41+
42+
expect(pretty_format(t)).to eq "28 Feb 20:15"
43+
end
44+
end
45+
3246
context "with non-English locale" do
3347
before(:all) do
3448
@previous_locale = I18n.locale.to_s

0 commit comments

Comments
 (0)