diff options
author | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 22:34:23 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 22:34:23 +0300 |
commit | 6438df3a1e0fb944485cebf07976160184697d72 (patch) | |
tree | 00b09bfd170e77ae9391b1a2f5a93ef6839f2597 /app/models/onboarding_progress.rb | |
parent | 42bcd54d971da7ef2854b896a7b34f4ef8601067 (diff) |
Add latest changes from gitlab-org/gitlab@13-8-stable-eev13.8.0-rc42
Diffstat (limited to 'app/models/onboarding_progress.rb')
-rw-r--r-- | app/models/onboarding_progress.rb | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/app/models/onboarding_progress.rb b/app/models/onboarding_progress.rb new file mode 100644 index 00000000000..419bbd595e9 --- /dev/null +++ b/app/models/onboarding_progress.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +class OnboardingProgress < ApplicationRecord + belongs_to :namespace, optional: false + + validate :namespace_is_root_namespace + + ACTIONS = [ + :git_pull, + :git_write, + :merge_request_created, + :pipeline_created, + :user_added, + :trial_started, + :subscription_created, + :required_mr_approvals_enabled, + :code_owners_enabled, + :scoped_label_created, + :security_scan_enabled, + :issue_auto_closed, + :repository_imported, + :repository_mirrored + ].freeze + + class << self + def onboard(namespace) + return unless root_namespace?(namespace) + + safe_find_or_create_by(namespace: namespace) + end + + def register(namespace, action) + return unless root_namespace?(namespace) && ACTIONS.include?(action) + + action_column = column_name(action) + onboarding_progress = find_by(namespace: namespace, action_column => nil) + onboarding_progress&.update!(action_column => Time.current) + end + + def completed?(namespace, action) + return unless root_namespace?(namespace) && ACTIONS.include?(action) + + action_column = column_name(action) + where(namespace: namespace).where.not(action_column => nil).exists? + end + + private + + def column_name(action) + :"#{action}_at" + end + + def root_namespace?(namespace) + namespace && namespace.root? + end + end + + private + + def namespace_is_root_namespace + return unless namespace + + errors.add(:namespace, _('must be a root namespace')) if namespace.has_parent? + end +end |