Search code examples
ruby-on-rails-3redmine-plugins

Migrating Redmine plugin to Rails 3


I am trying to run Time Tracker plugin with Redmine 2.0.1 on Bitnami Stack. I found that it's imcompatible with Redmine 2.* but it couldn't stop me. So, I placed this plugin in ../redmine/htdocs/plugins/redmine_time_tracker, migrated it with rake db:migrate_plugins RAILS_ENV=production, changed routes to Rails 3 style in redmine_time_tracker/config/routes.rb and renamed all files in redmine_time_tracker/app/views from *.rhtml to *.html.erb. After another Redmine's restart I rested against the wall. There is an error message in Redmine's production.log:

Started GET "/redmine" for 127.0.0.1 at Tue Jun 12 22:36:05 +0400 2012
Processing by WelcomeController#index as HTML
  Rendered welcome/index.html.erb within layouts/base (9.5ms)
  Rendered plugins/redmine_time_tracker/app/views/time_trackers/_embed_menu.html.erb (203.0ms)
  Rendered plugins/redmine_time_tracker/app/views/time_trackers/_update_menu.html.erb (203.5ms)
Completed 500 Internal Server Error in 252ms

ActionView::Template::Error (undefined method `time_tracker_for' for #<#<Class:0x7f5efb458e48>:0x7f5efb322290>):
    1: <% time_tracker = time_tracker_for(User.current) %>
    2: <% if !time_tracker.nil? %>
    3:     <% if time_tracker.paused %>
    4:         <%# A time tracker is in pause, display the tracked issue, the time spent and the resume/stop actions %>
  lib/redmine/hook.rb:110:in `send'
  lib/redmine/hook.rb:110:in `view_layouts_base_body_bottom'
  lib/redmine/hook.rb:61:in `send'
  lib/redmine/hook.rb:61:in `call_hook'
  lib/redmine/hook.rb:61:in `each'
  lib/redmine/hook.rb:61:in `call_hook'
  lib/redmine/hook.rb:58:in `tap'
  lib/redmine/hook.rb:58:in `call_hook'
  lib/redmine/hook.rb:151:in `call_hook'
  app/views/layouts/base.html.erb:80:in `_app_views_layouts_base_html_erb___1317325009_70022959536540'

time_tracker_for helper is placed in redmine_time_tracker/app/helpers/application_helper.rb and I think it should be loaded automatically according to "Plugin constructor" in redmine/htdocs/lib/redmine/plugin.rb.

I am new to Ruby and Rails and need some directions to continue fight this magic.


Solution

  • Finally Solved.

    I found here : http://www.redmine.org/boards/3/topics/31445 the answer (after reading almost anything I found about plugins in redmine and engines in rails3)

    Extending default helpers and controllers

    In Redmine versions prior to 2.0.0 all plugins were in vendor/plugins folder and because of that Rails automatically loaded and added all files inside app/controllers, app/helpers, app/models, etc folders.

    This approach allowed to extend existing controllers and helpers by simply adding appropriate files and methods to the plugin' folders. For example, if we have to add method get_list_of_projects to SettingsHelper we can simply create file named settings_helper.rb in app/helpers folder of the plugin. Redmine 2.x moved plugins to plugins folder in application's root and because of that method described above does not work at all. So we have to use patching and override mechanisms described above to make things work.

    In the link is explained how to make the mentioned patching mechanism.

    I implemented that patch for the time_tracker plugin for redmine and is working. The code is in a github fork https://github.com/martinllanos/redmine_time_tracker

    I hope it helps. Any questions ask here :)