Plugin Tutorial » History » Version 4
  Jean-Philippe Lang, 2008-08-10 18:10 
  
| 1 | 3 | Jean-Philippe Lang | {{>toc}} | 
|---|---|---|---|
| 2 | |||
| 3 | 1 | Jean-Philippe Lang | h1. Plugin Tutorial | 
| 4 | |||
| 5 | h2. Creating a new Plugin | ||
| 6 | |||
| 7 | Open up a command prompt and "cd" to your redmine directory, then execute the following command: | ||
| 8 | |||
| 9 | 3 | Jean-Philippe Lang | % ruby script/generate redmine_plugin Pools | 
| 10 | 1 | Jean-Philippe Lang | |
| 11 | The plugin structure is created in @vendor/plugins/redmine_pools@: | ||
| 12 | |||
| 13 | <pre> | ||
| 14 | create vendor/plugins/redmine_pools/app/controllers | ||
| 15 | create vendor/plugins/redmine_pools/app/helpers | ||
| 16 | create vendor/plugins/redmine_pools/app/models | ||
| 17 | create vendor/plugins/redmine_pools/app/views | ||
| 18 | create vendor/plugins/redmine_pools/db/migrate | ||
| 19 | create vendor/plugins/redmine_pools/lib/tasks | ||
| 20 | create vendor/plugins/redmine_pools/assets/images | ||
| 21 | create vendor/plugins/redmine_pools/assets/javascripts | ||
| 22 | create vendor/plugins/redmine_pools/assets/stylesheets | ||
| 23 | create vendor/plugins/redmine_pools/lang | ||
| 24 | create vendor/plugins/redmine_pools/README | ||
| 25 | create vendor/plugins/redmine_pools/init.rb | ||
| 26 | create vendor/plugins/redmine_pools/lang/en.yml | ||
| 27 | </pre> | ||
| 28 | |||
| 29 | 2 | Jean-Philippe Lang | Edit @vendor/plugins/redmine_pools/init.rb@ too adjust plugin information (name, author, description and version): | 
| 30 | 1 | Jean-Philippe Lang | |
| 31 | <pre><code class="ruby"> | ||
| 32 | require 'redmine' | ||
| 33 | |||
| 34 | Redmine::Plugin.register :redmine_pools do | ||
| 35 | name 'Pools plugin' | ||
| 36 | author 'John Smith' | ||
| 37 | description 'A plugin for managing pools' | ||
| 38 | version '0.0.1' | ||
| 39 | end | ||
| 40 | </code></pre> | ||
| 41 | |||
| 42 | 2 | Jean-Philippe Lang | Then restart the application and point your browser to http://localhost:3000/admin/info. | 
| 43 | 1 | Jean-Philippe Lang | After logging in, you should see your new plugin in the plugins list: | 
| 44 | 2 | Jean-Philippe Lang | |
| 45 | 4 | Jean-Philippe Lang | p=. !plugins_list1.png! | 
| 46 | 2 | Jean-Philippe Lang | |
| 47 | 1 | Jean-Philippe Lang | h2. Generating a controller | 
| 48 | |||
| 49 | 3 | Jean-Philippe Lang | For now, the plugin doesn't do anything. So let's create a controller for our plugin. | 
| 50 | Go back to the command prompt and run: | ||
| 51 | |||
| 52 | <pre> | ||
| 53 | % ruby script/generate redmine_plugin_controller Pools pools index vote | ||
| 54 | exists app/controllers/ | ||
| 55 | exists app/helpers/ | ||
| 56 | create app/views/pools | ||
| 57 | create test/functional/ | ||
| 58 | create app/controllers/pools_controller.rb | ||
| 59 | create test/functional/pools_controller_test.rb | ||
| 60 | create app/helpers/pools_helper.rb | ||
| 61 | create app/views/pools/index.html.erb | ||
| 62 | create app/views/pools/vote.html.erb | ||
| 63 | </pre> | ||
| 64 | |||
| 65 | A controller @PoolsController@ with 2 actions (@#index@ and @#vote@) is created. | ||
| 66 | |||
| 67 | Edit @app/controllers/pools_controller.rb@ in @redmine_pools@ directory to implement these 2 actions: | ||
| 68 | |||
| 69 | <pre><code class="ruby"> | ||
| 70 | class PoolsController < ApplicationController | ||
| 71 | unloadable | ||
| 72 | |||
| 73 |   @@pools = [ {:id => 1, :title => 'First pool', :question => 'This is the first pool question', :yes => 0, :no => 0}, | ||
| 74 |               {:id => 2, :title => 'Second pool', :question => 'This is the second pool question', :yes => 0, :no => 0} ] | ||
| 75 | |||
| 76 | def index | ||
| 77 | @pools = @@pools | ||
| 78 | end | ||
| 79 | |||
| 80 | def vote | ||
| 81 |     pool = @@pools.find {|p| p[:id].to_s == params[:id]} | ||
| 82 | # saves the vote | ||
| 83 | pool[params[:answer].to_sym] += 1 | ||
| 84 | flash[:notice] = 'Vote saved.' | ||
| 85 | redirect_to :action => 'index' | ||
| 86 | end | ||
| 87 | end | ||
| 88 | </code></pre> | ||
| 89 | |||
| 90 | Then edit @app/views/pools/index.html.erb@ that will display existing pools: | ||
| 91 | |||
| 92 | |||
| 93 | <pre> | ||
| 94 | <h2>Pools</h2> | ||
| 95 | |||
| 96 | <% @pools.each do |pool| %> | ||
| 97 | <p> | ||
| 98 | <%= pool[:question] %>? | ||
| 99 |   <%= link_to 'Yes', {:action => 'vote', :id => pool[:id], :answer => 'yes'}, :method => :post %> (<%= pool[:yes] %>) / | ||
| 100 |   <%= link_to 'No', {:action => 'vote', :id => pool[:id], :answer => 'no'}, :method => :post %> (<%= pool[:no] %>) | ||
| 101 | </p> | ||
| 102 | <% end %> | ||
| 103 | </pre> | ||
| 104 | |||
| 105 | You can remove @vote.html.erb@ since no rendering is done by the corresponding action. | ||
| 106 | |||
| 107 | 1 | Jean-Philippe Lang | Now, restart the application and point your browser to http://localhost:3000/pools. | 
| 108 | 4 | Jean-Philippe Lang | You should see the 2 pools and you should be able to vote for them: | 
| 109 | |||
| 110 | p=. !pools1.png! | ||
| 111 | |||
| 112 | 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. | ||
| 113 | |||
| 114 | h2. Extending menus | ||
| 115 | |||
| 116 | Our controller works fine but users have to know the url to see the pools. Using the Redmine plugin API, you can extend standard menus. | ||
| 117 | So let's add a new item to the application menu. | ||
| 118 | |||
| 119 | h3. Extending the application menu | ||
| 120 | |||
| 121 | Edit @init.rb@ at the root of your plugin directory to add the following line at the end of the plugin registration block: | ||
| 122 | |||
| 123 | <pre><code class="ruby"> | ||
| 124 | Redmine::Plugin.register :redmine_pools do | ||
| 125 | [...] | ||
| 126 | |||
| 127 |   menu :application_menu, :pools, { :controller => 'pools', :action => 'index' }, :caption => 'Pools' | ||
| 128 | end | ||
| 129 | </code></pre> | ||
| 130 | |||
| 131 | Syntax is: | ||
| 132 | |||
| 133 |   menu(menu_name, item_name, url, options={}) | ||
| 134 | |||
| 135 | There are 4 menus that you can extend: | ||
| 136 | |||
| 137 | * @:top_menu@ - the top left menu | ||
| 138 | * @:account_menu@ - the top right menu with sign in/sign out links | ||
| 139 | * @:application_menu@ - the main menu displayed when the user is not inside a project | ||
| 140 | * @:project_menu@ - the main menu displayed when the user is inside a project | ||
| 141 | |||
| 142 | Available options are: | ||
| 143 | |||
| 144 | * @:param@ - the parameter key that is used for the project id (default is @:id@) | ||
| 145 | * @:if@ - a Proc that is called before rendering the item, the item is displayed only if it returns true | ||
| 146 | * @:caption@ - the menu caption that can be: | ||
| 147 | |||
| 148 | * a localized string Symbol | ||
| 149 | * a String | ||
| 150 | * a Proc that can take the project as argument | ||
| 151 | |||
| 152 | * @:before@, @:after@ - specify where the menu item should be inserted (eg. @:after => :activity@) | ||
| 153 | * @:last@ - if set to true, the item will stay at the end of the menu (eg. @:last => true@) | ||
| 154 | * @:html_options@ - a hash of html options that are passed to @link_to@ when rendering the menu item | ||
| 155 | |||
| 156 | In our example, we've added an item to the application menu which is emtpy by default. | ||
| 157 | Restart the application and go to http://localhost:3000: | ||
| 158 | |||
| 159 | p=. !application_menu.png! | ||
| 160 | |||
| 161 | Now you can access the pools by clicking the Pools tab from the welcome screen. | ||
| 162 | |||
| 163 | h3. Extending the project menu | ||
| 164 | |||
| 165 | TODO | ||
| 166 | |||
| 167 | h2. Adding new permissions | ||
| 168 | |||
| 169 | TODO | ||
| 170 | |||
| 171 | h2. Creating a project module | ||
| 172 | |||
| 173 | TODO |