Skip to content

Commit

Permalink
Localise Dates (thoughtbot#570)
Browse files Browse the repository at this point in the history
* Reorder I18n backends in `with_translations` helper

The `with_translations` helper allows translations to be setup for a
method under test. A recent change (thoughtbot#772) added a backend chain to
ensure this helper won't break other spec by replacing all translations
and causing "missing_translations" failures.

This changes the order of the I18n backends so that specs will first use
the translations provided to the `with_translations` block and then
fallback to the default translations. Otherwise, there would be no way
to override the default translations in test cases.

* Adds format option to DateTime field

Updates DateTime field to accept 'format' option which determines
the display format of the field. The option can be
* A key for the localized date format in the corresponding yaml config
file
* A strftime format string
  • Loading branch information
nburkley authored and iarie committed Jun 17, 2017
1 parent fdf7be9 commit f1771a4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/views/fields/date_time/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ as a localized date & time string.
%>

<% if field.data %>
<%= l field.data.to_date %>
<%= field.date %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/fields/date_time/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ as a localized date & time string.
%>

<% if field.data %>
<%= l(field.data, default: field.data) %>
<%= field.datetime %>
<% end %>
13 changes: 13 additions & 0 deletions lib/administrate/field/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
module Administrate
module Field
class DateTime < Base
def date
I18n.localize(data.to_date, format: format)
end

def datetime
I18n.localize(data, format: format, default: data)
end

private

def format
options.fetch(:format, :default)
end
end
end
end
80 changes: 78 additions & 2 deletions spec/lib/fields/date_time_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,82 @@
require "spec_helper"
require "rails_helper"
require "administrate/field/date_time"
require "support/field_matchers"

describe Administrate::Field::DateTime do
let(:start_date) { DateTime.parse("2015-12-25 10:15:45") }
let(:formats) 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")
end
end

context "with `prefix` option" do
it "displays the date in the requested format" do
options_field = Administrate::Field::DateTime.
with_options(format: :short)
field = options_field.new(:start_date, start_date, :show)

with_translations(:en, formats) do
expect(field.date).to eq("Dec 25")
end
end

it "displays the date using a format string" do
options_field = Administrate::Field::DateTime.
with_options(format: "%Y")
field = options_field.new(:start_date, start_date, :show)

with_translations(:en, formats) do
expect(field.date).to eq("2015")
end
end
end
end

describe "#datetime" do
it "displays the datetime" 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 at 10:15:45 AM")
end
end

context "with `prefix` option" do
it "displays the datetime in the requested format" do
options_field = Administrate::Field::DateTime.
with_options(format: :short)
field = options_field.new(:start_date, start_date, :show)

with_translations(:en, formats) do
expect(field.datetime).to eq("25 Dec 10:15")
end
end

it "displays the datetime format string" do
options_field = Administrate::Field::DateTime.
with_options(format: "%H:%M")
field = options_field.new(:start_date, start_date, :show)

with_translations(:en, formats) do
expect(field.datetime).to eq("10:15")
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/support/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def with_translations(locale, translations)
new_backend = I18n::Backend::KeyValue.new({}, true)
new_backend.store_translations(locale, translations)

I18n.backend = I18n::Backend::Chain.new(original_backend, new_backend)
I18n.backend = I18n::Backend::Chain.new(new_backend, original_backend)

yield
ensure
Expand Down

0 comments on commit f1771a4

Please sign in to comment.