Redmine plugin view hook refresh when issues change
Asked Answered
A

1

6

I wrote a plugin that grabs latest issues and displays them on the home page. All seems to work pretty well, but it only works the first time I restart the server to run it - after that it 'caches' the issues from that time, and no new ones get fetched.

I've been reading a bit about it, and it seems that I should write a patch for issues to add extra functionality in my plugin. Is that true? If so, what should I add to after_save action? The same happens for the LatestPostsSetup model - if I change the values for max count and side on which it should be displayed, home page doesn't reflect it until I restart the server.

Sorry if this questions seems fairly trivial, I'm new to ruby. Please find the view helper code below:

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

EDIT

I have now changed the view helper to render the html on the fly, and I don't have to restart apache for the new issues to be displayed, I don't understand why would that work over using a html template? Please find the code below:

# 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
Avis answered 6/9, 2012 at 6:56 Comment(0)
T
1

Let's see if this helps you. I was reading this tutorial and down at the section Using Hooks there's this paragraph:

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.

In your code that does not work, I see you have created the class but, I don't see the methods implemented for the hooks you want to use.

As in the example given in that section, I went and looked at this view http://www.redmine.org/projects/redmine/repository/entry/tags/2.0.0/app/views/issues/show.html.erb (not sure this is the view you need) and it shows there are two hooks available: one named :view_issues_show_details_bottom and one named :view_issues_show_description_bottom and I would expect you'd implement in your code methods whose name match the hook names. Will these hooks in this view show what you want?? If not, you probably need to use a different view.

For reference I looked at the list of Redmine views here: http://www.redmine.org/projects/redmine/repository/show/tags/2.0.0/app/views

Thereinto answered 15/9, 2012 at 4:27 Comment(1)
thank you for the response, but this does not seem to be it. I changed render_on :view_welcome_index_right, :locals => {:issues => issues}, :partial => "latest_issues/issues" and the result was the same. the count of issues I want to display was not affected by the changes in the database.Avis

© 2022 - 2024 — McMap. All rights reserved.