nest helper methods in rails3 - ruby-on-rails

I would to nest some helper methods to simplify my application_helper, for example I have a bunch of methods dealing with currencies which apply for the entire app and I would like to put in the currencies_helper and then simply include that entire helper into the application helper.
How can I do this?
I tried:
helper :currencies
and got
undefined method `helper' for ApplicationHelper:Module

Helpers in rails are just modules which get included in controllers to help share functionality between them. There's been some weirdness around helpers in rails 3, so depending on what version you're running things may or may not work as you expect out the box.
Essentially what you want to do is add helper :all to your application_controller which will include ALL helpers in ALL your controllers. If this isn't what you want you can specify the specific helpers you want helper :currencies for example.
In the rails 3 betas helper :all was the default behavior, but I think they've reverted that in the latest release.
There's a great article that discusses how this works in rails 2, but there may be differences in rails 3, but it should be a good starting point.

Related

Why can't I call an ActionView method from the Rails Console?

I'm trying to do a little testing of the number_to_currency method, but no matter how I try and call it in the console I'm out of luck it seems.
ActionView::Helpers::NumberHelper::number_to_currency 12321334543.00
# => NoMethodError: undefined method `number_to_currency' for ActionView::Helpers::NumberHelper:Module
This may qualify as a duplicate, I am not entirely sure. The other question is a lot broader in scope, and therefore has a lot more ways of getting things gone presented in the answers, and I also believe it may have some outdated answers.
ActionView::Helpers::NumberHelper is a module. Whenever you want to use it you should include it and all it's method could be used after that. So the first step is including ActionView::Helpers::NumberHelper module:
include ActionView::Helpers::NumberHelper
and then call the method you want:
number_to_currency 12321334543.00
Take a look at Ruby modules tutorial.
Edit:
Getting a console session bootstrapped with Rails’ helpers is also a pain, which helper can fix for you! You could also use it to play with building HTML tags, or any existing Rails helper that ActionView knows about (checkout this blog post).
helper.number_to_currency 12321334543.00
try helper.number_to_currency 12321334543.00

Rails: best practice to load a helper in controller

I was hired some days ago to update a Rails 4 app. In general the rspecs and the code look good however in the top of some controller a found this line:
delegate :edit_app_path, :new_app_payment_path, to: :view_context
Searching in the net, I found that the line is a way to load methods from the helpers inside a controller through a new instance of ActionView::Base class. I mean, is a way to do it instead of the classic:
include MyHelper
in the controller. My question is: is this really a good practice? is faster? AFAIK, view_context will load a new class with all the helpers and all the context of the view instead of one helper if I use the classic "include MyHelper". By the way :edit_app_path and :new_app_payment_path methods are in the same helper.
Should I remove the line?
Using view_context allows the controller to be blissfully ignorant of where the path is defined. If the helper file structure is refactored in future, the controller will continue humming along without requiring change.
Performance wise, I doubt the impact will be significant since all the code will have been loaded. Rails (and the Ruby standard library) creates new objects all the time.

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 do I override _path helpers in rails properly?

I have a model called resource in my rails app and in need of modifying the return value of the helper *resource_path*, I've read some docs and SO Q/A and they're generally suggesting put the customized helper in *app/helpers/application_helper.rb*. The thing bothers me is that what do I do with the old auto generated helper? should I do something like
undef resource_path
before I go ahead and write my own helper? Currently I have a *resource_path* method defined within ApplicationHelper, interestingly when I open rails console, app.resource_path and helper.resource_path giving me different result.
Also, I'd like to hear a deeper explanation on how *_path* helpers implemented and how they are related to *link_to* helper, as the source code are kinda hard to read with so many meta programming techniques involved
Yes, you are able to do it like following:
resources :photos, as: 'images'
in your config/routes.rb file.
More details you can find here http://guides.rubyonrails.org/routing.html especially 4.3 Overriding the Named Helpers

Inherit from multiple controllers.

Im using a Rails engine as a cms. It all works fine. Im adding devise to this.
My generated devise controllers inherit from Devise::SessionsController. But there are some filters that are run from another controller in the engine that wont run in this case. A lot of the site relies on these filters being run. Of course I could just duplicate them, but thats bad juju.
So my Question is: How can I make one controller run the filters from another? I would prefer not to edit either of the gems.
Multiple inheritance is not supported in Ruby. I think extracting the filters into a module and mixing them in would be the cleanest solution.
See for example:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html
There is another option available for Devise: config.parent_controller. It defaults to ApplicationController but can be changed to something else. In my case (a Rails API) I use ApiController. In config/initializers/devise.rb:
Devise.setup do |config|
# ... other configuration options
config.parent_controller = 'ApiController'
end

Resources