lib/grape/cancan.rb in grape-cancan-0.0.1 vs lib/grape/cancan.rb in grape-cancan-0.0.2
- old
+ new
@@ -1,12 +1,30 @@
require 'grape'
require 'grape/cancan/version'
module Grape
module CanCan
- autoload :Helpers, 'grape/cancan/helpers'
- autoload :Authorizer, 'grape/cancan/authorizer'
+ module API
+ # Convienience method to authorize every route
+ def authorize_routes!
+ before { authorize_route! }
+ end
+ end
- Grape::Endpoint.send :include, Grape::CanCan::Helpers
- Grape::API.extend Grape::CanCan::Authorizer
+ module Endpoint
+ # Returns an instance of the CanCan ability
+ def current_ability
+ @current_ability ||= ::Ability.new(current_user)
+ end
+ delegate :can?, :cannot?, :authorize!, to: :current_ability
+
+ # Call authorize using the :authorize key on the route
+ def authorize_route!
+ opts = env['api.endpoint'].options[:route_options]
+ authorize!(*opts[:authorize]) if opts.key?(:authorize)
+ end
+ end
+
+ Grape::API.extend Grape::CanCan::API
+ Grape::Endpoint.send :include, Grape::CanCan::Endpoint
end
end