이 문서는 Redmine 2.x 기반의 플러그인 개발 가이드입니다. 그리고 이 문서는 redmine.org의 플러그인 개발 가이드를 번역하였습니다.
2. 모델 생성(Generating a model)
생성된 플러그인에 정보를 저장하기 위해, 간단한 Poll model을 생성합니다.
문법은 다음과 같습니다.
bundle exec ruby script/rails generate redmine_plugin_model <plugin_name> <model_name> [field[:type][:index] field[:type][:index] ...]
명령어 창으로 이동하여 다음 명령을 수행합니다.
$ bundle exec 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
이것은 Poll 모델(Model)을 만들고 plugins/polls/db/migrate에 다음과 같은 마이그레이션 파일 001_create_polls.rb을 만듭니다.
class CreatePolls < ActiveRecord::Migration def change create_table :polls do |t| t.string :question t.integer :yes, :default => 0 t.integer :no, :default => 0 end end end
필요시 마이그레이션 파일을 수정할 수 있습니다.
그다음 다음 명령어를 사용하여 마이그레이트 할 수 있습니다.
$ bundle exec rake redmine:plugins:migrate Migrating polls (Polls plugin)...
== CreatePolls: migrating ====================================================
-- create_table(:polls)
-> 0.0410s
== CreatePolls: migrated (0.0420s) ===========================================
각 플러그인은 자신 소유이 마이그레이션 세트를 가지게 됩니다.
콘솔(console)에 몇몇 Poll을 추가함으로써 원하는 기능을 수행할 수 있습니다.
콘솔(console)은 상호작용하는 일을 할 수 있고 레드마인 환경을 시험할 수 있고 수행 정보를 줄 수 있습니다.
이 예저에서는 단지 두개의 Poll 오브젝트를 만듭니다.
bundle exec ruby script/rails console [rails 3] rails console >> Poll.create(:question => "Can you see this poll") >> Poll.create(:question => "And can you see this other poll") >> exit
콘트롤러로부터 포함될 예정인 #vote 메소드를 추가하기 위해 플러그인 디렉토리에서 plugins/polls/app/models/poll.rb를 Edit합니다.
class Poll < ActiveRecord::Base def vote(answer) increment(answer == 'yes' ? :yes : :no) end end