Commit 0fc3b41c authored by charlie ablett's avatar charlie ablett

Merge branch '296686-move-useful-helper-methods-into-model' into 'master'

Move simple, useful methods from helper to model

See merge request gitlab-org/gitlab!51414
parents 53e03973 711b373a
......@@ -13,7 +13,7 @@ module TrialStatusWidgetHelper
end
def trial_days_remaining_in_words(group)
num_of_days = trial_days_remaining(group)
num_of_days = group.trial_days_remaining
plan_title = group.gitlab_subscription&.plan_title
ns_(
......@@ -22,21 +22,4 @@ module TrialStatusWidgetHelper
num_of_days
) % { plan: plan_title, num: num_of_days, en_dash: '–' }
end
def trial_days_remaining(group)
(group.trial_ends_on - Date.current).to_i
end
def total_trial_duration(group)
(group.trial_ends_on - group.trial_starts_on).to_i
end
def trial_days_used(group)
total_trial_duration(group) - trial_days_remaining(group)
end
# A value between 0 & 100 rounded to 2 decimal places
def trial_percentage_complete(group)
(trial_days_used(group) / total_trial_duration(group).to_f * 100).round(2)
end
end
......@@ -90,7 +90,9 @@ module EE
numericality: { only_integer: true, greater_than_or_equal_to: 0, allow_nil: true,
less_than: ::Gitlab::Pages::MAX_SIZE / 1.megabyte }
delegate :trial?, :trial_ends_on, :trial_starts_on, :upgradable?, to: :gitlab_subscription, allow_nil: true
delegate :trial?, :trial_ends_on, :trial_starts_on, :trial_days_remaining,
:trial_percentage_complete, :upgradable?,
to: :gitlab_subscription, allow_nil: true
before_create :sync_membership_lock_with_parent
......
......@@ -76,7 +76,7 @@ class GitlabSubscription < ApplicationRecord
def expired?
return false unless end_date
end_date < Date.today
end_date < Date.current
end
def upgradable?
......@@ -100,6 +100,22 @@ class GitlabSubscription < ApplicationRecord
seats_in_use_now
end
def trial_days_remaining
(trial_ends_on - Date.current).to_i
end
def trial_duration
(trial_ends_on - trial_starts_on).to_i
end
def trial_days_used
trial_duration - trial_days_remaining
end
def trial_percentage_complete(decimal_places = 2)
(trial_days_used / trial_duration.to_f * 100).round(decimal_places)
end
private
def seats_in_use_now
......
......@@ -4,4 +4,4 @@
#js-trial-status-widget{ data: { href: group_billings_path(group),
nav_icon_image_path: image_path('illustrations/golden_tanuki.svg'),
title: trial_days_remaining_in_words(group),
percentage_complete: trial_percentage_complete(group) } }
percentage_complete: group.trial_percentage_complete } }
......@@ -87,99 +87,4 @@ RSpec.describe TrialStatusWidgetHelper do
it { is_expected.to eq('Gold Trial – 13 days left') }
end
end
describe '#trial_days_remaining' do
subject { helper.trial_days_remaining(group) }
context 'at the beginning of a trial' do
before do
subscription.trial_starts_on = Date.current
subscription.trial_ends_on = Date.current.advance(days: 30)
end
it { is_expected.to eq(30) }
end
context 'in the middle of a trial' do
it { is_expected.to eq(15) }
end
context 'at the end of a trial' do
before do
subscription.trial_starts_on = Date.current.advance(days: -30)
subscription.trial_ends_on = Date.current
end
it { is_expected.to eq(0) }
end
end
describe '#total_trial_duration' do
subject { helper.total_trial_duration(group) }
context 'for a default trial duration' do
it { is_expected.to eq(30) }
end
context 'for a custom trial duration' do
before do
subscription.trial_starts_on = Date.current.advance(days: -5)
subscription.trial_ends_on = Date.current.advance(days: 5)
end
it { is_expected.to eq(10) }
end
end
describe '#trial_days_used' do
subject { helper.trial_days_used(group) }
context 'at the beginning of a trial' do
before do
subscription.trial_starts_on = Date.current
subscription.trial_ends_on = Date.current.advance(days: 30)
end
it { is_expected.to eq(0) }
end
context 'in the middle of a trial' do
it { is_expected.to eq(15) }
end
context 'at the end of a trial' do
before do
subscription.trial_starts_on = Date.current.advance(days: -30)
subscription.trial_ends_on = Date.current
end
it { is_expected.to eq(30) }
end
end
describe '#trial_percentage_complete' do
subject { helper.trial_percentage_complete(group) }
context 'at the beginning of a trial' do
before do
subscription.trial_starts_on = Date.current
subscription.trial_ends_on = Date.current.advance(days: 30)
end
it { is_expected.to eq(0.0) }
end
context 'in the middle of a trial' do
it { is_expected.to eq(50.0) }
end
context 'at the end of a trial' do
before do
subscription.trial_starts_on = Date.current.advance(days: -30)
subscription.trial_ends_on = Date.current
end
it { is_expected.to eq(100.0) }
end
end
end
......@@ -22,6 +22,9 @@ RSpec.describe Namespace do
it { is_expected.to delegate_method(:shared_runners_seconds_last_reset).to(:namespace_statistics) }
it { is_expected.to delegate_method(:trial?).to(:gitlab_subscription) }
it { is_expected.to delegate_method(:trial_ends_on).to(:gitlab_subscription) }
it { is_expected.to delegate_method(:trial_starts_on).to(:gitlab_subscription) }
it { is_expected.to delegate_method(:trial_days_remaining).to(:gitlab_subscription) }
it { is_expected.to delegate_method(:trial_percentage_complete).to(:gitlab_subscription) }
it { is_expected.to delegate_method(:upgradable?).to(:gitlab_subscription) }
it { is_expected.to delegate_method(:email).to(:owner).with_prefix.allow_nil }
it { is_expected.to delegate_method(:additional_purchased_storage_size).to(:namespace_limit) }
......
......@@ -275,7 +275,7 @@ RSpec.describe GitlabSubscription do
subject { gitlab_subscription.expired? }
context 'when end_date is expired' do
let(:end_date) { Date.yesterday }
let(:end_date) { Date.current.advance(days: -1) }
it { is_expected.to be(true) }
end
......@@ -493,4 +493,116 @@ RSpec.describe GitlabSubscription do
)
end
end
context 'when in a trial' do
let_it_be(:gitlab_subscription, reload: true) { create(:gitlab_subscription, :active_trial) }
let(:start_trial_on) { nil }
let(:end_trial_on) { nil }
before do
gitlab_subscription.trial_starts_on = start_trial_on if start_trial_on
gitlab_subscription.trial_ends_on = end_trial_on if end_trial_on
end
describe '#trial_days_remaining' do
subject { gitlab_subscription.trial_days_remaining }
context 'at the beginning of a trial' do
let(:start_trial_on) { Date.current }
let(:end_trial_on) { Date.current.advance(days: 30) }
it { is_expected.to eq(30) }
end
context 'in the middle of a trial' do
it { is_expected.to eq(15) }
end
context 'at the end of a trial' do
let(:start_trial_on) { Date.current.advance(days: -30) }
let(:end_trial_on) { Date.current }
it { is_expected.to eq(0) }
end
end
describe '#trial_duration' do
subject { gitlab_subscription.trial_duration }
context 'for a default trial duration' do
it { is_expected.to eq(30) }
end
context 'for a custom trial duration' do
let(:start_trial_on) { Date.current.advance(days: -5) }
let(:end_trial_on) { Date.current.advance(days: 5) }
it { is_expected.to eq(10) }
end
end
describe '#trial_days_used' do
subject { gitlab_subscription.trial_days_used }
context 'at the beginning of a trial' do
let(:start_trial_on) { Date.current }
let(:end_trial_on) { Date.current.advance(days: 30) }
it { is_expected.to eq(0) }
end
context 'in the middle of a trial' do
it { is_expected.to eq(15) }
end
context 'at the end of a trial' do
let(:start_trial_on) { Date.current.advance(days: -30) }
let(:end_trial_on) { Date.current }
it { is_expected.to eq(30) }
end
end
describe '#trial_percentage_complete' do
subject { gitlab_subscription.trial_percentage_complete }
context 'at the beginning of a trial' do
let(:start_trial_on) { Date.current }
let(:end_trial_on) { Date.current.advance(days: 30) }
it { is_expected.to eq(0.0) }
end
context 'in the middle of a trial' do
it { is_expected.to eq(50.0) }
end
context 'at the end of a trial' do
let(:start_trial_on) { Date.current.advance(days: -30) }
let(:end_trial_on) { Date.current }
it { is_expected.to eq(100.0) }
end
context 'rounding' do
let(:start_trial_on) { Date.current.advance(days: -10) }
let(:end_trial_on) { Date.current.advance(days: 20) }
context 'by default' do
it 'rounds to 2 decimal places' do
is_expected.to eq(33.33)
end
end
context 'with custom rounding options' do
subject { gitlab_subscription.trial_percentage_complete(4) }
it 'rounds to the given number of decimal places' do
is_expected.to eq(33.3333)
end
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment