Skip to content

Commit c43637a

Browse files
committed
Add administrate_date_default/administrate_datetime_default/administrate_time_default formats
1 parent 27f96fe commit c43637a

7 files changed

Lines changed: 212 additions & 38 deletions

File tree

lib/administrate/field/date.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ class Date < Base
66
def date
77
I18n.localize(
88
data.in_time_zone(timezone).to_date,
9-
format: format
9+
format: format,
10+
default: fallback_format
1011
)
1112
end
1213

1314
private
1415

1516
def format
16-
options.fetch(:format, :default)
17+
options.fetch(:format, default_format)
18+
end
19+
20+
def default_format
21+
:administrate_date_default
22+
end
23+
24+
def fallback_format
25+
I18n.t("date.formats.default", default: "%Y-%m-%d")
1726
end
1827

1928
def timezone

lib/administrate/field/date_time.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,35 @@ class DateTime < Base
66
def date
77
I18n.localize(
88
data.in_time_zone(timezone).to_date,
9-
format: format
9+
format: format(type: :date),
10+
default: fallback_format(type: :date)
1011
)
1112
end
1213

1314
def datetime
1415
I18n.localize(
1516
data.in_time_zone(timezone),
16-
format: format
17+
format: format(type: :datetime),
18+
default: fallback_format(type: :datetime)
1719
)
1820
end
1921

2022
private
2123

22-
def format
23-
options.fetch(:format, :default)
24+
def format(type: :date)
25+
options.fetch(:format, default_format(type: type))
26+
end
27+
28+
def default_format(type: :date)
29+
:"administrate_#{type}_default"
30+
end
31+
32+
def fallback_format(type: :date)
33+
if type == :date
34+
I18n.t("date.formats.default", default: "%Y-%m-%d")
35+
else
36+
I18n.t("time.formats.default", default: "%Y-%m-%d %H:%M")
37+
end
2438
end
2539

2640
def timezone

lib/administrate/field/time.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ class Time < Base
66
def time
77
I18n.localize(
88
data,
9-
format: format
9+
format: format,
10+
default: fallback_format
1011
)
1112
end
1213

1314
private
1415

1516
def format
16-
options.fetch(:format, "%I:%M%p")
17+
options.fetch(:format, default_format)
18+
end
19+
20+
def default_format
21+
:administrate_time_default
22+
end
23+
24+
def fallback_format
25+
"%I:%M%p"
1726
end
1827
end
1928
end

spec/example_app/config/locales/en.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ en:
55
"%m/%d/%Y"
66
with_weekday:
77
"%a %m/%d/%y"
8+
administrate_date_default:
9+
"%m/%d/%Y"
810

911
helpers:
1012
label:
@@ -20,6 +22,10 @@ en:
2022
"%b %-d, %Y"
2123
short:
2224
"%B %d"
25+
administrate_datetime_default:
26+
"%a, %b %-d, %Y at %r"
27+
administrate_time_default:
28+
"%I:%M%p"
2329

2430
titles:
2531
application: Administrate-prototype

spec/lib/fields/date_spec.rb

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,39 @@
66
let(:formats) do
77
{
88
date: {
9-
formats: {default: "%m/%d/%Y", short: "%b %d"},
9+
formats: {
10+
default: "%m/%d/%Y",
11+
short: "%b %d",
12+
administrate_date_default: "%m/%d, %Y"
13+
},
1014
abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 },
1115
abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 }
1216
},
1317
time: {
14-
formats: {default: "%a, %b %-d, %Y", short: "%d %b"}
18+
formats: {
19+
default: "%a, %b %-d, %Y at %r",
20+
short: "%d %b %H:%M",
21+
administrate_datetime_default: "%a, %b %-d, %Y, %r",
22+
administrate_time_default: "%I:%M%p"
23+
}
24+
}
25+
}
26+
end
27+
let(:formats_without_administrate_default) do
28+
{
29+
date: {
30+
formats: {
31+
default: "%m/%d/%Y",
32+
short: "%b %d"
33+
},
34+
abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 },
35+
abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 }
36+
},
37+
time: {
38+
formats: {
39+
default: "%a, %b %-d, %Y at %r",
40+
short: "%d %b %H:%M"
41+
}
1542
}
1643
}
1744
end
@@ -21,11 +48,11 @@
2148
with_translations(:en, formats) do
2249
field = Administrate::Field::Date
2350
.new(:start_date, start_date, :show)
24-
expect(field.date).to eq("12/25/2015")
51+
expect(field.date).to eq("12/25, 2015")
2552
end
2653
end
2754

28-
context "with `prefix` option" do
55+
context "with `format` option" do
2956
it "displays the date in the requested format" do
3057
options_field = Administrate::Field::Date
3158
.with_options(format: :short)
@@ -46,5 +73,34 @@
4673
end
4774
end
4875
end
76+
77+
context "without `format` option" do
78+
it "falls back to default format if administrate_date_default is missing" do
79+
options_field = Administrate::Field::Date
80+
field = options_field.new(:start_date, start_date, :show)
81+
82+
with_translations(:en, formats_without_administrate_default) do
83+
expect(field.date).to eq("12/25/2015")
84+
end
85+
end
86+
87+
it "falls back to static format (%Y-%m-%d) if locale formats are missing" do
88+
options_field = Administrate::Field::Date
89+
field = options_field.new(:start_date, start_date, :show)
90+
91+
with_translations(:unformatted_locale, {}) do
92+
available_locales = I18n.available_locales
93+
locale = I18n.locale
94+
begin
95+
I18n.available_locales = [:unformatted_locale]
96+
I18n.locale = :unformatted_locale
97+
expect(field.date).to eq("2015-12-25")
98+
ensure
99+
I18n.available_locales = available_locales
100+
I18n.locale = locale
101+
end
102+
end
103+
end
104+
end
49105
end
50106
end

spec/lib/fields/date_time_spec.rb

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,78 @@
66
let(:formats) do
77
{
88
date: {
9-
formats: {default: "%m/%d/%Y", short: "%b %d"},
9+
formats: {
10+
default: "%m/%d/%Y",
11+
short: "%b %d",
12+
administrate_date_default: "%m/%d, %Y"
13+
},
1014
abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 },
1115
abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 }
1216
},
1317
time: {
14-
formats: {default: "%a, %b %-d, %Y at %r", short: "%d %b %H:%M"}
18+
formats: {
19+
default: "%a, %b %-d, %Y at %r",
20+
short: "%d %b %H:%M",
21+
administrate_datetime_default: "%a, %b %-d, %Y, %r",
22+
administrate_time_default: "%I:%M%p"
23+
}
24+
}
25+
}
26+
end
27+
let(:formats_without_administrate_default) do
28+
{
29+
date: {
30+
formats: {
31+
default: "%m/%d/%Y",
32+
short: "%b %d"
33+
},
34+
abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 },
35+
abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 }
36+
},
37+
time: {
38+
formats: {
39+
default: "%a, %b %-d, %Y at %r",
40+
short: "%d %b %H:%M"
41+
}
1542
}
1643
}
1744
end
1845

1946
describe "#date" do
20-
it "displays the date" do
21-
with_translations(:en, formats) do
22-
field = Administrate::Field::DateTime
23-
.new(:start_date, start_date, :show)
24-
expect(field.date).to eq("12/25/2015")
47+
context "without `format` option" do
48+
it "displays the date in the administrate default format" do
49+
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
50+
with_translations(:en, formats) do
51+
expect(field.date).to eq("12/25, 2015")
52+
end
53+
end
54+
55+
it "falls back to default format if administrate_date_default is missing" do
56+
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
57+
with_translations(:en, formats_without_administrate_default) do
58+
expect(field.date).to eq("12/25/2015")
59+
end
60+
end
61+
62+
it "falls back to static format (%Y-%m-%d) if locale formats are missing" do
63+
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
64+
65+
with_translations(:unformatted_locale, {}) do
66+
available_locales = I18n.available_locales
67+
locale = I18n.locale
68+
begin
69+
I18n.available_locales = [:unformatted_locale]
70+
I18n.locale = :unformatted_locale
71+
expect(field.date).to eq("2015-12-25")
72+
ensure
73+
I18n.available_locales = available_locales
74+
I18n.locale = locale
75+
end
76+
end
2577
end
2678
end
2779

28-
context "with `prefix` option" do
80+
context "with `format` option" do
2981
it "displays the date in the requested format" do
3082
options_field = Administrate::Field::DateTime
3183
.with_options(format: :short)
@@ -90,15 +142,40 @@
90142
end
91143

92144
describe "#datetime" do
93-
it "displays the datetime" do
94-
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
145+
context "without `format` option" do
146+
it "displays the datetime in the administrate default format" do
147+
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
148+
with_translations(:en, formats) do
149+
expect(field.datetime).to eq("Fri, Dec 25, 2015, 10:15:45 AM")
150+
end
151+
end
152+
153+
it "falls back to default format if administrate_datetime_default is missing" do
154+
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
155+
with_translations(:en, formats_without_administrate_default) do
156+
expect(field.datetime).to eq("Fri, Dec 25, 2015 at 10:15:45 AM")
157+
end
158+
end
95159

96-
with_translations(:en, formats) do
97-
expect(field.datetime).to eq("Fri, Dec 25, 2015 at 10:15:45 AM")
160+
it "falls back to static format (%Y-%m-%d %H:%M) if locale formats are missing" do
161+
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
162+
163+
with_translations(:unformatted_locale, {}) do
164+
available_locales = I18n.available_locales
165+
locale = I18n.locale
166+
begin
167+
I18n.available_locales = [:unformatted_locale]
168+
I18n.locale = :unformatted_locale
169+
expect(field.datetime).to eq("2015-12-25 10:15")
170+
ensure
171+
I18n.available_locales = available_locales
172+
I18n.locale = locale
173+
end
174+
end
98175
end
99176
end
100177

101-
context "with `prefix` option" do
178+
context "with `format` option" do
102179
it "displays the datetime in the requested format" do
103180
options_field = Administrate::Field::DateTime
104181
.with_options(format: :short)

spec/lib/fields/time_spec.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,33 @@
4040

4141
expect(field.time).to eq("04:38PM")
4242
end
43-
end
4443

45-
it "formats the time with localized AM/PM markers" do
46-
time = DateTime.new(2021, 3, 26, 16, 38)
47-
formats = {
48-
time: {
49-
am: "午前",
50-
pm: "午後"
44+
it "formats the time with localized AM/PM markers" do
45+
time = DateTime.new(2021, 3, 26, 16, 38)
46+
formats = {
47+
time: {
48+
am: "午前",
49+
pm: "午後",
50+
formats: {administrate_time_default: "%p%I:%M"}
51+
}
5152
}
52-
}
5353

54-
field = Administrate::Field::Time.new(:time, time, :index)
54+
field = Administrate::Field::Time.new(:time, time, :index)
5555

56-
I18n.with_locale(:ja) do
57-
with_translations(:ja, formats) do
58-
expect(field.time).to eq("04:38午後")
56+
I18n.with_locale(:ja) do
57+
with_translations(:ja, formats) do
58+
expect(field.time).to eq("午後04:38")
59+
end
5960
end
6061
end
6162
end
6263

6364
it "returns a missing translation message if the translation is not available" do
6465
time = DateTime.new(2021, 3, 26, 16, 38)
6566
field = Administrate::Field::Time.new(:time, time, :index)
66-
formats = {}
67+
formats = {
68+
time: {formats: {administrate_time_default: "%I:%M%p"}}
69+
}
6770

6871
I18n.with_locale(:ja) do
6972
with_translations(:ja, formats) do

0 commit comments

Comments
 (0)