summaryrefslogtreecommitdiff
path: root/lib/gitlab/patch/chronic_duration.rb
blob: ab3cba3657f0fc1f6f0ffb80d12256b277f477e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

# Fixes a bug where parsing months doesn't take into account
# the ChronicDuration.days_per_week setting
#
# We can remove this when we do a refactor and push upstream in
# https://gitlab.com/gitlab-org/gitlab-ce/issues/66637

module Gitlab
  module Patch
    module ChronicDuration
      extend ActiveSupport::Concern

      class_methods do
        def duration_units_seconds_multiplier(unit)
          return 0 unless duration_units_list.include?(unit)

          case unit
          when 'months'
            3600 * ::ChronicDuration.hours_per_day * ::ChronicDuration.days_per_month
          else
            super
          end
        end

        # ChronicDuration#output uses 1mo = 4w as the conversion so we do the same here.
        # We do need to add a special case for the default days_per_week value because
        # we want to retain existing behavior for the default case
        def days_per_month
          ::ChronicDuration.days_per_week == 7 ? 30 : ::ChronicDuration.days_per_week * 4
        end
      end
    end
  end
end