Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a way to pass a specific message down through the stack.

I have no clue how to test this, but to use it:

    rescue_from Canable::Transgression, transgression

    def transgression(exception)
      flash[:notice] = exception.message
      redirect_to root_url
    end
  • Loading branch information...
commit 56e8f2168e6ffa7b6619bdb0d56c8dbff3dab204 1 parent 17b2dd6
@lenary lenary authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.