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

Render Datetime ingredient in local time zone #3003

Merged
merged 1 commit into from
Aug 29, 2024
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
5 changes: 3 additions & 2 deletions app/components/alchemy/ingredients/datetime_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ def initialize(ingredient, date_format: :"alchemy.default", html_options: {})
end

def call
datetime = ingredient.value.in_time_zone(Rails.application.config.time_zone)
if date_format == "rfc822"
ingredient.value.to_fs(:rfc822)
datetime.to_fs(:rfc822)
else
::I18n.l(ingredient.value, format: date_format)
::I18n.l(datetime, format: date_format)
end.html_safe
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/models/alchemy/ingredients/datetime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
end

describe "value" do
subject { datetime_ingredient.value }
subject(:value) { datetime_ingredient.value }

it "returns a time object" do
is_expected.to be_an(Time)
is_expected.to eq("01.04.2021")
end

it "timezone is UTC" do
expect(value.zone).to eq("UTC")
end

context "without value" do
let(:datetime_ingredient) do
described_class.new(
Expand Down
16 changes: 13 additions & 3 deletions spec/views/alchemy/ingredients/datetime_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
require "rails_helper"

describe "alchemy/ingredients/_datetime_view" do
let(:ingredient) { Alchemy::Ingredients::Datetime.new(value: "2013-10-27 21:14:16 +0100") }
around do |example|
time_zone = Rails.application.config.time_zone
Rails.application.config.time_zone = "Berlin"
example.run
Rails.application.config.time_zone = time_zone
end

let(:ingredient) do
Alchemy::Ingredients::Datetime.new(value: "2024-08-29T10:00:00.000Z")
end

let(:options) { {} }

context "with date value" do
context "without date_format passed" do
it "translates the date value with default format" do
render ingredient, options: options
expect(rendered).to have_content("10.27.2013 20:14")
expect(rendered).to have_content("08.29.2024 12:00")
end
end

Expand All @@ -19,7 +29,7 @@

it "renders the date rfc822 conform" do
render ingredient, options: options
expect(rendered).to have_content("Sun, 27 Oct 2013 20:14:16 +0000")
expect(rendered).to have_content("Thu, 29 Aug 2024 12:00:00 +0200")
end
end
end
Expand Down