README.md in access-granted-1.3.0 vs README.md in access-granted-1.3.1
- old
+ new
@@ -17,11 +17,13 @@
rails generate access_granted:policy
Add the `policies` (and `roles` if you're using it to split up your roles into files) directories to your autoload paths in `application.rb`:
- config.autoload_paths += %W(#{config.root}/app/policies #{config.root}/app/roles)
+```ruby
+config.autoload_paths += %W(#{config.root}/app/policies #{config.root}/app/roles)
+```
### Supported Ruby versions
Because it has **zero** runtime dependencies it is guaranteed to work on all major Ruby versions MRI 1.9.3-2.2, Rubinius >= 2.X and JRuby >= 1.7.
@@ -206,9 +208,29 @@
class ApplicationController < ActionController::Base
rescue_from "AccessGranted::AccessDenied" do |exception|
redirect_to root_path, alert: "You don't have permission to access this page."
end
end
+```
+
+You can also extract the action and subject which raised the error,
+if you want to handle authorization errors differently for some cases:
+```ruby
+ rescue_from "AccessGranted::AccessDenied" do |exception|
+ status = case exception.action
+ when :read # invocation like `authorize! :read, @something`
+ 403
+ else
+ 404
+ end
+
+ body = case exception.subject
+ when Post # invocation like `authorize! @some_action, Post`
+ "failed to access a post"
+ else
+ "failed to access something else"
+ end
+ end
```
#### Checking permissions in controllers
To check if the user has a permission to perform an action, use the `can?` and `cannot?` methods.