Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 49 lines (40 sloc) 1.447 kb
44b36ce @ryanb adding controller additions with basic behavior.
authored
1 module CanCan
2 module ControllerAdditions
3 def self.included(base)
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
4 base.helper_method :can?, :cannot?
44b36ce @ryanb adding controller additions with basic behavior.
authored
5 end
6
7 def unauthorized!
8 raise AccessDenied, "You are unable to access this page."
9 end
10
11 def current_ability
1edf583 @ryanb BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' t…
authored
12 ::Ability.new(current_user)
44b36ce @ryanb adding controller additions with basic behavior.
authored
13 end
14
15 def can?(*args)
16 (@current_ability ||= current_ability).can?(*args)
17 end
1034c81 @ryanb adding a before filter for loading and authorizing a resource
authored
18
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
19 def cannot?(*args)
20 (@current_ability ||= current_ability).cannot?(*args)
21 end
22
1034c81 @ryanb adding a before filter for loading and authorizing a resource
authored
23 def load_resource # TODO this could use some refactoring
7b299b5 @ryanb fix resource loading for new action
authored
24 unless params[:action] == "index"
25 if params[:id]
26 instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.find(params[:id]))
27 else
28 instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.new(params[params[:controller].singularize.to_sym]))
29 end
1034c81 @ryanb adding a before filter for loading and authorizing a resource
authored
30 end
31 end
32
33 def authorize_resource # TODO this could use some refactoring
34 unauthorized! unless can?(params[:action].to_sym, instance_variable_get("@#{params[:controller].singularize}") || params[:controller].singularize.camelcase.constantize)
35 end
36
37 def load_and_authorize_resource
38 load_resource
39 authorize_resource
40 end
44b36ce @ryanb adding controller additions with basic behavior.
authored
41 end
42 end
43
aaed265 @ryanb turning into a funtioning Rails plugin
authored
44 if defined? ActionController
45 ActionController::Base.class_eval do
46 include CanCan::ControllerAdditions
47 end
1edf583 @ryanb BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' t…
authored
48 end
Something went wrong with that request. Please try again.