2

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.

  • I think you can try to include both helper modules to you controller class (for example https://github.com/twinslash/redmine_omniauth_google/blob/c4c79f5ab5599a6d963bb57131c9065bd2bf921e/app/controllers/redmine_oauth_controller.rb#L5-L6). Not sure but I think if methods from included helpers are accessible in controller then these methods can be called from each other. – gotva Mar 03 '18 at 12:58

1 Answers1

1

For common functionality I would suggest to

  1. create a module in the lib folder (e.g. lib/my_plugin/common_code.rb)
  2. require it in the plugins init.rb:

    ActionDispatch::Callbacks.to_prepare do 
      require 'my_plugin/common_code'
    end
    
  3. include it in each helper it is needed

    include MyPlugin::CommonCode
    
Stephan Wiehr
  • 166
  • 1
  • 7