Skip to content
Merged
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
9 changes: 5 additions & 4 deletions lib/administrate/field/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ module Administrate
module Field
class Time < Base
def time
return I18n.localize(data, format: format) if options[:format]

data.strftime("%I:%M%p")
I18n.localize(
data,
format: format
)
end

private

def format
options[:format]
options.fetch(:format, "%I:%M%p")
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/example_app/app/dashboards/customer_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CustomerDashboard < Administrate::BaseDashboard
searchable_fields: ["name"],
include_blank: true
),
example_time: Field::Time,
password: Field::Password
}

Expand All @@ -28,6 +29,7 @@ class CustomerDashboard < Administrate::BaseDashboard
:email_subscriber,
:kind,
:territory,
:example_time,
:password
].freeze

Expand Down
1 change: 1 addition & 0 deletions spec/example_app/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
name: name,
email: Faker::Internet.email(name: name),
territory: countries.sample,
example_time: Faker::Time.between(from: "00:00:00", to: "23:59:59"),
password: Faker::Internet.password
}
end
Expand Down
30 changes: 30 additions & 0 deletions spec/lib/fields/time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,34 @@
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: "午後"
}
}

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午後")
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 = {}

I18n.with_locale(:ja) do
with_translations(:ja, formats) do
expect(field.time).to eq("Translation missing: ja.time.pm")
end
end
end
end