debian-mirror-gitlab/app/models/plan_limits.rb

41 lines
1.3 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
class PlanLimits < ApplicationRecord
2021-09-30 23:02:18 +05:30
include IgnorableColumns
ignore_column :ci_max_artifact_size_running_container_scanning, remove_with: '14.3', remove_after: '2021-08-22'
2020-07-28 23:09:34 +05:30
LimitUndefinedError = Class.new(StandardError)
2020-05-24 23:13:21 +05:30
belongs_to :plan
2020-07-28 23:09:34 +05:30
def exceeded?(limit_name, subject, alternate_limit: 0)
limit = limit_for(limit_name, alternate_limit: alternate_limit)
return false unless limit
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
case subject
when Integer
subject >= limit
when ActiveRecord::Relation
# We intentionally not accept just plain ApplicationRecord classes to
# enforce the subject to be scoped down to a relation first.
#
# subject.count >= limit value is slower than checking
2020-05-24 23:13:21 +05:30
# if a record exists at the limit value - 1 position.
2020-07-28 23:09:34 +05:30
subject.offset(limit - 1).exists?
else
raise ArgumentError, "#{subject.class} is not supported as a limit value"
2020-05-24 23:13:21 +05:30
end
end
2020-07-28 23:09:34 +05:30
def limit_for(limit_name, alternate_limit: 0)
limit = read_attribute(limit_name)
raise LimitUndefinedError, "The limit `#{limit_name}` is undefined" if limit.nil?
alternate_limit = alternate_limit.call if alternate_limit.respond_to?(:call)
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
limits = [limit, alternate_limit]
limits.map(&:to_i).select(&:positive?).min
2020-05-24 23:13:21 +05:30
end
end