Skip to content

Commit

Permalink
add Member#some_roles_deletable? instead of changing deletable?
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Mar 18, 2024
1 parent eaa3997 commit fdd858d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/contracts/members/delete_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DeleteContract < ::DeleteContract
private

def member_is_deletable
errors.add(:base, :not_deletable) unless model.deletable?
errors.add(:base, :not_deletable) unless model.some_roles_deletable?
end
end
end
2 changes: 1 addition & 1 deletion app/contracts/work_package_members/delete_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DeleteContract < ::DeleteContract
private

def member_is_deletable
errors.add(:base, :not_deletable) unless model.deletable?
errors.add(:base, :not_deletable) unless model.some_roles_deletable?
end
end
end
4 changes: 4 additions & 0 deletions app/models/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def <=>(other)
end

def deletable?
member_roles.none?(&:inherited_from?)
end

def some_roles_deletable?
!member_roles.all?(&:inherited_from?)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/contracts/members/delete_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

context 'when member is not deletable' do
before do
allow(member).to receive(:deletable?).and_return(false)
allow(member).to receive(:some_roles_deletable?).and_return(false)
end

context 'for admin' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

context 'when member is not deletable' do
before do
allow(member).to receive(:deletable?).and_return(false)
allow(member).to receive(:some_roles_deletable?).and_return(false)
end

context 'for admin' do
Expand Down
26 changes: 24 additions & 2 deletions spec/models/member_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,39 @@
end

describe '#deletable?' do
it 'is deletable, when no roles are inherited' do
it 'is true, when no roles are inherited' do
member.member_roles.first.inherited_from = nil
expect(member).to be_deletable
end

it 'is not deletable, when roles are inherited' do
it 'is false, when roles are inherited' do
member.member_roles.first.inherited_from = 1
expect(member).not_to be_deletable
end
end

describe '#some_roles_deletable?' do
before do
member.roles << project_role
end

it 'is true, when no roles are inherited' do
member.member_roles.first.inherited_from = nil
expect(member).to be_some_roles_deletable
end

it 'is true, when not all roles are inherited' do
member.member_roles.first.inherited_from = 1
expect(member).to be_some_roles_deletable
end

it 'is false, when all roles are inherited' do
member.member_roles.first.inherited_from = 1
member.member_roles.second.inherited_from = 1
expect(member).not_to be_some_roles_deletable
end
end

describe '#deletable_role?' do
it 'can delete directly assigned roles, but not if role is inherited through a group membership' do
# user has the global_role by directly being assigned
Expand Down

0 comments on commit fdd858d

Please sign in to comment.