Permalink
Browse files

Make applied_roles get destroyed if either their associated subject o…

…r resource is destroyed.
  • Loading branch information...
1 parent c0a741b commit 253559d3f54215bc4f245c8342dae3a949549fca @mcrowe committed May 23, 2012
Showing with 29 additions and 9 deletions.
  1. +1 −1 lib/roleable/resource.rb
  2. +1 −1 lib/roleable/subject.rb
  3. +16 −6 spec/roleable/resource_spec.rb
  4. +11 −1 spec/roleable/subject_spec.rb
View
@@ -1,7 +1,7 @@
module Roleable::Resource
def self.included(base)
- base.has_many :applied_roles, :as => :resource
+ base.has_many :applied_roles, :as => :resource, :dependent => :destroy
end
# Return a list of users that have the given role for this resource.
View
@@ -1,7 +1,7 @@
module Roleable::Subject
def self.included(base)
- base.has_many :applied_roles, :foreign_key => 'subject_id'
+ base.has_many :applied_roles, :foreign_key => 'subject_id', :dependent => :destroy
end
# Add a role to the subject scoped to the given resource or global if no resource given.
@@ -4,13 +4,13 @@
include_context 'with models'
- describe '#subjects_with_role' do
+ before do
+ @page = Page.create
+ @editor_role = Role.create(:name => 'editor')
+ @author_role = Role.create(:name => 'author')
+ end
- before do
- @page = Page.create
- @editor_role = Role.create(:name => 'editor')
- @author_role = Role.create(:name => 'author')
- end
+ describe '#subjects_with_role' do
context 'with a single role' do
@@ -60,5 +60,15 @@
end
end
+
+ describe '#destroy' do
+
+ it 'destroys any associated applied_roles' do
+ User.create.add_role(:editor, @page)
+
+ expect { @page.destroy }.to change { AppliedRole.count}.by(-1)
+ end
+
+ end
end
@@ -208,4 +208,14 @@
end
end
-end
+ describe '#destroy' do
+
+ it 'destroys any associated applied_roles' do
+ @user.add_role(:admin)
+
+ expect { @user.destroy }.to change { AppliedRole.count}.by(-1)
+ end
+
+ end
+
+end

0 comments on commit 253559d

Please sign in to comment.