redmine 読み (8)

secret_token.rb ですが導入時に生成しなければ、とのことらしい。

# つうか上記最初からきちんと確認しないとマズいな
ええと以下を実行して

$ rake generate_secret_token

config/initializer/ 配下にファイルが生成されてるのを確認して以下も実行なのか。

$ RAILS_ENV=production rake redmine:load_default_data

これで scripts/rails s してどうなるか。
をコンソールでは 200 OK 戻してます。

Started GET "/" for 127.0.0.1 at 2013-01-21 10:15:29 +0900
Processing by WelcomeController#index as HTML
  Current user: anonymous
  Rendered welcome/index.html.erb within layouts/base (162.4ms)
Completed 200 OK in 1211ms (Views: 499.9ms | ActiveRecord: 289.5ms)

ブラウザ表示は未だですがw
を、出た出た。本題は /admin/plugins へのアクセスなんですがログイン画面が出ましたな。初期状態では admin/admin でログインできるらしいので、ということでようやく plugin の画面が確認できました。

Generating a model

とりあえず簡単な model を、とのこと。

$ ruby script/rails generate redmine_plugin_model polls poll question:string yes:integer no:integer
      create  plugins/polls/app/models/poll.rb
      create  plugins/polls/test/unit/poll_test.rb
      create  plugins/polls/db/migrate/001_create_polls.rb

んで、migrate は以下になる模様。

$ rake redmine:plugins:migrate
Migrating polls (Polls plugin)...
==  CreatePolls: migrating ====================================================
-- create_table(:polls)
   -> 0.1493s
==  CreatePolls: migrated (0.1497s) ===========================================

で、console からデータを突っ込んでおいて、なのか。

$ ./scripts/rails c
Loading production environment (Rails 3.2.11)
irb(main):001:0> Poll.create(:question => "Can you see this poll")
   (0.1ms)  begin transaction
  SQL (32.2ms)  INSERT INTO "polls" ("no", "question", "yes") VALUES (?, ?, ?)  [["no", nil], ["question", "Can you see this poll"], ["yes", nil]]
   (229.9ms)  commit transaction
=> #<Poll id: 1, question: "Can you see this poll", yes: nil, no: nil>
irb(main):002:0> Poll.create(:question => "And can you see this other poll")
   (0.2ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "polls" ("no", "question", "yes") VALUES (?, ?, ?)  [["no", nil], ["question", "And can you see this other poll"], ["yes", nil]]
   (1336.1ms)  commit transaction
=> #<Poll id: 2, question: "And can you see this other poll", yes: nil, no: nil>

で、model に vote というメソドを追加しておけ、とありますね。とりあえず unloadable は削除しないでおいて現状以下。

class Poll < ActiveRecord::Base
  unloadable

  def vote(answer)
    increment(answer == 'yes' ? :yes : :no)
  end
end

Generating a controller

む、branch してないな、と思ったら作ってた。

$ git branch
* Polls
  master

では指示されている通りに controller を作ります。

$ ruby script/rails generate redmine_plugin_controller Polls polls index vote
      create  plugins/polls/app/controllers/polls_controller.rb
      create  plugins/polls/app/helpers/polls_helper.rb
      create  plugins/polls/test/functional/polls_controller_test.rb
      create  plugins/polls/app/views/polls/index.html.erb
      create  plugins/polls/app/views/polls/vote.html.erb

アクションが二つな模様。rails g のソレも色々確認しとかないとアレですね。以下なカンジに修正、なのかな。

class PollsController < ApplicationController
  unloadable

  def index
    @polls = Poll.all
  end

  def vote
    poll = Poll.find(params[:id])
    poll.vote(params[:answer])
    if poll.save
      flash[:notice] = 'Vote saved.'
    end
    redirect_to :action => 'index'
  end
end

あるいは index.html.erb が以下か。

<h2>Polls</h2>

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

plugins/polls/config/routes.rb に以下を追加、とのこと。

get 'polls', :to => 'polls#index'
post 'post/:id/vote', :to => 'polls#vote'

最初 config/routes.rb かと思ってびっくりしたけど、当り前ながらそんな訳ゃないですね。上記を保存して rake routes してみたら末端に以下が出力されてました。

polls GET          /polls(.:format)          polls#index
      POST         /post/:id/vote(.:format)  polls#vote

ええと routing 云々については以下を見れ、とありますので別途確認の方向で。

で、http://localhost:3000/polls にアクセスしてみたらテキスト通りの出力、って思ったらサーバ再起動が必要らしい。当り前と言えば当り前か。
無事ブラウザ出力確認できたんですが、YES とか NO をクリックしたら更新されるツクリ
になってるのか、ってイマサラ index.html.erb のソレを見返してたりして。

Internationalization

plugins/polls/config/locales/ を、とのこと

Extending menus

むむ。application menu に追加が云々、なのか。

Extending the application menu

plugins/polls/init.rb 修正しろ、との由。以下を追加、なのかな。

  menu :application_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls'

メニュとしては以下が追加可能とのこと。

: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
:admin_menu - the menu displayed on the Administration page (can only insert after Settings, before Plugins)

http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial より引用
上書きして reload してみます。出ない。これもサーバ再起動必要なのか。うん、出ましたね。

Extending the project menu

試験用のプロジェクトを作っておいて以下を plugins/polls/init.rb に盛り込むのか。

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

WEBrick 再起動して作成したプロジェクトの overview を reload したら Activity の隣りに Polls が出ました。このあたりの仕組みってのはソース掘ってるときになんとなく見えてた部分ですね。
あ、でも plugin で云々、って訳ではなかったのかどうか。
むむ、現状グローバル (?) な Polls 取得、になってるんですが

  def index
    @polls = Poll.all
  end

こうすると project ローカルな Polls のみ取得、になるの?

def index
  @project = Project.find(params[:project_id])
  @polls = Poll.find(:all) # @project.polls
end

WEBrick を reload してもう一つプロジェクトを新規作成して Polls 見てみると、ってやっぱグローバルなソレを取得してますね。これって model 変更しないとプロジェクト毎の Poll がなんちゃら、ってできないですよね、よく考えてみるに。
と、init.rb な記述が以下になっているのを発見。

Redmine::Plugin.register :redmine_polls do

盛り込んで確認。RoutingError (uninitialized constant PollsController) とのこと。
んーと、どうも @project が云々な記述は Polls なメニュを表示したまんまにするため、な模様。成程。
ちょっと以降について若干微妙なカンジなのでこれはここで止めよ。あまり Rails の勉強になっていない気もするし。
てことでエントリ投入。