Skip to content
Browse files

Merge pull request #3 from lenary/messages

Customize transgression messages.
  • Loading branch information...
2 parents 17b2dd6 + 56e8f21 commit 21ef99b2e838eb3e1f81f1bb3fdf5e0260bf8c49 @jnunemaker committed Jun 30, 2011
Showing with 15 additions and 2 deletions.
  1. +2 −2 lib/canable.rb
  2. +13 −0 test/test_enforcers.rb
View
4 lib/canable.rb
@@ -70,8 +70,8 @@ def can_#{can}?(resource)
current_user.can_#{can}?(resource)
end
- def enforce_#{can}_permission(resource)
- raise Canable::Transgression unless can_#{can}?(resource)
+ def enforce_#{can}_permission(resource, message="")
+ raise(Canable::Transgression, message) unless can_#{can}?(resource)
end
private :enforce_#{can}_permission
EOM
View
13 test/test_enforcers.rb
@@ -20,6 +20,10 @@ def show
def update
enforce_update_permission(article)
end
+
+ def edit
+ enforce_update_permission(article, "You Can't Edit This")
+ end
end
@article = mock('article')
@@ -43,5 +47,14 @@ def update
@controller.current_user = nil
assert_raises(Canable::Transgression) { @controller.update }
end
+
+ should "pass message around the stack" do
+ @controller.current_user = nil
+ begin
+ @controller.edit
+ rescue Canable::Transgression => e
+ assert_equal e.message, "You Can't Edit This"
+ end
+ end
end
end

0 comments on commit 21ef99b

Please sign in to comment.
Something went wrong with that request. Please try again.