Skip to content

Commit 3b69443

Browse files
authored
Merge pull request #3349 from AlchemyCMS/backport/7.3-stable/pr-3348
[7.3-stable] fix(DatetimeView): Use settings value if present
2 parents bade083 + 2cb0c12 commit 3b69443

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if ENV["DB"] == "mysql" || ENV["DB"] == "mariadb"
1515
end
1616
gem "pg", "~> 1.0" if ENV["DB"] == "postgresql"
1717

18-
gem "alchemy_i18n", git: "https://github.com/AlchemyCMS/alchemy_i18n.git", branch: "main"
18+
gem "alchemy_i18n", git: "https://github.com/AlchemyCMS/alchemy_i18n.git", branch: "4.2-stable"
1919

2020
group :development, :test do
2121
gem "execjs", "~> 2.9.1"

app/components/alchemy/ingredients/datetime_view.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
module Alchemy
22
module Ingredients
33
class DatetimeView < BaseView
4+
DEFAULT_DATE_FORMAT = :"alchemy.default"
5+
46
attr_reader :date_format
57

68
# @param ingredient [Alchemy::Ingredient]
79
# @param date_format [String] The date format to use. Use either a strftime format string, a I18n format symbol or "rfc822". Defaults to "time.formats.alchemy.default".
8-
def initialize(ingredient, date_format: :"alchemy.default", html_options: {})
10+
def initialize(ingredient, date_format: nil, html_options: {})
911
super(ingredient)
10-
@date_format = settings_value(:date_format, value: date_format)
12+
@date_format = settings_value(:date_format, value: date_format) || DEFAULT_DATE_FORMAT
1113
end
1214

1315
def call

app/models/alchemy/ingredients/headline.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def size_options
4242
private
4343

4444
def levels
45-
settings.fetch(:levels, (1..6))
45+
settings.fetch(:levels, 1..6)
4646
end
4747

4848
def sizes

spec/views/alchemy/ingredients/datetime_view_spec.rb

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,43 @@
1616

1717
let(:options) { {} }
1818

19+
subject do
20+
render ingredient, options: options
21+
rendered
22+
end
23+
1924
context "with date value" do
2025
context "without date_format passed" do
2126
it "translates the date value with default format" do
22-
render ingredient, options: options
23-
expect(rendered).to have_content("08.29.2024 12:00")
27+
is_expected.to have_content("08.29.2024 12:00")
28+
end
29+
end
30+
31+
context "with date_format in settings" do
32+
before do
33+
allow(ingredient).to receive(:settings) do
34+
{date_format: "%d.%m."}
35+
end
36+
end
37+
38+
it "translates the date value with format from settings" do
39+
is_expected.to have_content("29.08.")
40+
end
41+
42+
context "but with format passed as argument" do
43+
let(:options) { {date_format: "%d.%m.%Y"} }
44+
45+
it "translates the date value with format from arguments" do
46+
is_expected.to have_content("29.08.2024")
47+
end
2448
end
2549
end
2650

2751
context "with option date_format set to rfc822" do
2852
let(:options) { {date_format: "rfc822"} }
2953

3054
it "renders the date rfc822 conform" do
31-
render ingredient, options: options
32-
expect(rendered).to have_content("Thu, 29 Aug 2024 12:00:00 +0200")
55+
is_expected.to have_content("Thu, 29 Aug 2024 12:00:00 +0200")
3356
end
3457
end
3558
end
@@ -38,8 +61,7 @@
3861
let(:ingredient) { Alchemy::Ingredients::Datetime.new(value: nil) }
3962

4063
it "renders nothing" do
41-
render ingredient, options: options
42-
expect(rendered).to eq("")
64+
is_expected.to eq("")
4365
end
4466
end
4567
end

0 commit comments

Comments
 (0)