load and authorise the resource with the same parameters as the Can Can controller additions class macro load_and_authorize_resource.
This differs in that it is an instance method, so you must call it in a before_filter (or from another instance method).
This method has no effect if the resource is already loaded.
Any block passed to this method will be executed if the resource was successfully loaded by this call. Nested calls automatically supply the :through
parameter. To bypass this, set :through => nil. rubocop:disable
def load_and_authorize(name, options = {})
@_through_stack ||= []
resource = instance_variable_get("@#{name}")
if resource.nil?
proceed = true
proceed &&= [*options[:only]].include?(action_name.to_sym) if options[:only]
proceed &&= ![*options[:except]].include?(action_name.to_sym) if options[:except]
proceed &&= case options[:if]
when Symbol
send(options[:if])
when Proc
options[:if].call
when nil
true
end
if proceed
options[:through] = @_through_stack.last unless @_through_stack.empty? || options.include?(:through)
cancan = self.class.cancan_resource_class.new(self, name, options.except(:if, :only, :except, :param))
resource = cancan.load_resource
cancan.authorize_resource unless options[:skip_authorize]
if resource && block_given?
begin
@_through_stack.push(name)
yield
ensure
@_through_stack.pop
end
end
end
end
resource
end