Plugin Tutorial » History » Version 3
Jean-Philippe Lang, 2008-08-10 17:36
| 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 | !plugins_list1.png! |
||
| 46 | |||
| 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 | Now, restart the application and point your browser to http://localhost:3000/pools. |
||
| 108 | You should see the 2 pools and you should be able to vote for them. |