我写了一个插件来抓取最新的问题并将它们显示在主页上。一切似乎都运行良好,但它仅在我第一次重新启动服务器以运行它时才有效 - 之后它“缓存”了当时的问题,并且没有获取新的问题。
我已经阅读了一些关于它的内容,看来我应该为问题编写一个补丁,以便在我的插件中添加额外的功能。真的吗?如果是这样,我应该after_save
采取什么行动?LatestPostsSetup 模型也会发生同样的情况 - 如果我更改最大计数和应该显示它的一侧的值,主页不会反映它,直到我重新启动服务器。
抱歉,如果这个问题看起来相当琐碎,我是 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
编辑
我现在已经更改了视图助手以即时渲染 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