Skip to content

Commit

Permalink
Merge pull request jnunemaker#3 from lenary/messages
Browse files Browse the repository at this point in the history
Customize transgression messages.
  • Loading branch information
jnunemaker committed Jun 30, 2011
2 parents 17b2dd6 + 56e8f21 commit 21ef99b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/canable.rb
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/test_enforcers.rb
Expand Up @@ -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')
Expand All @@ -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.