Added #has_only_roles? and #is_exactly? methods.

This commit is contained in:
James McCarthy 2012-05-28 20:05:14 +01:00
parent 643f3d1a13
commit aef2eb0e35
2 changed files with 43 additions and 0 deletions

View file

@ -44,5 +44,20 @@ module RoleModel
alias_method :is_any_of?, :has_any_role?
alias_method :has_role?, :has_any_role?
# :call-seq:
# has_only_roles?(:role)
# has_only_roles?('role')
# has_only_roles?(:role_1, ..., :role_n)
# has_only_roles?('role_1', ..., 'role_n')
# has_only_roles?([:role_1, ..., :role_n])
# has_only_roles?(['role_1', ..., 'role_n'])
#
# check if ONLY of the given roles have been assigned
# this method is aliased as #is_exactly?
def has_only_roles?(*roles)
self.send("#{self.class.roles_attribute_name}") == self.class.mask_for(*roles)
end
alias_method :is_exactly?, :has_only_roles?
end
end

View file

@ -318,6 +318,11 @@ describe RoleModel do
describe "##{check_role_assignment_method}" do
subject { model_class.new }
it "returns true when the assigned roles include the given roles" do
subject.roles = [:foo, :bar, :third]
subject.send(check_role_assignment_method, :foo, :bar).should be_true
end
it "should return true when all of the given roles were assigned" do
subject.roles = [:foo, :bar]
subject.send(check_role_assignment_method, :foo, :bar).should be_true
@ -336,6 +341,29 @@ describe RoleModel do
end
end
context "query for exact roles" do
[:has_only_roles?, :is_exactly?].each do |check_role_assignment_method|
describe "##{check_role_assignment_method}" do
subject { model_class.new }
it "returns false when the given roles are a subset of those assigned" do
subject.roles = [:foo, :bar, :third]
subject.send(check_role_assignment_method, :foo, :bar).should be_false
end
it "returns true when the given roles exactly match those assigned" do
subject.roles = [:foo, :bar]
subject.send(check_role_assignment_method, :foo, :bar).should be_true
end
it "should return false when only some of the given roles were assigned" do
subject.roles = [:foo, :bar]
subject.send(check_role_assignment_method, :bar, :baz).should be_false
end
end
end
end
context "query for roles when none defined in model" do
[:has_any_role?, :is_any_of?, :has_role?, :has_all_roles?, :is?, :has_roles?].each do |check_role_assignment_method|
describe "##{check_role_assignment_method}" do