Plugin Tutorial » History » Revision 18

Revision 17 (Jean-Philippe Lang, 2008-08-19 10:06) → Revision 18/115 (Jean-Philippe Lang, 2008-09-11 17:22)

h1. Plugin Tutorial 


 h2. Creating a new Plugin 

 Creating a new plugin can be done using the Redmine plugin generator. 
 Syntax for this generator is: 

   ruby script/generate redmine_plugin <plugin_name> 

 So open up a command prompt and "cd" to your redmine directory, then execute the following command: 

   % ruby script/generate redmine_plugin Polls Pools 

 The plugin structure is created in @vendor/plugins/redmine_polls@: @vendor/plugins/redmine_pools@: 

       create    vendor/plugins/redmine_polls/app/controllers vendor/plugins/redmine_pools/app/controllers 
       create    vendor/plugins/redmine_polls/app/helpers vendor/plugins/redmine_pools/app/helpers 
       create    vendor/plugins/redmine_polls/app/models vendor/plugins/redmine_pools/app/models 
       create    vendor/plugins/redmine_polls/app/views vendor/plugins/redmine_pools/app/views 
       create    vendor/plugins/redmine_polls/db/migrate vendor/plugins/redmine_pools/db/migrate 
       create    vendor/plugins/redmine_polls/lib/tasks vendor/plugins/redmine_pools/lib/tasks 
       create    vendor/plugins/redmine_polls/assets/images vendor/plugins/redmine_pools/assets/images 
       create    vendor/plugins/redmine_polls/assets/javascripts vendor/plugins/redmine_pools/assets/javascripts 
       create    vendor/plugins/redmine_polls/assets/stylesheets vendor/plugins/redmine_pools/assets/stylesheets 
       create    vendor/plugins/redmine_polls/lang vendor/plugins/redmine_pools/lang 
       create    vendor/plugins/redmine_polls/README vendor/plugins/redmine_pools/README 
       create    vendor/plugins/redmine_polls/init.rb vendor/plugins/redmine_pools/init.rb 
       create    vendor/plugins/redmine_polls/lang/en.yml vendor/plugins/redmine_pools/lang/en.yml 

 Edit @vendor/plugins/redmine_polls/init.rb@ @vendor/plugins/redmine_pools/init.rb@ to adjust plugin information (name, author, description and version): 

 <pre><code class="ruby"> 
 require 'redmine' 

 Redmine::Plugin.register :redmine_polls :redmine_pools do 
   name 'Polls 'Pools plugin' 
   author 'John Smith' 
   description 'A plugin for managing polls' pools' 
   version '0.0.1' 

 Then restart the application and point your browser to http://localhost:3000/admin/info. 
 After logging in, you should see your new plugin in the plugins list: 

 p=. !plugins_list1.png! 

 h2. Generating a model 

 Let's create a simple Pool model for our plugin: 

   ruby script/generate redmine_plugin_model polls pools pool question:string yes:integer no:integer 

 This creates the Pool model and the corresponding migration file. 
 Migrate the database using the following command: 

   rake db:migrate_plugins 

 Note that each plugin has its own set of migrations. 

 Edit @app/models/pool.rb@ in your plugin directory to add a #vote method that will be invoked from our controller: 

 <pre><code class="ruby"> 
 class Pool < ActiveRecord::Base 
   def vote(answer) 
     increment(answer == 'yes' ? :yes : :no) 

 h2. Generating a controller 

 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 <plugin_name> <controller_name> [<actions>] 

 So go back to the command prompt and run: 

 % ruby script/generate redmine_plugin_controller Polls polls Pools pools index vote 
       exists    app/controllers/ 
       exists    app/helpers/ 
       create    app/views/polls app/views/pools 
       create    test/functional/ 
       create    app/controllers/polls_controller.rb app/controllers/pools_controller.rb 
       create    test/functional/polls_controller_test.rb test/functional/pools_controller_test.rb 
       create    app/helpers/polls_helper.rb app/helpers/pools_helper.rb 
       create    app/views/polls/index.html.erb app/views/pools/index.html.erb 
       create    app/views/polls/vote.html.erb app/views/pools/vote.html.erb 

 A controller @PollsController@ @PoolsController@ with 2 actions (@#index@ and @#vote@) is created. 

 Edit @app/controllers/polls_controller.rb@ @app/controllers/pools_controller.rb@ in @redmine_polls@ @redmine_pools@ directory to implement these 2 actions. 

 <pre><code class="ruby"> 
 class PollsController PoolsController < ApplicationController 

   def index 
     @polls @pools = Pool.find(:all) 

   def vote 
     pool = Pool.find(params[:id])[:anwser]) 
     flash[:notice] = 'Vote saved.' 
     redirect_to :action => 'index' 

 Then edit @app/views/polls/index.html.erb@ @app/views/pools/index.html.erb@ that will display existing polls: pools: 

 <h2>Polls</h2> <h2>Pools</h2> 

 <% @polls.each @pools.each do |pool| %> 
   <%= pool[:question] %>? 
   <%= link_to 'Yes', {:action => 'vote', :id => pool[:id], :answer => 'yes'}, :method => :post %> (<%= pool[:yes] %>) / 
   <%= link_to 'No', {:action => 'vote', :id => pool[:id], :answer => 'no'}, :method => :post %> (<%= pool[:no] %>) 
 <% end %> 

 You can remove @vote.html.erb@ since no rendering is done by the corresponding action. 

 Now, restart the application and point your browser to http://localhost:3000/polls. http://localhost:3000/pools. 
 You should see the 2 polls pools and you should be able to vote for them: 

 p=. !polls1.png! !pools1.png! 

 Note that pool results are reset on each request if you don't run the application in production mode, since our pool "model" is stored in a class variable in this example. 

 h2. Extending menus 

 Our controller works fine but users have to know the url to see the polls. pools. Using the Redmine plugin API, you can extend standard menus. 
 So let's add a new item to the application menu. 

 h3. Extending the application menu 

 Edit @init.rb@ at the root of your plugin directory to add the following line at the end of the plugin registration block: 

 <pre><code class="ruby"> 
 Redmine::Plugin.register :redmine_polls :redmine_pools do 
   menu :application_menu, :polls, :pools, { :controller => 'polls', 'pools', :action => 'index' }, :caption => 'Polls' 'Pools' 

 Syntax is: 

   menu(menu_name, item_name, url, options={}) 

 There are 4 menus that you can extend: 

 * @:top_menu@ - the top left menu 
 * @:account_menu@ - the top right menu with sign in/sign out links 
 * @:application_menu@ - the main menu displayed when the user is not inside a project 
 * @:project_menu@ - the main menu displayed when the user is inside a project 

 Available options are: 

 * @:param@ - the parameter key that is used for the project id (default is @:id@) 
 * @:if@ - a Proc that is called before rendering the item, the item is displayed only if it returns true 
 * @:caption@ - the menu caption that can be: 

   * a localized string Symbol 
   * a String 
   * a Proc that can take the project as argument 

 * @:before@, @:after@ - specify where the menu item should be inserted (eg. @:after => :activity@) 
 * @:last@ - if set to true, the item will stay at the end of the menu (eg. @:last => true@) 
 * @:html_options@ - a hash of html options that are passed to @link_to@ when rendering the menu item 

 In our example, we've added an item to the application menu which is emtpy by default. 
 Restart the application and go to http://localhost:3000: 

 p=. !application_menu.png! 

 Now you can access the polls pools by clicking the Polls Pools tab from the welcome screen. 

 h3. Extending the project menu 

 Now, let's consider that the polls pools are defined at project level (even if it's not the case in our example pool model). So we would like to add the Polls Pools tab to the project menu instead. 
 Open @init.rb@ and replace the line that was added just before with these 2 lines: 

 <pre><code class="ruby"> 
 Redmine::Plugin.register :redmine_polls :redmine_pools do 

   permission :polls, {:polls :pools, {:pools => [:index, :vote]}, :public => true 
   menu :project_menu, :polls, :pools, { :controller => 'polls', 'pools', :action => 'index' }, :caption => 'Polls', 'Pools', :after => :activity, :param => :project_id 

 The second line adds our Polls Pools tab to the project menu, just after the activity tab. 
 The first line is required and declares that our 2 actions from @PollsController@ @PoolsController@ are public. We'll come back later to explain this with more details. 

 Restart the application again and go to one of your projects: 

 p=. !project_menu.png! 

 If you click the Polls Pools 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 PollsController PoolsController to do so: 

 <pre><code class="ruby"> 
 def index 
   @project = Project.find(params[:project_id]) 
   @polls @pools = Pool.find(:all) # @project.polls @project.pools 

 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 polls: pools: 

 p=. !project_menu_polls.png! !project_menu_pools.png! 

 h2. Adding new permissions 

 For now, anyone can vote for polls. pools. Let's make it more configurable by changing the permission declaration. 
 We're going to declare 2 project based permissions, one for viewing the polls pools and an other one for voting. These permissions are no longer public (@:public => true@ option is removed). 

 Edit @init.rb@ to replace the previous permission declaration with these 2 lines: 

 <pre><code class="ruby"> 
   permission :view_polls, :polls :view_pools, :pools => :index 
   permission :vote_polls, :polls :vote_pools, :pools => :vote 

 Restart the application and go to http://localhost:3000/roles/report: 

 p=. !permissions1.png! 

 You're now able to give these permissions to your existing roles. 

 Of course, some code needs to be added to the PollsController PoolsController so that actions are actually protected according to the permissions of the current user. 
 For this, we just need to append the @:authorize@ filter and make sure that the @project instance variable is properly set before calling this filter. 

 Here is how it would look like for the @#index@ action: 

 <pre><code class="ruby"> 
 class PollsController PoolsController < ApplicationController 
   before_filter :find_project, :authorize, :only => :index 

   def index 
     @polls @pools = Pool.find(:all) # @project.polls @project.pools 

   def find_project 
     # @project variable must be set before calling the authorize filter 
     @project = Project.find(params[:project_id]) 

 Retrieving the current project before the @#vote@ action could be done using a similiar way. 
 After this, viewing and voting polls pools will be only available to admin users or users that have the appropriate role on the project. 

 h2. Creating a project module 

 For now, the pool functionality is added to all your projects. But you way want to enable polls pools for some projects only. 
 So, let's create a 'Polls' 'Pools' project module. This is done by wraping the permissions declaration inside a call to @#project_module@. 

 Edit @init.rb@ and change the permissions declaration: 

 <pre><code class="ruby"> 
   project_module :polls :pools do 
     permission :view_polls, :polls :view_pools, :pools => :index 
     permission :vote_polls, :polls :vote_pools, :pools => :vote 

 Restart the application and go to one of your project settings. 
 Click on the Modules tab. You should see the Polls Pools module at the end of the modules list (disabled by default): 

 p=. !modules.png! 

 You can now enable/disable polls pools at project level. 

 h2. Improving the plugin views 

 h3. Adding stylesheets 

 Let's start by adding a stylesheet to our plugin views. 
 Create a file named @voting.css@ in the @assets/stylesheets@ directory of your plugin: 

 <pre> { font-size: 120%; } { color: green; }    { color: red; } 

 When starting the application, plugin assets are automatically copied to @public/plugin_assets/redmine_polls/@ @public/plugin_assets/redmine_pools/@ 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 @app/views/polls/index.html.erb@ @app/views/pools/index.html.erb@ so that your stylesheet get included in the page header by Redmine: 

 <% content_for :header_tags do %> 
     <%= stylesheet_link_tag 'voting', :plugin => 'redmine_polls' 'redmine_pools' %> 
 <% end %> 

 Note that the @:plugin => 'redmine_polls'@ 'redmine_pools'@ option is required when calling the @stylesheet_link_tag@ helper. 

 Javascripts can be included in plugin views using the @javascript_include_tag@ helper in the same way. 

 h3. Setting page title 

 You can set the HTML title from inside your views by using the @html_title@ helper. 

   <% html_title "Polls" "Pools" -%>