Which class, specifically, do view helpers get mixed into? - ruby-on-rails

Everything I read says that view helpers get mixed into views, but which class, specifically do they get mixed into?
References:
http://guides.rubyonrails.org/getting_started.html#view-helpers
Why can private helper methods still be accessed in views?
Do helper classes get mixed into the controller?

The controller has a view_context, which is an instance of view_context_class, which is (by default) an anonymous subclass of ActionView::Base created by ActionView::Base.prepare. The helpers are mixed in to these view context classes.
The view context is also the place where the controller instance variables "magically" become instance variables in the view.

Related

What is the scope of an instance variable in a Rails controller?

If I create an instance variable in a Rails controller, what is the scope of that instance variable? Is it available to all of the Rails application, or just to the views and models associated with that particular controller? Since I am new to Rails, I am a bit confused.
An instance variable in a Rails controller is used in two ways:
It is available within instance methods of that controller (including superclass instance methods), just like any other Ruby instance variable.
When a Rails controller action renders a view, the controller's instance variables are copied (shallowly) to the view instance. (The view instance is the value of self within a template.) Controller instance variables defined at the time an action renders are therefore effectively available within that action's view. View helpers are just modules which are extended by the view instance, so controller instance variables are effectively available within view helper methods too.
Note that although controller instance variables are copied to the view, instance variables defined in the view are not copied back to the controller. That's not something you'd normally need to have happen because rendering the view is normally the last thing done in a controller action. And local variables defined in a view are available throughout the view, so there's no reason at all to define an instance variable in a view.
An instance variable in a given controller is not available within other controllers, within other controllers' views, or within any models at all.

where can i find the class definition for action controller base?

I am just a bit curious how application controller and action controller objects work in rails. Is there anywhere I can view the complete class templates for these objects? I was wondering if methods like "show" and "index" are just hook methods that are called inside the initialize of the object. Is this why we define our instance members from these? Are they basically just empty methods called in the init that will then define the instance variables as the objects are selected from the routes.rb file?

Rails: How can an instance variable declared in a controller accessible in a view?

Ruby instance variables are accessible to a single object. But in rails, if I declare an instance variable in a controller, it is still accessible in the views. What is the architecture behind this?
Well, your controller calls render which renders your templates. So, the template code is being run within the scope of the controller instance. Therefore, you can use any instance variables declared.

What is the mechanism behind view helpers?

I wonder what approach Rails uses to find the correct method of a view helper. I recognized while calling a view helper method in a partial that the view helper must not belong to the same view nor must it have a similar name, the method is always found. If more than one view helper has the same method, there is some logic behind to find the "nearest" helper and use this method. Is that mechanism somewhere documented (or blogged about in detail)?
My guess would be:
The view class automatically includes the helper modules in the order of increasing priority("near"ness). The "nearest" helper module is included last, and its methods override any previously defined methods with the same name.

call a helper from another view

I know that if I want to call a helper of another controller, I can do something like:
helper :other_controllers
But I was wondering why I can't do something like OtherControllersHelper.method inside the view?
Due to the way that Rails loads your modules, you cannot do this without modification.
Rails includes the associated helper models into the ActionView::Base instance used to render a template. ActionController::Helpers#helper (used in the example above) adds more helper modules to the list of those to be included. The helper methods that are used in views are written as instance methods. Modules in Ruby do not provide any good ways of getting at instance methods without using a constructor. Which is one of the big things that separates modules from classes.
To access your helpers from another controller with just OtherControllersHelper.method, you will need to redefine method as a class method. However, redefining those methods as class methods would make them inaccessible from your views.
You could duplicate all instance methods in your helpers as class methods, but that's definitely not a better solution that adding helper :other_controllers. There are ways to define wrappers pragmatically, but again, it's not the best way to handle the situation.
If you've got a lot of helpers that are likely to be used in multiple controllers/views maybe you're better off putting them somewhere else. Somewhere like app/helpers/application_helper.rb. Or another helper module that could be loaded only in the controllers that need it.

Resources