Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

namespaced models weren't loading params #658

Open
wants to merge 3 commits into
from
@@ -82,7 +82,7 @@ def load_collection
end
def build_resource
- resource = resource_base.new(resource_params || {})
+ resource = resource_base.new(resource_params)
assign_attributes(resource)
end
@@ -94,9 +94,34 @@ def assign_attributes(resource)
resource
end
+ def param_key
+ @options.has_key?(:param_key) ? @options[:param_key] : name
+ end
+
+ def resource_param_key
+ if defined? ActiveModel
+ case ActiveModel::VERSION::MINOR
+ when 1
+ ActiveModel::Naming.param_key(resource_class)
+ when 0
+ ActiveModel::Naming.singular(resource_class)
+ end
+ end
+ end
+
+ def resource_params
+ if @params[param_key]
+ @params[param_key]
+ elsif resource_param_key && @params[resource_param_key]
+ @params[resource_param_key]
+ else
+ {}
+ end
+ end
+
def initial_attributes
current_ability.attributes_for(@params[:action].to_sym, resource_class).delete_if do |key, value|
- resource_params && resource_params.include?(key)
+ resource_params.include?(key)
end
end
@@ -211,20 +236,8 @@ def name
@name || name_from_controller
end
- def resource_params
- if @options[:class]
- @params[@options[:class].to_s.underscore.gsub('/', '_')]
- else
- @params[namespaced_name.to_s.underscore.gsub("/", "_")]
- end
- end
-
- def namespace
- @params[:controller].split("::")[0..-2]
- end
-
def namespaced_name
- [namespace, name.camelize].join('::').singularize.camelize.constantize
+ @name || @params[:controller].sub("Controller", "").singularize.camelize.constantize
rescue NameError
name
end
@@ -82,6 +82,20 @@ class Project < ::Project; end
@controller.instance_variable_get(:@project).name.should == "foobar"
end
+ it "should build a new resource with hash if params[:id] is not specified (using param_key)" do
+ @params.merge!(:action => "create", :project => {:name => "foobar"})
+ resource = CanCan::ControllerResource.new(@controller, :param_key => 'project')
+ resource.load_resource
+ @controller.instance_variable_get(:@project).name.should == "foobar"
+ end
+
+ it "should build a new resource for namespaced model with hash if params[:id] is not specified (using ActiveModel::Naming)" do
+ @params.merge!(:controller => 'namespaced/models', :action => "create", :namespaced_model => {:name => "foobar"})
+ resource = CanCan::ControllerResource.new(@controller)
+ resource.load_resource
+ @controller.instance_variable_get(:@model).name.should == "foobar"
+ end
+
it "should build a new resource with attributes from current ability" do
@params.merge!(:action => "new")
@ability.can(:create, Project, :name => "from conditions")
View
@@ -58,3 +58,8 @@ def self.respond_to?(method, include_private = false)
end
end
end
+
+module Namespaced
+ class Model < SuperModel::Base
+ end
+end