Helper directory in rails - ruby-on-rails

Will a file in helper directory included in all controllers?. I didn't find any good explanation regarding this. I have 2 custom directories in my controller( like admin, for normal user). Do I have same directory structure at my helper?. Is Helper name same as controller name only for readability?

By default all helper files under app/helpers are included in all controllers. As such, it doesn't matter how you structure what's inside helpers folder. If you really want to enforce controller to only include matching helper then set config.action_controller.include_all_helpers in config to false.
See comment section for details: https://github.com/rails/rails/blob/b5db73076914e7103466bd76bec785cbcfe88875/actionpack/lib/action_controller/metal/helpers.rb

Helper is just a ruby module which is openly available for views and controllers. You should never keep your code in helper if you do not want it to expose to views.
If you want to use helper methods for all your controller and views. Then you can add methods to application helper and include it to application controller. However if you don't want to expose methods to views, then you can use rails concerns. create a methods inside it and include it inside different controllers.
No helper do not name the same name only for readability. you still need to include inside your same name controller to call functions if you want to use it inside controller. But you can still use inside views methods with same name.

Related

Rails: Is it possible to organize code in a subfolder without create a namespace for it?

Rails uses a convention that each custom subfolder needs to became a module, for example:
--apps/
----blog/
------controllers
--------my_controller.rb
In the above case, to call the controller I need to call: Blog::Controllers::MyController
But what I want is to avoid use Controllers as a namespace, to call the controller is this way: Blog::MyController
Is it possible?
Some context: I'm working in a large codebase and we are starting to try to create a new module in a separate structure, similar how Shopify did.

How does Rails View Helper work

I recently came across a tricky situation related to rails view helpers.
The situation is like follows-
I am having a controller as Feature1::Feature1.1::Feature1.1.1Controller.
The Feature1.1 also includes other controllers like Feature1.1.2Controller, Feature1.1.3Controller...
So ofcourse related view helpers in folder app/helpers/feature1/feature1.1/...
Now the real problem I am facing is that a few helpers for feature1.1 includes the same method name method1 with related definition.
I was wondering how rails identifies all these helpers as I am noticing that the method1 i.e. being called in a view for the controller feature1.1.1 is using the definition of the method1 i.e. written for the controller feature1.1.2.
So does rails consider all helper modules defined in one folder as one?
In a view feature1/feature1.1/feature1.1.1/index I am making a method call for method1.
I am using rails3
It depends a little bit on your Rails version. With eralier Rails versions, Rails did only include application_helper.rb and <controler_name>_helper.rb.
Additional helper modules can be included via helper :helper_name1, :helper_name2, ... within your controller.
With later Rails verions (4.2.? and up, maybe previous versions too), Rails includes all helpers within your helper folder at once. You can set config.action_controller.include_all_helpers = false within application.rb and you will fall back to the old behaviour.
This makes the helper only available within your views. If you want to use a helper within your controller you still have to include your helper with include XXXHelper.
I did some research and would like to share some additional info.
As per #slowjack2k mentioned, view helpers are included by rails as a default behavior.
But my question was about the situation of same method names across multiple helpers.
I found this article to be useful in this scenario. Though it explains the behavior for Rails 4 but I found it behaves in the same fashion for Rails 3.2.2.
I will summarize the article -
If there will be any conflict in the same names of methods in different helper modules, rails will use method from latter file (alphabetically)

How to Include a Helper Method from a Rails Engine (Spree) inside of another Rails Engine (AlchemyCMS)

What I've run into is this:
AlchemyCMS is a Rails Engine for allowing Rails applications to have a Content Management System. It also has a preview page where it can load up an iframe of the example page with the layout. The layout here is the Spree layout. I've modified Alchemy to be able to load up the spree application layout and not its default.
In doing so, it is not loading up the helper methods. I am currently receiving:
undefined local variable or method `title' for #<#<Class:0x007f8dcc359498>:0x007f8de17dd6a8>
Where title is the first helper method in the application.
I've tried 5000 different techniques to try to load in Spree's helper methods into AlchemyCMS and I just can't do it.
Does anyone know how?
Ben,
You could do so by either including Spree's helpers within your application controller or within the base Alchemy controllers.
There is an extension for alchemy and spree together, which does a similar thing here:
https://github.com/magiclabs/alchemy_spree/blob/master/app/controllers/spree/base_controller_decorator.rb
You will just want to go in the opposite direction so instead of decorating a Spree controller to add Alchemy in you would decorate Alchemy controllers to include whichever of Spree's controller helpers you need to use:
https://github.com/spree/spree/blob/master/core/app/controllers/spree/base_controller.rb
In this case you need to include the common controller helpers:
https://github.com/spree/spree/blob/master/core/lib/spree/core/controller_helpers/common.rb
EDIT:
Alchemy::BaseController.class_eval do
include Spree::Core::ControllerHelpers
include Spree::Core::ControllerHelpers::Store
helper Spree::Core::Engine.helpers
end

Difference between helper files and lib files in rails

What exactly is the difference between helper files and lib files in rails? When should these files be used appropriately?
Helpers in Rails are used to organize helpers in the views. So you can create a method in some helper module, say:
module SomeModule
def markdown(string)
#some behaviuor
end
end
and then use it in the view: markdown("Hello world").
The Lib folder should keep the parts of your code that are not completely relevant to models, controller, helpers or views. Say you implement your own web crawler in separate class. It is better to keep it in lib/my_crawler.rb.
helpers is a mixins (modules) otherwise in /lib you can place classes and whole libraries

Is there a list of all RoR helpers used in view pages?

Is there a list of all RoR helpers used in view pages?
Can these helpers be used in both the view pages and action pages?
Let's first distinguish Helpers from helpers, in the context of Rails. Helpers are helper methods available in Rails layouts and views you can use to customize the rendering of an element and/or generate HTML code.
The Helper is available in the official documentation. Helpers are defined in the ActionView::Helpers namescope and they are grouped into modules, by their scope.
ActionView::Helpers
ActionView::Helpers::ActiveModelFormBuilder
ActionView::Helpers::ActiveModelHelper
ActionView::Helpers::ActiveModelInstanceTag
ActionView::Helpers::AssetTagHelper
ActionView::Helpers::AtomFeedHelper
ActionView::Helpers::AtomFeedHelper::AtomBuilder
ActionView::Helpers::AtomFeedHelper::AtomFeedBuilder
ActionView::Helpers::CacheHelper
...
Click on a module, e.g. ActionView::Helpers::AssetTagHelper, to view all the helper methods defined in the module. By default, a view includes all the view helpers defined in ActionView, with some small exceptions (a few methods are lazy-loaded).
If you talk about controllers and actions, in the controller you can take advantage of any helper method (and with helper I mean a convenient method to access a feature) defined in the ActionController::Base class.

Resources