580d250166
There are several changes to this module: 1. The use of an explicit stack in Participable#participants 2. Proc behaviour has been changed 3. Batch permissions checking == Explicit Stack Participable#participants no longer uses recursion to process "self" and all child objects, instead it uses an Array and processes objects in breadth-first order. This allows us to for example create a single Gitlab::ReferenceExtractor instance and pass this to any Procs. Re-using a ReferenceExtractor removes the need for running potentially many SQL queries every time a Proc is called on a new object. == Proc Behaviour Changed Previously a Proc in Participable was expected to return an Array of User instances. This has been changed and instead it's now expected that a Proc modifies the Gitlab::ReferenceExtractor passed to it. The return value of the Proc is ignored. == Permissions Checking The method Participable#participants uses Ability.users_that_can_read_project to check if the returned users have access to the project of "self" _without_ running multiple SQL queries for every user.
83 lines
2.1 KiB
Ruby
83 lines
2.1 KiB
Ruby
require 'spec_helper'
|
|
|
|
describe Participable, models: true do
|
|
let(:model) do
|
|
Class.new do
|
|
include Participable
|
|
end
|
|
end
|
|
|
|
describe '.participant' do
|
|
it 'adds the participant attributes to the existing list' do
|
|
model.participant(:foo)
|
|
model.participant(:bar)
|
|
|
|
expect(model.participant_attrs).to eq([:foo, :bar])
|
|
end
|
|
end
|
|
|
|
describe '#participants' do
|
|
it 'returns the list of participants' do
|
|
model.participant(:foo)
|
|
model.participant(:bar)
|
|
|
|
user1 = build(:user)
|
|
user2 = build(:user)
|
|
user3 = build(:user)
|
|
project = build(:project, :public)
|
|
instance = model.new
|
|
|
|
expect(instance).to receive(:foo).and_return(user2)
|
|
expect(instance).to receive(:bar).and_return(user3)
|
|
expect(instance).to receive(:project).twice.and_return(project)
|
|
|
|
participants = instance.participants(user1)
|
|
|
|
expect(participants).to include(user2)
|
|
expect(participants).to include(user3)
|
|
end
|
|
|
|
it 'supports attributes returning another Participable' do
|
|
other_model = Class.new { include Participable }
|
|
|
|
other_model.participant(:bar)
|
|
model.participant(:foo)
|
|
|
|
instance = model.new
|
|
other = other_model.new
|
|
user1 = build(:user)
|
|
user2 = build(:user)
|
|
project = build(:project, :public)
|
|
|
|
expect(instance).to receive(:foo).and_return(other)
|
|
expect(other).to receive(:bar).and_return(user2)
|
|
expect(instance).to receive(:project).twice.and_return(project)
|
|
|
|
expect(instance.participants(user1)).to eq([user2])
|
|
end
|
|
|
|
context 'when using a Proc as an attribute' do
|
|
it 'calls the supplied Proc' do
|
|
user1 = build(:user)
|
|
project = build(:project, :public)
|
|
|
|
user_arg = nil
|
|
ext_arg = nil
|
|
|
|
model.participant -> (user, ext) do
|
|
user_arg = user
|
|
ext_arg = ext
|
|
end
|
|
|
|
instance = model.new
|
|
|
|
expect(instance).to receive(:project).twice.and_return(project)
|
|
|
|
instance.participants(user1)
|
|
|
|
expect(user_arg).to eq(user1)
|
|
expect(ext_arg).to be_an_instance_of(Gitlab::ReferenceExtractor)
|
|
end
|
|
end
|
|
end
|
|
end
|