diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-22 18:09:27 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-22 18:09:27 +0300 |
commit | 7a7345366550f509c03595e0dada7cbd0d73103d (patch) | |
tree | ae2c4e74faa9f87237082abf4b23f325e34df6e3 /app/models/plan_limits.rb | |
parent | ae96e65ee23d81be3924b87ed16becbbbe002b91 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/plan_limits.rb')
-rw-r--r-- | app/models/plan_limits.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/app/models/plan_limits.rb b/app/models/plan_limits.rb new file mode 100644 index 00000000000..7f5c9b8b6b4 --- /dev/null +++ b/app/models/plan_limits.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class PlanLimits < ApplicationRecord + belongs_to :plan + + def exceeded?(limit_name, object) + return false unless enabled?(limit_name) + + if object.is_a?(Integer) + object >= read_attribute(limit_name) + else + # object.count >= limit value is slower than checking + # if a record exists at the limit value - 1 position. + object.limit(1).offset(read_attribute(limit_name) - 1).exists? + end + end + + private + + def enabled?(limit_name) + read_attribute(limit_name) > 0 + end +end |