summaryrefslogtreecommitdiff
path: root/app/models/concerns/limitable.rb
blob: f320f54bb828acf9e3f18ab95b8f09f9ac59905e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# frozen_string_literal: true

module Limitable
  extend ActiveSupport::Concern

  included do
    class_attribute :limit_scope
    class_attribute :limit_name
    self.limit_name = self.name.demodulize.tableize

    validate :validate_plan_limit_not_exceeded, on: :create
  end

  private

  def validate_plan_limit_not_exceeded
    scope_relation = self.public_send(limit_scope) # rubocop:disable GitlabSecurity/PublicSend
    return unless scope_relation

    relation = self.class.where(limit_scope => scope_relation)

    if scope_relation.actual_limits.exceeded?(limit_name, relation)
      errors.add(:base, _("Maximum number of %{name} (%{count}) exceeded") %
        { name: limit_name.humanize(capitalize: false), count: scope_relation.actual_limits.public_send(limit_name) }) # rubocop:disable GitlabSecurity/PublicSend
    end
  end
end