Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/administrate/field/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ class Date < Base
def date
I18n.localize(
data.in_time_zone(timezone).to_date,
format: format
format: format,
default: fallback_format
)
end

private

def format
options.fetch(:format, :default)
options.fetch(:format, default_format)
end

def default_format
:administrate_date_default
end

def fallback_format
I18n.t("date.formats.default", default: "%Y-%m-%d")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In environments where rails-i18n is installed, formats.default should always be present, but I've added an additional fallback just in case.

end

def timezone
Expand Down
22 changes: 18 additions & 4 deletions lib/administrate/field/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,35 @@ class DateTime < Base
def date
I18n.localize(
data.in_time_zone(timezone).to_date,
format: format
format: format(type: :date),
default: fallback_format(type: :date)
)
end

def datetime
I18n.localize(
data.in_time_zone(timezone),
format: format
format: format(type: :datetime),
default: fallback_format(type: :datetime)
)
end

private

def format
options.fetch(:format, :default)
def format(type: :date)
options.fetch(:format, default_format(type: type))
end

def default_format(type: :date)
:"administrate_#{type}_default"
end

def fallback_format(type: :date)
if type == :date
I18n.t("date.formats.default", default: "%Y-%m-%d")
else
I18n.t("time.formats.default", default: "%Y-%m-%d %H:%M")
end
end

def timezone
Expand Down
13 changes: 11 additions & 2 deletions lib/administrate/field/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ class Time < Base
def time
I18n.localize(
data,
format: format
format: format,
default: fallback_format
)
end

private

def format
options.fetch(:format, "%I:%M%p")
options.fetch(:format, default_format)
end

def default_format
:administrate_time_default
end

def fallback_format
"%I:%M%p"
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/example_app/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ en:
"%m/%d/%Y"
with_weekday:
"%a %m/%d/%y"
administrate_date_default:
"%m/%d/%Y"

helpers:
label:
Expand All @@ -20,6 +22,10 @@ en:
"%b %-d, %Y"
short:
"%B %d"
administrate_datetime_default:
"%a, %b %-d, %Y at %r"
administrate_time_default:
"%I:%M%p"

titles:
application: Administrate-prototype
64 changes: 60 additions & 4 deletions spec/lib/fields/date_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,39 @@
let(:formats) do
{
date: {
formats: {default: "%m/%d/%Y", short: "%b %d"},
formats: {
default: "%m/%d/%Y",
short: "%b %d",
administrate_date_default: "%m/%d, %Y"
},
abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 },
abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 }
},
time: {
formats: {default: "%a, %b %-d, %Y", short: "%d %b"}
formats: {
default: "%a, %b %-d, %Y at %r",
short: "%d %b %H:%M",
administrate_datetime_default: "%a, %b %-d, %Y, %r",
administrate_time_default: "%I:%M%p"
}
}
}
end
let(:formats_without_administrate_default) do
{
date: {
formats: {
default: "%m/%d/%Y",
short: "%b %d"
},
abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 },
abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 }
},
time: {
formats: {
default: "%a, %b %-d, %Y at %r",
short: "%d %b %H:%M"
}
}
}
end
Expand All @@ -21,11 +48,11 @@
with_translations(:en, formats) do
field = Administrate::Field::Date
.new(:start_date, start_date, :show)
expect(field.date).to eq("12/25/2015")
expect(field.date).to eq("12/25, 2015")
end
end

context "with `prefix` option" do
context "with `format` option" do
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not meant to cover the prefix option, so I changed it to use format instead.

it "displays the date in the requested format" do
options_field = Administrate::Field::Date
.with_options(format: :short)
Expand All @@ -46,5 +73,34 @@
end
end
end

context "without `format` option" do
it "falls back to default format if administrate_date_default is missing" do
options_field = Administrate::Field::Date
field = options_field.new(:start_date, start_date, :show)

with_translations(:en, formats_without_administrate_default) do
expect(field.date).to eq("12/25/2015")
end
end

it "falls back to static format (%Y-%m-%d) if locale formats are missing" do
options_field = Administrate::Field::Date
field = options_field.new(:start_date, start_date, :show)

with_translations(:unformatted_locale, {}) do
available_locales = I18n.available_locales
locale = I18n.locale
begin
I18n.available_locales = [:unformatted_locale]
I18n.locale = :unformatted_locale
expect(field.date).to eq("2015-12-25")
ensure
I18n.available_locales = available_locales
I18n.locale = locale
end
end
end
end
end
end
103 changes: 90 additions & 13 deletions spec/lib/fields/date_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,78 @@
let(:formats) do
{
date: {
formats: {default: "%m/%d/%Y", short: "%b %d"},
formats: {
default: "%m/%d/%Y",
short: "%b %d",
administrate_date_default: "%m/%d, %Y"
},
abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 },
abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 }
},
time: {
formats: {default: "%a, %b %-d, %Y at %r", short: "%d %b %H:%M"}
formats: {
default: "%a, %b %-d, %Y at %r",
short: "%d %b %H:%M",
administrate_datetime_default: "%a, %b %-d, %Y, %r",
administrate_time_default: "%I:%M%p"
}
}
}
end
let(:formats_without_administrate_default) do
{
date: {
formats: {
default: "%m/%d/%Y",
short: "%b %d"
},
abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 },
abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 }
},
time: {
formats: {
default: "%a, %b %-d, %Y at %r",
short: "%d %b %H:%M"
}
}
}
end

describe "#date" do
it "displays the date" do
with_translations(:en, formats) do
field = Administrate::Field::DateTime
.new(:start_date, start_date, :show)
expect(field.date).to eq("12/25/2015")
context "without `format` option" do
it "displays the date in the administrate default format" do
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
with_translations(:en, formats) do
expect(field.date).to eq("12/25, 2015")
end
end

it "falls back to default format if administrate_date_default is missing" do
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
with_translations(:en, formats_without_administrate_default) do
expect(field.date).to eq("12/25/2015")
end
end

it "falls back to static format (%Y-%m-%d) if locale formats are missing" do
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)

with_translations(:unformatted_locale, {}) do
available_locales = I18n.available_locales
locale = I18n.locale
begin
I18n.available_locales = [:unformatted_locale]
I18n.locale = :unformatted_locale
expect(field.date).to eq("2015-12-25")
ensure
I18n.available_locales = available_locales
I18n.locale = locale
end
end
end
end

context "with `prefix` option" do
context "with `format` option" do
it "displays the date in the requested format" do
options_field = Administrate::Field::DateTime
.with_options(format: :short)
Expand Down Expand Up @@ -90,15 +142,40 @@
end

describe "#datetime" do
it "displays the datetime" do
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
context "without `format` option" do
it "displays the datetime in the administrate default format" do
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
with_translations(:en, formats) do
expect(field.datetime).to eq("Fri, Dec 25, 2015, 10:15:45 AM")
end
end

it "falls back to default format if administrate_datetime_default is missing" do
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)
with_translations(:en, formats_without_administrate_default) do
expect(field.datetime).to eq("Fri, Dec 25, 2015 at 10:15:45 AM")
end
end

with_translations(:en, formats) do
expect(field.datetime).to eq("Fri, Dec 25, 2015 at 10:15:45 AM")
it "falls back to static format (%Y-%m-%d %H:%M) if locale formats are missing" do
field = Administrate::Field::DateTime.new(:start_date, start_date, :show)

with_translations(:unformatted_locale, {}) do
available_locales = I18n.available_locales
locale = I18n.locale
begin
I18n.available_locales = [:unformatted_locale]
I18n.locale = :unformatted_locale
expect(field.datetime).to eq("2015-12-25 10:15")
ensure
I18n.available_locales = available_locales
I18n.locale = locale
end
end
end
end

context "with `prefix` option" do
context "with `format` option" do
it "displays the datetime in the requested format" do
options_field = Administrate::Field::DateTime
.with_options(format: :short)
Expand Down
29 changes: 16 additions & 13 deletions spec/lib/fields/time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,33 @@

expect(field.time).to eq("04:38PM")
end
end

it "formats the time with localized AM/PM markers" do
time = DateTime.new(2021, 3, 26, 16, 38)
formats = {
time: {
am: "午前",
pm: "午後"
it "formats the time with localized AM/PM markers" do
time = DateTime.new(2021, 3, 26, 16, 38)
formats = {
time: {
am: "午前",
pm: "午後",
formats: {administrate_time_default: "%p%I:%M"}
}
}
}

field = Administrate::Field::Time.new(:time, time, :index)
field = Administrate::Field::Time.new(:time, time, :index)

I18n.with_locale(:ja) do
with_translations(:ja, formats) do
expect(field.time).to eq("04:38午後")
I18n.with_locale(:ja) do
with_translations(:ja, formats) do
expect(field.time).to eq("午後04:38")
end
end
end
end

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

I18n.with_locale(:ja) do
with_translations(:ja, formats) do
Expand Down