Format dates according to instance setting

We default to the ISO8601 format, if the instance
set no preference, but otherwise we use the instance's
preference.
This commit is contained in:
Jan Sandbrink
2025-11-28 09:13:10 +01:00
parent 1b6eb3f9e4
commit 719ac2908a
2 changed files with 17 additions and 4 deletions
@@ -33,7 +33,7 @@ module WorkPackageTypes
class TokenPropertyMapper
STRING_OR_NIL = ->(v) { v&.to_s }
ARRAY = ->(v) { v.compact.presence&.join(", ") }
DATE = ->(v) { v&.strftime("%Y-%m-%d") }
DATE = ->(v) { v&.strftime(Setting.date_format || "%Y-%m-%d") }
DURATION = ->(v) { DurationConverter.output(v) }
class << self
@@ -120,11 +120,13 @@ module WorkPackageTypes
end
def tokenize(custom_field_scope, prefix = nil)
custom_field_scope.pluck(:name, :id, :multi_value).map do |name, id, multiple|
custom_field_scope.pluck(:name, :id, :field_format, :multi_value).map do |name, id, format, multiple|
formatter = if multiple
ARRAY
elsif format == "date"
DATE
else
->(v) { v.is_a?(Symbol) ? v : v&.to_s }
->(v) { v.is_a?(Symbol) ? v : STRING_OR_NIL.call(v) }
end
AttributeToken.new(
:"#{prefix}custom_field_#{id}",
@@ -133,7 +133,7 @@ RSpec.describe WorkPackageTypes::Patterns::TokenPropertyMapper do
expect(token.call(work_package)).to eq("false")
end
it "formats date custom fields correctly" do
it "formats date custom fields with a default format" do
enabled, = subject
token = enabled.detect do |t|
t.key == :"custom_field_#{date_custom_field.id}"
@@ -175,6 +175,17 @@ RSpec.describe WorkPackageTypes::Patterns::TokenPropertyMapper do
expect(detect(enabled, :"custom_field_#{cf.id}")).to be_nil
expect(detect(disabled, :"custom_field_#{cf.id}")&.label).to eq(cf.name)
end
context "when defining an instance date format", with_settings: { date_format: "%d.%m.%Y" } do
it "formats date custom fields according to the instance date format" do
enabled, = subject
token = enabled.detect do |t|
t.key == :"custom_field_#{date_custom_field.id}"
end
expect(token.call(work_package)).to eq("03.10.2025")
end
end
end
private