Project

General

Profile

Plugin Tutorial » History » Revision 3

Revision 2 (Jean-Philippe Lang, 2008-08-08 21:31) → Revision 3/119 (Jean-Philippe Lang, 2008-08-10 17:36)

{{>toc}} 

 h1. Plugin Tutorial 

 h2. Creating a new Plugin 

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

   % ruby script/generate redmine_plugin Pools pools 

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

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

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

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

 Redmine::Plugin.register :redmine_pools do 
   name 'Pools plugin' 
   author 'John Smith' 
   description 'A plugin for managing pools' 
   version '0.0.1' 
 end 
 </code></pre> 

 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: 

 !plugins_list1.png! 

 h2. Generating a controller 

 For now, the plugin doesn't do anything. So let's create a controller for our plugin. TODO 
 Go back to the command prompt and run: 

 <pre> 
 % ruby script/generate redmine_plugin_controller Pools pools index vote 
       exists    app/controllers/ 
       exists    app/helpers/ 
       create    app/views/pools 
       create    test/functional/ 
       create    app/controllers/pools_controller.rb 
       create    test/functional/pools_controller_test.rb 
       create    app/helpers/pools_helper.rb 
       create    app/views/pools/index.html.erb 
       create    app/views/pools/vote.html.erb 
 </pre> 

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

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

 <pre><code class="ruby"> 
 class PoolsController < ApplicationController 
   unloadable 
  
   @@pools = [ {:id => 1, :title => 'First pool', :question => 'This is the first pool question', :yes => 0, :no => 0}, 
               {:id => 2, :title => 'Second pool', :question => 'This is the second pool question', :yes => 0, :no => 0} ] 

   def index 
     @pools = @@pools 
   end 

   def vote 
     pool = @@pools.find {|p| p[:id].to_s == params[:id]} 
     # saves the vote 
     pool[params[:answer].to_sym] += 1 
     flash[:notice] = 'Vote saved.' 
     redirect_to :action => 'index' 
   end 
 end 
 </code></pre> 

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


 <pre> 
 <h2>Pools</h2> 

 <% @pools.each do |pool| %> 
   <p> 
   <%= 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] %>) 
   </p> 
 <% end %> 
 </pre> 

 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/pools. 
 You should see the 2 pools and you should be able to vote for them.