Skip to main content
 首页 » 编程设计

ruby-on-rails之问题更改时Redmine插件 View Hook 刷新

2025年12月25日21yxwkf

我写了一个插件来抓取最新的问题并将它们显示在主页上。一切似乎都运行良好,但它仅在我第一次重新启动服务器以运行它时才有效 - 之后它“缓存”了当时的问题,并且没有获取新的问题。

我已经阅读了一些关于它的内容,看来我应该为问题编写一个补丁,以便在我的插件中添加额外的功能。真的吗?如果是这样,我应该添加什么到 after_save行动? LatestPostsSetup 模型也会发生同样的情况 - 如果我更改最大计数和应该显示它的一侧的值,主页不会反射(reflect)它,直到我重新启动服务器。

抱歉,如果这个问题看起来相当琐碎,我是 ruby​​ 新手。请在下面找到查看帮助程序代码:

module LatestPosts 
    class ViewHookListener < Redmine::Hook::ViewListener 
 
        require  'plugins/latest_posts/app/models/latest_posts_setup.rb' 
        setup = LatestPostsSetup.find_by_id(1) 
 
        if setup == nil 
          count = LatestPostsSetup::DEFAULT_COUNT 
          side  = LatestPostsSetup::DEFAULT_SIDE 
        else 
          count = setup.max_count 
          side  = setup.side 
        end 
 
        issues  = Issue.find(:all, :limit => count, :order => "created_on DESC") 
 
        if side == 'left' 
           render_side = :view_welcome_index_left 
        else 
           render_side = :view_welcome_index_right 
        end 
        render_on render_side, :locals => {:issues => issues}, :partial => "latest_issues/issues" 
    end end 

编辑

我现在已经更改了 View 助手以即时渲染 html,并且我不必重新启动 apache 来显示新问题,我不明白为什么使用 html 模板可以解决问题?
请在下面找到代码:
# lib/latest_posts_hook_listener.rb 
module LatestPosts 
    class ViewHookListener < Redmine::Hook::ViewListener 
 
        def view_welcome_index_left(context={}) 
            setup = load_setup() 
            if setup[:side] == "left" 
                load_issues(setup[:count]) 
            end 
        end 
 
        def view_welcome_index_right(context={}) 
            setup = load_setup() 
            if setup[:side] == "right" 
                load_issues(setup[:count]) 
            end 
        end 
 
        def load_setup() 
            require  'plugins/latest_posts/app/models/latest_posts_setup.rb' 
            setup = LatestPostsSetup.find_by_id(1) 
 
            if setup == nil 
                count = LatestPostsSetup::DEFAULT_COUNT 
                side  = LatestPostsSetup::DEFAULT_SIDE 
            else 
                count = setup.max_count 
                side  = setup.side 
            end 
 
            {:count => count, :side => side} 
        end 
 
        def load_issues(count) 
            html = '<div class="box" id="statuses">' 
            html += '<h3 class="icon22 icon22-users">Latest Issues</h3><ul>' 
            issues  = Issue.find(:all, :limit => count, :order => "created_on DESC") 
            issues.each do |issue| 
                html += <<EOHTML 
                  <li> 
                      #{link_to h(truncate(issue.subject, :length => 60)), :controller => 'issues', :action => 'show', :id => issue } (#{ format_date(issue.created_on) }) 
                   </li> 
EOHTML 
            end 
            html += '</ul></div>' 
            return html 
        end 
    end 
end 

请您参考如下方法:

让我们看看这是否对您有帮助。我正在阅读 this tutorial并在 Using Hooks 部分有这样一段:

To use one or more hooks in views, you need to create a class that inherits from Redmine::Hook::ViewListener and implement methods named with the hook(s) you want to use.



在您不起作用的代码中,我看到您已经创建了该类,但是,我没有看到为您要使用的 Hook 实现的方法。

正如该部分中给出的示例一样,我查看了此 View http://www.redmine.org/projects/redmine/repository/entry/tags/2.0.0/app/views/issues/show.html.erb (不确定这是您需要的 View )它显示有两个可用的钩子(Hook):一个名为 :view_issues_show_details_bottom还有一个名为 :view_issues_show_description_bottom我希望你会在你的代码方法中实现名称与钩子(Hook)名称匹配的方法。这个 View 中的这些钩子(Hook)会显示你想要的吗?如果没有,您可能需要使用不同的 View 。

作为引用,我在这里查看了 Redmine View 列表: http://www.redmine.org/projects/redmine/repository/show/tags/2.0.0/app/views