Wednesday 14 December 2011

How to Create Redmine Plugin (Custom Menus) and integrate on HostGator Server VPS.

How to Create Redmine Plugin to Add Custom Menus to Project Menu on HostGator Server VPS:
The name of Plugin is CMenu and tittle is workplans.

steps are below:
first i have taken the backup of the redmine application database.
host: localhost
username: user_redmine
password: password
encoding: utf8

Then start creating the new plugin:

ssh -p 2222
password:

mysqldump -u user database > databasebakup

Creating a new Plugin
To set the RAILS_ENV variable in order to use the command below:

export RAILS_ENV="production"

Creating a new plugin can be done using the Redmine plugin generator.
Syntax for this generator is:
So open up a command prompt and "cd" to your redmine directory, then execute the following command:

ruby script/generate redmine_plugin CMenu

The plugin structure is created in vendor/plugins/redmine_C_Menu:
Edit vendor/plugins/redmine_C_Menu/init.rb to adjust plugin information (name, author, description and version):
.............................................
require 'redmine'
Redmine::Plugin.register :redmine_C_Menu do
name 'CMenu plugin'
author 'sysadmin'
description 'A plugin for adding custum menu'
version '0.0.1'
end
.............................................

Then restart the application and point your browser to http://redmin.domain/admin/plugins.

After logging in, you should see your new plugin in the plugins list:

Generating a model
For now plugin doesn't store anything. Let's create a simple Workplans model for our plugin. Syntax is:

ruby script/generate redmine_plugin_model CMenu workplans tabmenu:string Click:string

This creates the workplans model and the corresponding migration file.
Please note you may have to rename your migration. Timestamped migrations are not supported by the actual Redmine plugin engine (Engines). If your migrations are named with a timestamp, rename it using "001", "002", etc. instead.

mv vendor/plugins/redmine_c_menu/db/migrate/20111214133141_create_workplans.rb vendor/plugins/redmine_c_menu/db/migrate/001_create_workplans.rb

If you have already created a database table record in plugin_schema_info with the timestamp version number, you will have to change it to reflect your new version number, or the migration will hang.

Migrate the database using the following command:

rake db:migrate_plugins

Generating a controller

Warning: starting from 1.4.0, Redmine won't provide anymore the default wildcard route (':controller/:action/:id'). Plugins will have to declare the routes they need in their proper config/routes.rb file.
For now, the plugin doesn't do anything. So let's create a controller for our plugin.
We can use the plugin controller generator for that. Syntax is:

ruby script/generate redmine_plugin_controller CMenu workplans index

A controller workplansController with 1 action (#index) is created.
Edit vendor/plugins/redmine_c_menu/app/controllers/workplans_controller.rb redmine_C_menu directory to implement the actions.

vim vendor/plugins/redmine_c_menu/app/controllers/workplans_controller.rb
.........................................................
class WorkplansController < ApplicationController
unloadable
before_filter :find_project, :authorize, :only => :index
def index
       @workplans = CMenu.find(:all)
 end
 def find_project
      @project = Project.find(params[:project_id])
 end
end
............................................................

Then edit vendor/plugins/redmine_c_menu/app/views/workplans/index.html.erb that will display existing polls:
...........................................................
<h2>Workplans</h2>
............................................................
Extending the project menu

Now, let's consider that the CMenu are defined at project level (even if it's not the case in our example poll model). So we would like to add the CMenu tab to the project menu instead.
Open init.rb and replace the line that was added just before with these 2 lines:
................................................................
permission :workplans, { :workplans => [:index] }, :public => true
menu :project_menu, :workplans, { :controller => 'workplans', :action => 'index' }, :caption => 'CMenu', :after => :activity, :param => :project_id
.................................................................

The second line adds our Polls tab to the project menu, just after the activity tab.
The first line is required and declares that our 1 action from workplansController are public
To make it private add the permission:
permission :view_workplans, :workplans => :index and remove the :public => true from 1st line.
Restart the application again and go to one of your projects:
If you click the CMenu tab, you should notice that the project menu is no longer displayed.
To make the project menu visible, you have to initialize the controller's instance variable @project.
Edit your workplansController to do so:
................................................................
class WorkplansController < ApplicationController
unloadable
before_filter :find_project, :authorize, :only => :index
def index
       @CMenu = CMenu.find(:all)
 end
 def find_project
      @project = Project.find(params[:project_id])
 end
end
................................................................
The project id is available in the :project_id param because of the :param => :project_id option in the menu item declaration above.
Now, you should see the project menu when viewing the CMenu:

Adding stylesheets
Let's start by adding a stylesheet to our plugin views.
Create a file named workplans.css in the vendor/plugins/redmine_C_Menu/assets/stylesheets directory:

a.workplans { font-size: 120%; color: blue }

When starting the application, plugin assets are automatically copied to public/plugin_assets/redmine_C_Menu/ by Rails Engines to make them available through your web server. So any change to your plugin stylesheets or javascripts needs an application restart.
Then, append the following lines at the end of vendor/plugins/redmine__C_Menu/app/views/polls/index.html.erb so that your stylesheet get included in the page header by Redmine:
..................................................................
<% html_title "Workplans" >
< content_for :header_tags do >
<= stylesheet_link_tag 'workplans', :plugin => 'redmine_C_Menu' >
< end %>
..................................................................

Restart your application and view the Plugin:

Thats it

Regards:
Imran Ullah

No comments:

Post a Comment