Class: PlanLimits

Inherits:
ApplicationRecord show all
Includes:
IgnorableColumns
Defined in:
app/models/plan_limits.rb

Constant Summary collapse

ALLOWED_LIMITS_HISTORY_ATTRIBUTES =
%i[notification_limit enforcement_limit storage_size_limit
dashboard_limit_enabled_at].freeze
LimitUndefinedError =
Class.new(StandardError)

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Instance Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Instance Method Details

#dashboard_storage_limit_enabled?Boolean

Overridden in EE

Returns:

  • (Boolean)


52
53
54
# File 'app/models/plan_limits.rb', line 52

def dashboard_storage_limit_enabled?
  false
end

#exceeded?(limit_name, subject, alternate_limit: 0) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/plan_limits.rb', line 22

def exceeded?(limit_name, subject, alternate_limit: 0)
  limit = limit_for(limit_name, alternate_limit: alternate_limit)
  return false unless limit

  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
    # if a record exists at the limit value - 1 position.
    subject.offset(limit - 1).exists?
  else
    raise ArgumentError, "#{subject.class} is not supported as a limit value"
  end
end

#format_limits_history(user, new_limits) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/plan_limits.rb', line 56

def format_limits_history(user, new_limits)
  allowed_limits = new_limits.slice(*ALLOWED_LIMITS_HISTORY_ATTRIBUTES)
  return {} if allowed_limits.empty?

  allowed_limits.each do |attribute, value|
    next if value == self[attribute]

    limits_history[attribute] ||= []
    limits_history[attribute] << {
      "user_id" => user.id,
      "username" => user.username,
      "timestamp" => Time.current.utc.to_i,
      "value" => value
    }
  end

  limits_history
end

#limit_for(limit_name, alternate_limit: 0) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'app/models/plan_limits.rb', line 41

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)

  limits = [limit, alternate_limit]
  limits.map(&:to_i).select(&:positive?).min
end