Commit 6f5c1e7c authored by Pedro Pombeiro's avatar Pedro Pombeiro

Allow custom relations in Limitable

parent a316a76b
......@@ -6,6 +6,7 @@ module Limitable
included do
class_attribute :limit_scope
class_attribute :limit_relation
class_attribute :limit_name
class_attribute :limit_feature_flag
self.limit_name = self.name.demodulize.tableize
......@@ -28,7 +29,7 @@ module Limitable
return unless scope_relation
return if limit_feature_flag && ::Feature.disabled?(limit_feature_flag, scope_relation, default_enabled: :yaml)
relation = self.class.where(limit_scope => scope_relation)
relation = limit_relation ? self.public_send(limit_relation) : self.class.where(limit_scope => scope_relation) # rubocop:disable GitlabSecurity/PublicSend
limits = scope_relation.actual_limits
check_plan_limit_not_exceeded(limits, relation)
......
# frozen_string_literal: true
require 'spec_helper'
require 'fast_spec_helper'
require 'active_model'
RSpec.describe Limitable do
let(:minimal_test_class) do
......@@ -35,6 +36,28 @@ RSpec.describe Limitable do
instance.valid?(:create)
end
context 'with custom relation' do
before do
MinimalTestClass.limit_relation = :custom_relation
end
it 'triggers custom limit_relation' do
instance = MinimalTestClass.new
def instance.project
@project ||= Object.new
end
limits = Object.new
custom_relation = Object.new
expect(instance).to receive(:custom_relation).and_return(custom_relation)
expect(instance.project).to receive(:actual_limits).and_return(limits)
expect(limits).to receive(:exceeded?).with(instance.class.name.demodulize.tableize, custom_relation).and_return(false)
instance.valid?(:create)
end
end
end
context 'with global limit' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment