Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Namespaced controllers and Object is not missing constants XXX exception #530

Open
wants to merge 4 commits into from

5 participants

@fuelxc

This change seems more like a workaround than a fix but I am unable to correct any other way and my previous change for including :class => lambda{} doesn't seem to be working in all cases

@smashtank

added the passing of the controller to the lambda

@jeremyf
Collaborator

@fuelxc Unfortunately the pull requests don't cleanly merge against master. Would you be able to rebase this and ping me to verify once you have a clean merge?

@fuelxc fuelxc Merge branch 'master' of https://github.com/ryanb/cancan
* 'master' of https://github.com/ryanb/cancan:
  Clarify readme for rails 2.3 users
  adding a .rbenv-version file
  Add check for Enumerable as condition value
  adding project status message to readme
  Just add singleton to description of authorize_resource
  Don't remove key-value from the subject hash we might want to use it again.
  adding travis-ci badge
  Pass forward :if and :unless options to the before filter. i.e:
  Fixed bug with params for actions that build new instances with namespaced models
  Use latest with_model gem

Conflicts:
	spec/cancan/controller_resource_spec.rb
1141dab
@fuelxc

I merged and pushed now.

Cheers

@jeremyf
Collaborator

@ryanb The tests all pass. There are quite a few concepts going on, but they look good.

@andhapp
Collaborator

@fuelxc: Thanks for the pull request and sorry for such a long delay in merging this request. Can you please give us a real-world use case where you encountered this issue originally. Quite a few namespace related pull requests have been merged recently and I just wanted to find out the actual issue this one is solving.

Thanks.

@fuelxc

It was a fair bit ago, so I apologize that i can't remember exact details, but basically it was an STI and I was getting an exception "Object is not missing constants" on a namespaced controller. The only way I found to correct it was to use a lambda to determine what was needed on request.

@fuelxc

Out of curiosity has this been solved a separate way?

@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 Dec 12, 2011
  1. @fuelxc
Commits on Jan 3, 2012
  1. @fuelxc
  2. @fuelxc
Commits on May 11, 2012
  1. @fuelxc

    Merge branch 'master' of https://github.com/ryanb/cancan

    fuelxc authored
    * 'master' of https://github.com/ryanb/cancan:
      Clarify readme for rails 2.3 users
      adding a .rbenv-version file
      Add check for Enumerable as condition value
      adding project status message to readme
      Just add singleton to description of authorize_resource
      Don't remove key-value from the subject hash we might want to use it again.
      adding travis-ci badge
      Pass forward :if and :unless options to the before filter. i.e:
      Fixed bug with params for actions that build new instances with namespaced models
      Use latest with_model gem
    
    Conflicts:
    	spec/cancan/controller_resource_spec.rb
This page is out of date. Refresh to see the latest.
View
10 lib/cancan/controller_resource.rb
@@ -143,6 +143,7 @@ def resource_class
when false then name.to_sym
when nil then namespaced_name.to_s.camelize.constantize
when String then @options[:class].constantize
+ when Proc then klass = @options[:class].call(@controller); klass.is_a?(String) ? klass.constantize : klass
else @options[:class]
end
end
@@ -212,8 +213,13 @@ def name
def namespaced_name
@name || @params[:controller].sub("Controller", "").singularize.camelize.constantize
- rescue NameError
- name
+ rescue Exception => e
+ #Object is not missing Constant means we actually do have that model, un-namespaced
+ if [NameError,ArgumentError].include?(e.class)
+ name
+ else
+ raise e
+ end
end
def name_from_controller
View
16 spec/cancan/controller_resource_spec.rb
@@ -332,6 +332,22 @@ class Project < ::Project; end
@controller.instance_variable_get(:@project).should == project
end
+ it "should load the model using a custom class that is a proced constant" do
+ project = Project.create!
+ @params.merge!(:action => "show", :id => project.id)
+ resource = CanCan::ControllerResource.new(@controller, :class => lambda{ Project })
+ resource.load_resource
+ @controller.instance_variable_get(:@project).should == project
+ end
+
+ it "should load the model using a custom class that is a proced string" do
+ project = Project.create!
+ @params.merge!(:action => "show", :id => project.id)
+ resource = CanCan::ControllerResource.new(@controller, :class => lambda{ "Project" })
+ resource.load_resource
+ @controller.instance_variable_get(:@project).should == project
+ end
+
it "should load the model using a custom namespaced class" do
project = Sub::Project.create!
@params.merge!(:action => "show", :id => project.id)
Something went wrong with that request. Please try again.