diff options
Diffstat (limited to 'app/models/analytics/devops_adoption/segment.rb')
-rw-r--r-- | app/models/analytics/devops_adoption/segment.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/app/models/analytics/devops_adoption/segment.rb b/app/models/analytics/devops_adoption/segment.rb new file mode 100644 index 00000000000..71d4a312627 --- /dev/null +++ b/app/models/analytics/devops_adoption/segment.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class Analytics::DevopsAdoption::Segment < ApplicationRecord + ALLOWED_SEGMENT_COUNT = 20 + + has_many :segment_selections + has_many :groups, through: :segment_selections + + validates :name, presence: true, uniqueness: true, length: { maximum: 255 } + validate :validate_segment_count + + accepts_nested_attributes_for :segment_selections, allow_destroy: true + + scope :ordered_by_name, -> { order(:name) } + scope :with_groups, -> { preload(:groups) } + + 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 |