Using restful path helper methods in a separate lib - ruby-on-rails

In a certain module that I am using in my application, I would like to use the method alarm_path(alarm). How can I include this method in my current module?
I already tried to include ActionView::Helpers::UrlHelper, though, it still complain that this method does not exists.

Simply add this instead of your current include:
include Rails.application.routes.url_helpers

Related

Redmine/Ruby/Rails - Use custom helper methods inside of another custom helper

I don't know Ruby&Rails, but I would like to build a small custom Redmine plugin for my personal needs.
And I have faced with the problem which might look quite straightforward for Ruby experts.
I have two helpers (modules):
helper1
helper2
And I would like to use helper1.method1 inside of helper2.method3.
I have tried following to achieve this:
simply call method helper1.method1 inside helper2.method3, with thoughts that relations resolved automatically - didn't work;
require helper1 inside helper2 by require '../../relative/path'- didn't worked;
require helper1 inside helper2 by require '../../relative/path'- didn't worked;
included helper1 inside helper2- didn't worked
I have tried to find information how proper to call a method from one custom helper inside of another custom helper but didn't found any relevant results. The most of results were about how to call custom helper method inside view, controller, settings view.
So, could somebody explain to me how properly use methods from one custom helper inside of another one?
Best, regards.
For common functionality I would suggest to
create a module in the lib folder (e.g. lib/my_plugin/common_code.rb)
require it in the plugins init.rb:
ActionDispatch::Callbacks.to_prepare do
require 'my_plugin/common_code'
end
include it in each helper it is needed
include MyPlugin::CommonCode

How do I globally register a Liquid filter

I created a custom Liquid filter
module LinkFilter
include ActionView::Helpers::UrlHelper
include Rails.application.routes.url_helpers
def link(input,source)
link_to input,source
end
end
and placed it in app/filters/LinkFilter.rb
How do I globally register the filter? According the Liquid documentation, adding this line at the end of the above file would register it globally:
Liquid::Template.register_filter(LinkFilter)
But it doesn't seem to work. Am I placing the filter in the wrong location or what?
You have to place the filter in the lib directory instead. The one in the application root, not the app/lib one (if you have one).

Extending a method in rails

In the application I'm creating I use a gem. This gem has a Module with a method that is called by the gem when something changes. What I need to do is extend the functionality of this method. I cannot change the name of this method and I am not able to call a different method instead.
I'm talking about the rebuild method defined here:
https://github.com/the-teacher/the_sortable_tree/blob/master/app/controllers/the_sortable_tree_controller.rb
How can I add functionality to this method without touching the source?
You need to include the module with the function name which you want to override in the controller and then you can write the function with the same name and call super when you are done with your work.
class A
include TheSortableTreeController::Rebuild
def rebuild
# do something here
super
end
This way you will be able to perform yours as well as the operation of the gem as well. If you want to completely remove the dependency on the rebuild function remove super from the code.

How do I invoke a method from a custom module?

I've created a module in the /lib folder:
Module CookieHelper
$str = cookies["shoppingcart"]
def get_all_cookie_info()
end
end
But this isn't working. If I move the code into a controller it works fine.
Also, I'm trying to invoke the method within this module. I've tried:
require CookieHelper
CookieHelper::get_all_cookie_info()
to use methods from a module inside of an other class (such as a controller), you do something like this:
include CookieHelper
Once you do that inside of your controller's class, you can call get_all_cookie_info() with just the method name.
It sounds like you might be trying to something odd, so if you want to spell out what get_all_cookie_info is supposed to do, then maybe I can offer more advice.
CookieHelper::get_all_cookie_info is the correct way to call this method.
include CookieHelper
get_all_cookie_info
is another valid way, if you want include all of the methods in cookie helper available without having to namespace them (once the file lib/cookie_helper has been loaded).
What the issue probably is, is that the lib file isn't even required yet, this is because rails3 doesn't automatically load files in lib anymore. You can tell it to do so by editing your application.rb file, and setting inside class Application < Rails::Application
config.autoload_paths += %W( #{config.root}/lib )

Overriding ApplicationHelper?

There is an ApplicationHelper in an engine we're using that looks like this:
module ApplicationHelper
def page_title()
# ...
end
end
This gets called by a number of views in the engine. I'd like to override this method in my application to provide a different default title. How do I do this? Simply defining my own ApplicationHelper didn't seem to work.
Did you define the page_title method in your ApplicationHelper module? Just having an ApplicationHelper by itself won't do it.
By default, your ApplicationHelper module should already be in app/helpers/application_helper.rb. Your app files are required by rails after it requires the plugins you are using. That means any method you define in app/helpers/application_helper.rb should override methods defined in the plugins code.
This behavior arises out of ruby's open class structure. Any time, anywhere any class/module/object can be reopened to add methods and attributes to it. More or less.
Could you post some of the code you are trying to override the engine method with?
Are other methods defined in your ApplicationHelper available?
If yes, the engine helper is probably loaded before your ApplicationHelper, and your method overwritten.
If no, Rails may never have loaded your module because it thinks it already knows about ApplicationHelper and doesn't try to load it again.

Resources