Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Introduce duration component

Oliver Günther8ef5676f584b
diff --git a/app/components/open_project/common/duration_component.rb b/app/components/open_project/common/duration_component.rb
new file mode 100644
index 00000000000..55864e7dcfc
--- /dev/null
+++ b/app/components/open_project/common/duration_component.rb
@@ -0,0 +1,87 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module OpenProject
+ module Common
+ class DurationComponent < Primer::Component
+ VALID_TYPES = %i[seconds minutes hours days weeks months years].freeze
+ attr_reader :duration, :abbreviated, :separator
+
+ def initialize(duration, type = :seconds, separator: ', ', abbreviated: false, **args)
+ super
+
+ @duration = parse_duration(duration, type)
+ @abbreviated = abbreviated
+ @separator = separator
+ @system_arguments = args
+ end
+
+ def call
+ render(Primer::Beta::Text.new) do
+ localized_parts.join(separator)
+ end
+ end
+
+ private
+
+ def localized_parts
+ duration.parts.map do |fragment, count|
+ abbreviated_key = "datetime.distance_in_words.x_#{fragment}_abbreviated"
+ key = "datetime.distance_in_words.x_#{fragment}"
+
+ if abbreviated && I18n.exists?(abbreviated_key)
+ I18n.t(abbreviated_key, count:)
+ else
+ I18n.t(key, count:)
+ end
+ end
+ end
+
+ def parse_duration(duration, type)
+ case duration
+ when ActiveSupport::Duration
+ parse_number(duration.to_i, :seconds)
+ when Numeric
+ parse_number(duration, type)
+ when String
+ ActiveSupport::Duration.parse(duration)
+ else
+ raise ArgumentError, "Invalid duration type #{duration.class}."
+ end
+ end
+
+ def parse_number(duration, type)
+ if type.nil? || VALID_TYPES.exclude?(type.to_sym)
+ raise ArgumentError, "Provide known type (#{VALID_TYPES.join(', ')}) when providing a number to this component."
+ end
+
+ ActiveSupport::Duration.build(duration.send(type))
+ end
+ end
+ end
+end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 29a7956e380..2802d8ded1a 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1306,18 +1306,33 @@ en:
x_days:
one: "1 day"
other: "%{count} days"
- x_min_abbreviated:
- one: "1 min"
- other: "%{count} mins"
x_minutes:
one: "1 minute"
other: "%{count} minutes"
+ x_minutes_abbreviated:
+ one: "1 min"
+ other: "%{count} mins"
+ x_hours:
+ one: "1 hour"
+ other: "%{count} hours"
+ x_hours_abbreviated:
+ one: "1 hr"
+ other: "%{count} hrs"
+ x_weeks:
+ one: "1 week"
+ other: "%{count} weeks"
x_months:
one: "1 month"
other: "%{count} months"
+ x_years:
+ one: "1 year"
+ other: "%{count} years"
x_seconds:
one: "1 second"
other: "%{count} seconds"
+ x_seconds_abbreviated:
+ one: "1 s"
+ other: "%{count} s"
units:
hour:
one: "hour"
diff --git a/lookbook/previews/open_project/common/duration_component_preview.rb b/lookbook/previews/open_project/common/duration_component_preview.rb
new file mode 100644
index 00000000000..63d9f3f729f
--- /dev/null
+++ b/lookbook/previews/open_project/common/duration_component_preview.rb
@@ -0,0 +1,22 @@
+module OpenProject
+ module Common
+ # @logical_path OpenProject/Common
+ class DurationComponentPreview < Lookbook::Preview
+ # @param duration number
+ # @param type select { choices: [seconds, minutes, hours, days, weeks, months, years] }
+ # @param separator text
+ # @param abbreviated toggle
+ def default(duration: 1234, type: :minutes, separator: ', ', abbreviated: false)
+ render OpenProject::Common::DurationComponent.new(duration, type, separator:, abbreviated:)
+ end
+
+ def system_arguments
+ render OpenProject::Common::DurationComponent.new(3625, :seconds, color: :subtle)
+ end
+
+ def iso8601
+ render OpenProject::Common::DurationComponent.new('P3DT12H5M', :seconds, color: :subtle)
+ end
+ end
+ end
+end
diff --git a/modules/meeting/app/components/meeting_agenda_items/item_component/show_component.html.erb b/modules/meeting/app/components/meeting_agenda_items/item_component/show_component.html.erb
index a61a9304e1d..8b1fbb3b6ab 100644
--- a/modules/meeting/app/components/meeting_agenda_items/item_component/show_component.html.erb
+++ b/modules/meeting/app/components/meeting_agenda_items/item_component/show_component.html.erb
@@ -39,7 +39,7 @@
if @meeting_agenda_item.duration_in_minutes.present? && @meeting_agenda_item.duration_in_minutes > 0
grid.with_area(:duration, Primer::Beta::Text, color: duration_color_scheme, mr: 1) do
- I18n.t('datetime.distance_in_words.x_min_abbreviated', count: @meeting_agenda_item.duration_in_minutes)
+ I18n.t('datetime.distance_in_words.x_minutes_abbreviated', count: @meeting_agenda_item.duration_in_minutes)
end
end
diff --git a/modules/meeting/app/components/meetings/sidebar/details_component.rb b/modules/meeting/app/components/meetings/sidebar/details_component.rb
index a617b913a64..87bda864f83 100644
--- a/modules/meeting/app/components/meetings/sidebar/details_component.rb
+++ b/modules/meeting/app/components/meetings/sidebar/details_component.rb
@@ -121,20 +121,10 @@ module Meetings
end
def duration_partial
- duration = Duration.new(seconds: @meeting.duration * 3600)
-
meeting_attribute_row(:stopwatch) do
flex_layout do |flex|
flex.with_column do
- if duration.hours > 0
- render(Primer::Beta::Text.new) do
- "#{duration.hours} h #{duration.minutes} min"
- end
- else
- render(Primer::Beta::Text.new) do
- "#{duration.minutes} min"
- end
- end
+ render OpenProject::Common::DurationComponent.new(@meeting.duration, :hours, abbreviated: true)
end
flex.with_column(ml: 2) do
render(Primer::Beta::Text.new(color: :danger, font_size: :small)) do
diff --git a/spec/components/open_project/common/duration_component_spec.rb b/spec/components/open_project/common/duration_component_spec.rb
new file mode 100644
index 00000000000..7442b65a478
--- /dev/null
+++ b/spec/components/open_project/common/duration_component_spec.rb
@@ -0,0 +1,72 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe OpenProject::Common::DurationComponent, type: :component do
+ let(:args) do
+ {}
+ end
+
+ subject { described_class.new(duration, type, **args) }
+
+ shared_examples 'renders a duration' do |duration, type, **args|
+ let(:duration) { duration }
+ let(:type) { type }
+ let(:args) { args }
+
+ it "renders #{duration} #{type}" do
+ expect(render_inline(subject).text).to eq(expected)
+ end
+ end
+
+ context 'with numeric duration' do
+ it_behaves_like 'renders a duration', 1, :minutes do
+ let(:expected) { '1 minute' }
+ end
+ it_behaves_like 'renders a duration', 20, :minutes do
+ let(:expected) { '20 minutes' }
+ end
+ it_behaves_like 'renders a duration', 63, :minutes do
+ let(:expected) { '1 hour, 3 minutes' }
+ end
+ it_behaves_like 'renders a duration', 125, :minutes, abbreviated: true do
+ let(:expected) { '2 hrs, 5 mins' }
+ end
+ end
+
+ context 'with ISO8601 duration' do
+ it_behaves_like 'renders a duration', 'P3DT12H25M' do
+ let(:expected) { '3 days, 12 hours, 25 minutes' }
+ end
+ end
+
+ context 'with AS::Duration' do
+ it_behaves_like 'renders a duration', 3612.seconds do
+ let(:expected) { '1 hour, 12 seconds' }
+ end
+
+ it_behaves_like 'renders a duration', 3680.seconds do
+ let(:expected) { '1 hour, 1 minute, 20 seconds' }
+ end
+ end
+
+ context 'when providing an invalid type' do
+ let(:duration) { 1234 }
+ let(:type) { :bogus }
+
+ it 'raises an error' do
+ expected = 'Provide known type (seconds, minutes, hours, days, weeks, months, years) ' \
+ 'when providing a number to this component.'
+ expect { subject }.to raise_error(ArgumentError, expected)
+ end
+ end
+
+ context 'when providing an invalid duration' do
+ let(:duration) { %w[what's this] }
+ let(:type) { :minutes }
+
+ it 'raises an error' do
+ expect { subject }.to raise_error(ArgumentError, 'Invalid duration type Array.')
+ end
+ end
+end