diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-02 12:08:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-02 12:08:35 +0300 |
commit | 94823a9248837114ec84582f83b642ec45fe68ad (patch) | |
tree | b8e9fec80a6df5af5449ab47e46a2570da183426 /app/models/analytics | |
parent | 26c50e0eb9e1513e60e3a38e538f25f682146ac6 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/analytics')
-rw-r--r-- | app/models/analytics/devops_adoption.rb | 6 | ||||
-rw-r--r-- | app/models/analytics/devops_adoption/segment.rb | 18 | ||||
-rw-r--r-- | app/models/analytics/devops_adoption/segment_selection.rb | 36 |
3 files changed, 60 insertions, 0 deletions
diff --git a/app/models/analytics/devops_adoption.rb b/app/models/analytics/devops_adoption.rb new file mode 100644 index 00000000000..ed5a5b16a6e --- /dev/null +++ b/app/models/analytics/devops_adoption.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +module Analytics::DevopsAdoption + def self.table_name_prefix + 'analytics_devops_adoption_' + end +end diff --git a/app/models/analytics/devops_adoption/segment.rb b/app/models/analytics/devops_adoption/segment.rb new file mode 100644 index 00000000000..e679d5ab71c --- /dev/null +++ b/app/models/analytics/devops_adoption/segment.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class Analytics::DevopsAdoption::Segment < ApplicationRecord + ALLOWED_SEGMENT_COUNT = 20 + + has_many :segment_selections + + validates :name, presence: true, uniqueness: true, length: { maximum: 255 } + validate :validate_segment_count + + private + + def validate_segment_count + if self.class.count >= ALLOWED_SEGMENT_COUNT + errors.add(:name, s_('DevopsAdoptionSegment|The maximum number of segments has been reached')) + end + end +end diff --git a/app/models/analytics/devops_adoption/segment_selection.rb b/app/models/analytics/devops_adoption/segment_selection.rb new file mode 100644 index 00000000000..6b70c13a773 --- /dev/null +++ b/app/models/analytics/devops_adoption/segment_selection.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +class Analytics::DevopsAdoption::SegmentSelection < ApplicationRecord + ALLOWED_SELECTIONS_PER_SEGMENT = 20 + + belongs_to :segment + belongs_to :project + belongs_to :group + + validates :segment, presence: true + validates :project, presence: { unless: :group } + validates :project_id, uniqueness: { scope: :segment_id, if: :project } + validates :group, presence: { unless: :project } + validates :group_id, uniqueness: { scope: :segment_id, if: :group } + + validate :exclusive_project_or_group + validate :validate_selection_count + + private + + def exclusive_project_or_group + if project.present? && group.present? + errors.add(:group, s_('DevopsAdoptionSegmentSelection|The selection cannot be configured for a project and for a group at the same time')) + end + end + + def validate_selection_count + return unless segment + + selection_count_for_segment = self.class.where(segment: segment).count + + if selection_count_for_segment >= ALLOWED_SELECTIONS_PER_SEGMENT + errors.add(:segment, s_('DevopsAdoptionSegmentSelection|The maximum number of selections has been reached')) + end + end +end |