Skip to content
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

Are instance variables in cells overridden ? #1

Closed
kinokuni opened this issue May 14, 2009 · 5 comments
Closed

Are instance variables in cells overridden ? #1

kinokuni opened this issue May 14, 2009 · 5 comments

Comments

@kinokuni
Copy link

I have noticed that instance variables in cells are overridden
by those in the controllers of the same name while the cell is
being rendered from the template (although the variables in the cell
are used if the cell method returns string directly). Is this
the intended behavior? I think it would be better the
variables defined in cells are on the top.
The patch attached will make it so.

diff --git a/lib/cell/base.rb b/lib/cell/base.rb
index 26720b0..6f7ea95 100755
--- a/lib/cell/base.rb
+++ b/lib/cell/base.rb
@@ -195,7 +195,9 @@ module Cell
     def render_view_for_state(state)
       ### DISCUSS: create Cell::View directly? are there still problematic class vars in View::Base
       view_class  = Class.new(Cell::View)
-      action_view = view_class.new(@@view_paths, {}, @controller)
+      controller = @controller.dup
+      clone_ivars_to(controller)
+      action_view = view_class.new(@@view_paths, {}, controller)
       action_view.cell = self
       ### FIXME/DISCUSS:
       action_view.template_format = :html # otherwise it's set to :js in AJAX context!
@cowboyd
Copy link

cowboyd commented May 14, 2009

It seems to me that the variables in the controller should not be visible at all inside the cells. Isn't that better encapsulation? I.e. if it isn't passed as an option to the cell, then it isn't visible. I've actually been burned by this in the past where I had cells silently depending on variables set in the controller.

@apotonick
Copy link
Member

this is NOT the intended behaviour, but a serious bug! the cell view should never know anything about controller variables. guess the problem is inside ActionView::Base where controller variables are prefered in place of the passed local vars. damn- gotta fix that.
thanks for spotting

@apotonick
Copy link
Member

fixed in bf49226
please report if that works for you!

@kinokuni
Copy link
Author

I'm sorry for the late reply, but I tested the source
and I confirmed that it works as expected for me.
Thank you.

@apotonick
Copy link
Member

thank you, kinokuni.

one thing we have to take into account is that controller ivars with no same-named cell ivar will still be accessible in the cell view, since an unspecified set of controller ivars have to be around in the view, for helpers, so we have to copy them. sucks, but i don't see any way around that if we want to stay forward-compatible.

@giates giates mentioned this issue Dec 20, 2011
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants