Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Some refactor to be more DRY

  • Loading branch information...
commit e92a7d8bf4c9240edd6ec03d739f6bceacb86607 1 parent c40490d
Rafael Silva authored committed
Showing with 21 additions and 10 deletions.
  1. +21 −10 lib/cancan/controller_additions.rb
View
31 lib/cancan/controller_additions.rb
@@ -83,14 +83,7 @@ def cannot?(*args)
# before_filter :load_resource
#
def load_resource # TODO this could use some refactoring
- model_name = params[:controller].split('/').last.singularize
- unless params[:action] == "index"
- if params[:id]
- instance_variable_set("@#{model_name}", model_name.camelcase.constantize.find(params[:id]))
- else
- instance_variable_set("@#{model_name}", model_name.camelcase.constantize.new(params[model_name.to_sym]))
- end
- end
+ self.model_instance = params[:id] ? model_class.find(params[:id]) : model_class.new(params[model_name.to_sym]) unless params[:action] == "index"
end
# Authorizes the resource in the current instance variable. For example,
@@ -106,8 +99,7 @@ def load_resource # TODO this could use some refactoring
#
# See load_and_authorize_resource to automatically load the resource too.
def authorize_resource # TODO this could use some refactoring
- model_name = params[:controller].split('/').last.singularize
- unauthorized! if cannot?(params[:action].to_sym, instance_variable_get("@#{model_name}") || model_name.camelcase.constantize)
+ unauthorized! if cannot?(params[:action].to_sym, model_instance || model_class)
end
# Calls load_resource to load the current resource model into an instance variable.
@@ -120,6 +112,25 @@ def load_and_authorize_resource
load_resource
authorize_resource
end
+
+ private
+
+ def model_name
+ params[:controller].split('/').last.singularize
+ end
+
+ def model_class
+ model_name.camelcase.constantize
+ end
+
+ def model_instance
+ instance_variable_get("@#{model_name}")
+ end
+
+ def model_instance=(instance)
+ instance_variable_set("@#{model_name}", instance)
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.