Project

General

Profile

menu integration

Added by Christian Reich almost 12 years ago

hi,
i am developing a plugin and have set up some links in the redmine menu.
the main menu for my plugin will be placed in the "top_menu" and works well.
my problem is, that for all other functionality i want to use the "application_menu", but only if the user clicks on the menu which was placed at the "top_menu".
afaik the application menu will be rendered everytime, except i am within a project.
i know that it is possible to place some conditions in my init.rb but i don't know whats the best for my needs?
the only condition i have seen there looks like that:

  menu :application_menu, :time_tracker_menu_tab_overview, { :controller => 'time_trackers', :action => 'index'},
       {
           :caption => :time_tracker_label_menu_tab_overview,
           # TODO figure out a condition that enables the menu only for the timeTracker-page
           :if => Proc.new { User.current.logged? }
       }

where can i find a list of valid conditions? i want to do something like that:

   :if => Proc.new { :controller => "time_trackers" || :controller => "time_logs"}

or like that:
   :if => Proc.new { <User clicked on "timeTracker" link in the top_menu> }

btw. the top_menu entry i have done that way:

  # setup an menu entry into the redmine top-menu on the upper left corner
  menu :top_menu, :time_tracker_main_menu, {:controller => 'time_trackers', :action => 'index'}, :caption => :time_tracker_label_main_menu, :if => Proc.new { User.current.logged? }

is that possible?

regards christian reich

edit: i am running redmine 2.0.2 on rails 3.2.5


Replies (17)

RE: menu integration - Added by Christian Reich almost 12 years ago

just to have a complete thread i'll show the sollution i figured out.

i don't use the :if => <condition> statement of the menuentries.
instead i wrote a menu-patch which expanses the funcionality of the MenuManager and refactored the render_main_menu and disply_main_menu? methods via an alias_method_chain.

code looks like that:

require_dependency 'lib/redmine/menu_manager'

# redmine only differs between project_menu and application_menu! but we want to display the
# time_tracker submenu only if the plugin specific controllers are called
module MenuPatch

  def self.included(base)
    base.send(:extend, ClassMethods)
    base.send(:include, InstanceMethods)
    base.class_eval do
      alias_method_chain :render_main_menu, :time_tracker
      alias_method_chain :display_main_menu?, :time_tracker
    end
  end

  module ClassMethods
  end

  module InstanceMethods

    def display_main_menu_with_time_tracker?(project)
      Redmine::MenuManager.items(get_menu_name(project)).children.present?
    end

    def render_main_menu_with_time_tracker(project)
      render_menu(get_menu_name(project), project)
    end

    private

    def get_menu_name(project)
      if (project && !project.new_record?)
        :project_menu
      else
        if params[:controller] == "time_trackers" || params[:controller] == "time_logs" 
          :timetracker_menu
        else
          :application_menu
        end
      end
    end
  end
end

i also added a few lines to the init.rb:

   :
# adding the patch
require 'menu_patch'
Redmine::MenuManager::MenuHelper.send(:include, MenuPatch)
   :
   :

# setup an menu entry into the redmine top-menu on the upper left corner
menu :top_menu, :time_tracker_main_menu, {:controller => 'time_trackers', :action => 'index'}, :caption => :time_tracker_label_main_menu, :if => Proc.new { User.current.logged? }

# create the plugin-specific menu
  Redmine::MenuManager.map :timetracker_menu do |menu|
    menu.push :time_tracker_menu_tab_overview, {:controller => 'time_trackers', :action => 'index'}, :caption => :time_tracker_label_menu_tab_overview, :if => Proc.new { User.current.logged? }
    menu.push :time_tracker_menu_tab_logs, {:controller => 'time_logs', :action => 'index'}, :caption => :time_tracker_label_menu_tab_logs, :if => Proc.new { User.current.logged? }
  end

maybe it will help anyone else :)

btw. found useful information about alias_method_chain at the following link and in the api-doc for rails:
http://erniemiller.org/2011/02/03/when-to-use-alias_method_chain/

regards christian

RE: menu integration - Added by # And over 11 years ago

I've got No such file to load -- lib/redmine/menu_manager when try to look on your plugin. Thats is the line with problem: require_dependency 'lib/redmine/menu_manager'. What is the trouble? menu_manager is the 3rd party plugin?

Thanks in any advise.

RE: menu integration - Added by Christian Reich over 11 years ago

have you tried only the codesnippets from above?
maybe you need the whole plugin, which can be found at https://github.com/hicknhack-software/redmine_time_tracker

menu_manager is not 3rd party. it's part of redmine (i am running redmine 2.x)

RE: menu integration - Added by # And over 11 years ago

I've tried to use whole plugin from link - https://github.com/hicknhack-software/redmine_time_tracker, but have no success.
I've got No such file to load -- lib/redmine/menu_manager after rake db:migrate_plugins RAILS_ENV=production.

My environment is:

Environment:
  Redmine version                          2.0.3.stable
  Ruby version                             1.9.3 (x86_64-linux)
  Rails version                            3.2.6
  Environment                              production
  Database adapter                         Mysql2

so I think there is no problem.
Really, I have no idea on how to solve or debug this trouble. Can you give any ideas?

RE: menu integration - Added by # And over 11 years ago

It could be collision with another plugin?

RE: menu integration - Added by Christian Reich over 11 years ago

as far as i can see the rake-command you use is for the old redmine version 1.x
please check this tutorial: http://www.redmine.org/projects/redmine/wiki/Plugins
the rake command should be rake redmine:plugins:migrate RAILS_ENV=production

my environment for now is

Environment:
  Redmine version                          2.0.2.stable
  Ruby version                             1.9.3 (i386-mingw32)
  Rails version                            3.2.5
  Environment                              development
  Database adapter                         SQLite

maybe that will help

RE: menu integration - Added by # And over 11 years ago

It would help, if I was ruby-programmer. =)
Full shell log:

root@artvertep:/var/www/redmine/plugins# git clone git://github.com/hicknhack-software/redmine_time_tracker.git
Cloning into redmine_time_tracker...
remote: Counting objects: 1848, done.
remote: Compressing objects: 100% (802/802), done.
remote: Total 1848 (delta 1065), reused 1689 (delta 926)
Receiving objects: 100% (1848/1848), 299.36 KiB | 396 KiB/s, done.
Resolving deltas: 100% (1065/1065), done.

root@artvertep:/var/www/redmine/plugins# rake redmine:plugins:migrate RAILS_ENV=production
(in /var/www/redmine)
rake aborted!
No such file to load -- lib/redmine/menu_manager

Tasks: TOP => redmine:plugins:migrate => environment
(See full trace by running task with --trace)

root@artvertep:/var/www/redmine/plugins# ls redmine_time_tracker/
app  assets  config  COPYRIGHT.txt  db  GPL.txt  init.rb  lib  README.markdown  test

I'll try to disable all other plugins.

RE: menu integration - Added by # And over 11 years ago

No success. =(

root@artvertep:/var/www/redmine/plugins# ls
redmine_time_tracker

root@artvertep:/var/www/redmine/plugins# !rake
rake redmine:plugins:migrate RAILS_ENV=production
(in /var/www/redmine)
rake aborted!
No such file to load -- lib/redmine/menu_manager

Tasks: TOP => redmine:plugins:migrate => environment
(See full trace by running task with --trace)

RE: menu integration - Added by Christian Reich over 11 years ago

i reproduced the problem in a virtual machine and on my developing-machine...
i am looking for a solution..

maybe you will have a look in the IRC #redmine channel on freenode..

RE: menu integration - Added by Christian Reich over 11 years ago

got it :D
the paths had to be modified somehow... (havn't had this error in the development environment! only in production)
please checkout the latest version from git, should work now!

kind regards
christian :D

RE: menu integration - Added by # And over 11 years ago

Yeah, it's works! Cool! =) Thanks for your work.

One more question. Why this plugin could conflict with another usable plugin - redmine_local_avatars?
Look, please on shell log:

root@artvertep:/var/www/redmine# mv plugins/* plugins2

root@artvertep:/var/www/redmine# mv plugins2/redmine_time_tracker/ plugins

root@artvertep:/var/www/redmine# mv plugins2/redmine_local_avatars/ plugins

root@artvertep:/var/www/redmine# ./webrick-start.sh
=> Booting WEBrick
=> Rails 3.2.6 application starting in production on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
/usr/local/rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
[2012-09-01 12:21:46] INFO  WEBrick 1.3.1
[2012-09-01 12:21:46] INFO  ruby 1.9.3 (2012-04-20) [x86_64-linux]
[2012-09-01 12:21:46] INFO  WEBrick::HTTPServer#start: pid=31986 port=3000

Started GET "/" for 217.212.230.20 at 2012-09-01 12:21:48 +0200
Processing by WelcomeController#index as HTML
  Rendered news/_news.html.erb (124.1ms)
  Rendered welcome/index.html.erb within layouts/base (5952.3ms)
Completed 500 Internal Server Error in 6475ms

ActionView::Template::Error (undefined method `get_menu_name' for #<#<Class:0x00000004e3d740>:0x0000000511fdf0>):
    46:         <%= render_project_jump_box %>
    47:     </div>
    48:     <% end %>
    49:
    50:     <h1><%= page_header_title %></h1>
    51:
    52:     <% if display_main_menu?(@project) %>
  app/views/layouts/base.html.erb:49:in `_app_views_layouts_base_html_erb___2071273372119788582_45957260'

Look like one class replace another (local_avatars replace class from time_tracker), but I dont see any similar hooks, or similar stuff in local_avatars.
Can you please look? =) Plugin git link - git://github.com/luckval/redmine_local_avatars.git
If something wrong in local_avatars - I'll report about it to author.

Christian, thanks again. =)

RE: menu integration - Added by Christian Reich over 11 years ago

i am not sure why this happened, but i think i found a workaround :)
so feel free to try the most actual version from git, should work even with other plug-ins now :D

RE: menu integration - Added by # And over 11 years ago

Yay, really nice! =) Thats work!
Thanks for your time.

P.S. Found several bugs, where I can report it?

RE: menu integration - Added by # And over 11 years ago

P.P.S.
I have idea to improve js-popup timer. I can design, code and anything else that's frontend-specific. Whats you say about?

RE: menu integration - Added by Christian Reich over 11 years ago

your welcome :)
The Issue-Tracker at github is activated now, so if you found bugs please report them there.

if you want to submit some code, feel free to fork the repo and send me a pullrequest if your done with your work.
i will appreciate any help :)

RE: menu integration - Added by # And over 11 years ago

Ok. I see you enable issue tracker at github. I'll post found bugs in that tracker.
Can I contact with you by e-mail that you registered on redmine.org? Or any other way?

RE: menu integration - Added by Christian Reich over 11 years ago

sure, feel free to mail me :)
e-mail can be fund on redmine or github as well

    (1-17/17)