Permalink
Browse files

added dependent destroys for the principals / permissionables

  • Loading branch information...
1 parent fb2e750 commit 5f951ed07c4c5bf28d3f44fd42a72a9cf457b6c0 @matthewvermaak committed Dec 3, 2009
Showing with 32 additions and 1 deletion.
  1. +2 −0 lib/sanction/permissionable/base.rb
  2. +2 −0 lib/sanction/principal/base.rb
  3. +1 −1 lib/sanction/version.rb
  4. +27 −0 test/unit/sanction_test.rb
View
2 lib/sanction/permissionable/base.rb
@@ -5,6 +5,8 @@ def self.extended(base)
base.class_eval %q{
has_many :permissionable_roles, :as => :permissionable, :class_name => "Sanction::Role",
:finder_sql => 'SELECT * FROM roles WHERE roles.permissionable_type = "#{self.class.name.to_s}" AND (roles.permissionable_id = "#{id}" OR roles.permissionable_id IS NULL)'
+
+ has_many :specific_permissionable_roles, :as => :permissionable, :class_name => "Sanction::Role", :dependent => :destroy
}
base.named_scope :as_permissionable_self, lambda {
View
2 lib/sanction/principal/base.rb
@@ -5,6 +5,8 @@ def self.extended(base)
base.class_eval %q{
has_many :principal_roles, :as => :principal, :class_name => "Sanction::Role",
:finder_sql => 'SELECT * FROM roles WHERE roles.principal_type = "#{self.class.name.to_s}" AND (roles.principal_id = "#{id}" OR roles.principal_id IS NULL)'
+
+ has_many :specific_principal_roles, :as => :principal, :class_name => "Sanction::Role", :dependent => :destroy
}
base.named_scope :as_principal_self, lambda {
View
2 lib/sanction/version.rb
@@ -1,7 +1,7 @@
module Sanction
module Version
MAJOR = 1
- MINOR = 0
+ MINOR = 1
TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
View
27 test/unit/sanction_test.rb
@@ -443,4 +443,31 @@ def test_instance_unauthorize_should_not_result_in_universal_deletion
assert_raise(Sanction::Role::Error::UnknownPermissionable) { Magazine.new.unauthorize(:reader, Person) }
assert_raise(Sanction::Role::Error::UnknownPrincipal) { Magazine.unauthorize(:reader, Person.new) }
end
+
+ def test_removal_of_principal_instance
+ assert Person.grant(:reader, Magazine)
+
+ person = Person.create
+ assert person.grant(:editor, Magazine)
+ assert Sanction::Role.count(:all) == 2
+
+ person.destroy
+
+ assert Sanction::Role.count(:all) == 1
+ assert Person.revoke(:reader, Magazine)
+ assert Sanction::Role.count(:all) == 0
+ end
+
+ def test_removal_of_permissionable_instance
+ assert Magazine.authorize(:reader, Person)
+
+ magazine = Magazine.create
+ assert magazine.authorize(:editor, Person)
+
+ magazine.destroy
+
+ assert Sanction::Role.count(:all) == 1
+ assert Magazine.unauthorize(:reader, Person)
+ assert Sanction::Role.count(:all) == 0
+ end
end

0 comments on commit 5f951ed

Please sign in to comment.