Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Time-related Fields ahead of upcoming improvements #2702

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion lib/administrate/field/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Field
class Date < Base
def date
I18n.localize(
data.to_date,
data.in_time_zone(timezone).to_date,
format: format
)
end
Expand All @@ -15,6 +15,10 @@ def date
def format
options.fetch(:format, :default)
end

def timezone
options.fetch(:timezone, ::Time.zone)
end
end
end
end
5 changes: 2 additions & 3 deletions lib/administrate/field/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def date
def datetime
I18n.localize(
data.in_time_zone(timezone),
format: format,
default: data
format: format
)
end

Expand All @@ -25,7 +24,7 @@ def format
end

def timezone
options.fetch(:timezone, ::Time.zone.name || "UTC")
options.fetch(:timezone, ::Time.zone)
end
end
end
Expand Down
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
30 changes: 30 additions & 0 deletions spec/lib/fields/time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,35 @@

expect(field.time).to eq("04:38PM")
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
end
Loading