【DevOps工具篇】Redmine插件开发流程
@TOC
推荐超级课程:
本文将简单快速介绍如何进行Redmine插件的开发流程。
- 前提:需要您具备一定的Ruby/Rails开发基础。
- 创建一个新插件。
要使用此命令,您可能需要设置RAILS_ENV变量。
$ export RAILS_ENV= "production"
在Windows上:
$ set RAILS_ENV=production
您还可以使用Redmine插件生成器。
bundle exec rails generate redmine_plugin <plugin_name>
打开命令提示符并‘cd’到Redmine目录。接下来,执行此命令:
$ bundle exec rails generate redmine_plugin Polls
创建 plugins/polls/app
创建 plugins/polls/app/controllers
创建 plugins/polls/app/helpers
创建 plugins/polls/app/models
创建 plugins/polls/app/views
创建 plugins/polls/db/migrate
创建 plugins/polls/lib/tasks
创建 plugins/polls/assets/images
创建 plugins/polls/assets/javascripts
创建 plugins/polls/assets/stylesheets
创建 plugins/polls/config/locales
创建 plugins/polls/test
创建 plugins/polls/test/fixtures
创建 plugins/polls/test/unit
创建 plugins/polls/test/functional
创建 plugins/polls/test/integration
创建 plugins/polls/test/system
创建 plugins/polls/README.rdoc
创建 plugins/polls/init.rb
创建 plugins/polls/config/routes.rb
创建 plugins/polls/config/locales/en.yml
创建 plugins/polls/test/test_helper.rb
...
生成一个模型
插件不存储任何东西。您可以使用以下语法创建一个简单的Poll模型:
bundle exec rails generate redmine_plugin_model <plugin_name> <model_name> [field[:type][:index] field[:type][:index] …]
转到命令提示符并运行此命令:
$ bundle exec rails generate redmine_plugin_model polls poll question:string yes:integer no:integer
创建 plugins/polls/app/models/poll.rb
创建 plugins/polls/test/unit/poll_test.rb
创建 plugins/polls/db/migrate/xxxxxxxxxxxx_create_polls.rb
- 生成一个控制器
您可以使用以下语法为插件创建一个控制器:
bundle exec rails generate redmine_plugin_controller <plugin_name> <controller_name> [<actions>]
转到命令提示符并运行此命令:
$ bundle exec rails generate redmine_plugin_controller Polls polls index vote
创建 plugins/polls/app/controllers/polls_controller.rb
创建 plugins/polls/app/helpers/polls_helper.rb
创建 plugins/polls/test/functional/polls_controller_test.rb
创建 plugins/polls/app/views/polls/index.html.erb
创建 plugins/polls/app/views/polls/vote.html.erb
- 添加路由
Redmine不会为用户提供默认的通配符路由(‘:controller/:action/:id’)。因此,插件必须在适当的文件中声明所需的路线:config/routes.rb 使用以下方式为2个动作添加2个路由:
get 'polls', to: 'polls#index'
post 'post/:id/vote', to: 'polls#vote'
现在,重新启动应用程序并查看投票的投票。
- 如何国际化
要国际化,请将翻译文件存储在config/locales中,例如 plugins/polls/config/locales/。
- 如何添加新权限
如果您希望团队中的任何人都能在投票中投票,您可以更改权限设置并进行配置。编辑plugins/polls/init.rb并使用以下方式更改先前的声明:
permission :view_polls, polls: :index
permission :vote_polls, polls: :vote
现在重新启动应用程序并转到:http://localhost:3000/roles/permissions :
- 如何创建一个项目模块 尽管投票功能适用于所有项目,但您可能只想为某些特定项目启用投票。因此,您可以创建一个‘Polls’项目模块。编辑init.rb更改权限设置:
project_module :polls **do**
permission :view_polls, polls: :index
permission :vote_polls, polls: :vote
重新启动应用程序并转到项目设置。选择模块选项卡并在列表末尾查看投票模块。