Permalink
Browse files

Adding a delete method to remove a simple role

  • Loading branch information...
1 parent aec78af commit e2e5578d886eb641febcf8dfbf1ebdd6d4d77779 @PabloInsignia4u PabloInsignia4u committed with Sep 26, 2010
Showing with 55 additions and 0 deletions.
  1. +7 −0 lib/role_model/roles.rb
  2. +41 −0 spec/role_model_spec.rb
  3. +7 −0 spec/roles_spec.rb
View
7 lib/role_model/roles.rb
@@ -13,5 +13,12 @@ def <<(role)
model_instance.roles = super.to_a
self
end
+
+ def delete(role)
+ model_instance.roles = super.to_a
+ self
+ end
+
end
end
+
View
41 spec/role_model_spec.rb
@@ -180,6 +180,47 @@
end
end
+ describe "#delete" do
+ subject { model_class.new }
+
+ context "with roles :foo and :bar already assigned" do
+ before(:each) do
+ subject.roles = [:foo, :bar]
+ end
+
+ it "should delete a existing role given as a symbol" do
+ subject.roles.delete(:foo)
+ subject.roles.should_not include(:foo)
+ subject.should have(1).roles
+ end
+
+ it "should not delete a existing role if it is given as a string" do
+ subject.roles.delete('foo')
+ subject.roles.should include(:foo)
+ subject.should have(2).roles
+ end
+
+ it "should not change anything if a non existing role is given" do
+ subject.roles.delete(:third)
+ subject.roles.should include(:foo, :bar)
+ subject.should have(2).roles
+ end
+ end
+
+ context "without roles assigned" do
+ it "should have 0 roles if a role is given as a symbol" do
+ subject.roles.delete(:foo)
+ subject.should have(0).roles
+ end
+
+ it "should have 0 roles if a role is given as a string" do
+ subject.roles.delete('foo')
+ subject.should have(0).roles
+ end
+ end
+
+ end
+
context "query for an individual role" 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
View
7 spec/roles_spec.rb
@@ -16,4 +16,11 @@
subject << :baz
end
end
+
+ describe "#delete" do
+ it "should delete the given element to the model_instance.roles by re-assigning all roles" do
+ model_instance.should_receive(:roles=).with(array_including(:bar))
+ subject.delete :foo
+ end
+ end
end

0 comments on commit e2e5578

Please sign in to comment.