Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

namespaced models weren't loading params #658

Open
wants to merge 3 commits into from

4 participants

@andhapp
Collaborator

Pull request #528 and #524 did not apply cleanly to master. This pull request takes the fix in #528 and rebases it against current master.

@ryanb
Owner

It looks like this will no longer merge cleanly since #670. Do we still need this with that fix? Also only checking the minor version of ActiveModel seems strange to me.

@andhapp
Collaborator

@ryanb: Okay. My bad, I'll have a look at this again. May be we don't need this now.

@xhoy

Thanks for your submission! The ryanb/cancan repository has been inactive since Sep 06, 2013.
Since only Ryan himself has commit permissions, the CanCan project is on a standstill.

CanCan has many open issues, including missing support for Rails 4. To keep CanCan alive, an active fork exists at cancancommunity/cancancan. The new gem is cancancan. More info is available at #994.

If your pull request or issue is still applicable, it would be really appreciated if you resubmit it to CanCanCan.

We hope to see you on the other side!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 19, 2012
  1. @ericsullivan @andhapp

    ActiveModel::Naming for param_key & param_key option

    ericsullivan authored andhapp committed
  2. @andhapp

    Fix pull request 528.

    andhapp authored
  3. @andhapp

    Fix broken spec and had to undo the changes introduced in ryanb/cancan@

    andhapp authored
    …d5baed6. Haven't really investigated why this code fixes the bug that commit fixed already.
This page is out of date. Refresh to see the latest.
View
43 lib/cancan/controller_resource.rb
@@ -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
View
14 spec/cancan/controller_resource_spec.rb
@@ -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
5 spec/spec_helper.rb
@@ -58,3 +58,8 @@ def self.respond_to?(method, include_private = false)
end
end
end
+
+module Namespaced
+ class Model < SuperModel::Base
+ end
+end
Something went wrong with that request. Please try again.