-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Presenting more specifically when using modules #7
Comments
Maybe I didn't fully comprehend the question, but if you have a namespaced class, it will attempt to get the namespaced presenter, check out the code here Maybe there are some loading issues? |
Yes, it seems the less specific one is used even if you have specified a more specific one. Sorry about the description, it seems I had two errors in one, the uninitialized was because my loading issues, they are solved now. I have a |
Ok now I see your problem. I will create a test case for that and check what's happening. Thanks! |
I'm having a hard time reproducing this in an isolated environment. Will see if I can isolate the issue and then it should be easier to make a test case for it. |
I'm just guessing, but are you presenting something like this? class Namespace::Controller
def index
present Model.new # which points to Namespace::Model
end
end |
No, unfortunately not. I have succeeded to narrow down the scope some more. I have setup a dummy rails project because that is the only place I have been able to reproduce it. The strange thing is it only breaks in the access of the page, not in tests. This is basically what I did to reproduce this:
|
The strange thing is this passes in the tests with correct presenters. But when visiting the page in the browser I get:
and the log file shows:
However if I switch place like this in the controller action:
Then it works! Insanely strange IMO. |
Try this: class BoxController < ActionController::Base
require_dependency 'app/presenters/fancy/box_presenter'
def index
@box = present Box.new
@fancy_box = present Fancy::Box.new
Rails.logger.info @box.class.name
Rails.logger.info @fancy_box.class.name
end
end |
This is clearly an autoload issue. Basically on
with
It works. The problem is with Rails autoload. Since the module Fancy is auto-created (it's not defined anywhere), when you call Object.const_get('Fancy').const_get('BoxPresenter')
=> BoxPresenter It returns the wrong constant. However if you run this at the top of your action, it works fine, because it triggers the load of Fancy module from Box presenter. |
I tried the require_dependency solution and it worked. So I guess this might be a bug with Rails then? However I solved it by renaming my classes which in some cases wouldn't be the right thing to do. How do we proceed from here? |
It's not a bug in Rails, it's just how it works - if you have this exact situation - and you have to rely on However there's a fix for the One clarification: since |
I just found out that this issue can be solved by doing the following. presenter.split('::').inject(Object) { |ns, cons| ns.const_get(cons, false) } http://ruby-doc.org/core-1.9.3/Module.html#method-i-const_get I am not sure if this is the desired behaviour in all cases. All the tests are passing with this change though, so I guess it is not a intended behavior? |
wow, nice finding! I just tested and you're right, it fixes this scenario with rails autoload. I couldn't write a failing test to justify this thought... Unless I require ActiveSupport and create a very specific scenario (which I think it doesn't make sense). If you have this on your fork, could you please open a PR? I'd just comment out the code on why this decision was taken, since we can't justify writing tests. Thanks again! |
I think it is kind of nice that
Some::Name::Space
falls back to the presenterSpacePresenter
. But if I had some more specific presenter, saySome::Name::SpacePresenter
it doesn't seem to recognise it.I get ther error:
uninitialized constant Some::Name::SpacePresenter
If I had the
SpacePresenter
it would use that one even if I had the more specificSome::Name::SpacePresenter
The text was updated successfully, but these errors were encountered: