如何为redmine钩子视图提供自定义控制器

时间:2017-07-06 16:57:04

标签: ruby-on-rails hook redmine redmine-plugins

我写了一个redmine钩子视图,在问题描述的底部添加了字段。 我的钩子视图需要从问题控制器以外的另一个控制器中提取数据。

我用这段代码创建了我的钩子:

module RedmineKnowledgebase
class Hooks < Redmine::Hook::ViewListener
def view_issues_show_description_bottom(context = {})
context[:controller].send(:render_to_string, {
:partial => " redmine_plugin_xxx/hooks/view_issues_show_
description_bottom",
:locals => context
})
end
end
end

在我的钩子视图中,我有要填充的表:

<tbody>
    <% @ok_results.each do |res| %>
      <tr>
        <td><%= res.id %></td>
        <td><%= res.key %></td>
        <td><%= res.value %></td>      
      </tr>
    <% end %>
  </tbody>

基本上,我的@ok_results变量应该由使用couchdb休息模型的控制器提供,而不是由问题控制器提供。

应提供@ok_results变量的控制器如下:

class RequirementsController < ApplicationController
  before_action :set_requirement, only: [:show, :edit, :update, :destroy]

  # GET /requirements
  # GET /requirements.json
  def index
    #@requirements = requirement.all
    #Requirement.create(:first_name => "Homer", :last_name => "Simpson")
  end

  # GET /requirements/1
  # GET /requirements/1.json
  def show
  end

  def ok_result
    @ok_results = Requirement.ok_result.rows
  end

  def by_topic
    @by_topic_record = Requirement.by_topic.rows
  end

  def by_requirement_no
  @by_req_record = Requirement.by_requirement_no.rows
  end

  # GET /requirements/new
  def new
    @requirement = Requirement.new
  end

  # GET /requirements/1/edit
  def edit
  end

  # POST /requirements
  # POST /requirements.json
  def create
    @requirement = Requirement.new(requirement_params)
    print requirement_params[:name]
    print requirement_params[:email]
    respond_to do |format|
      if @requirement.save
        format.html { redirect_to @requirement, notice: 'requirement was successfully created.' }
        format.json { render :show, status: :created, location: @requirement }
      else
        format.html { render :new }
        format.json { render json: @requirement.errors, status: :unprocessable_entity }
      end
    end
  end

和我上面的控制器相关的沙发数据库模型如下:

require 'couchrest_model'

class Requirement < CouchRest::Model::Base
  use_database 'raw_files'
  property :requirement_no, String
  property :test_case, Array
  property :topic, Array
  property :round_1, Array
  property :ecupn, Array
  property :sim_tool_pn, Array
  property :details, Array
  design do
    view :by_topic, :map => "function(doc){if(doc.topic){emit(doc.topic,doc.details)}}"
    view :by_requirement_no, :map => "function(doc){if(doc.requirement_no){emit(doc.requirement_no,doc.test_case)}}"
    view :ok_result,
      :map =>
        "function(doc) {
          if (doc['requirement_no']) {
            doc.round_1.forEach(function(res, index){
                if(res == 'OK')
                emit(doc.requirement_no, [doc.test_case[index], doc.topic[index]]);
            });
          }
        }"
  end
  timestamps!

end

我该如何解决这个问题?

0 个答案:

没有答案