Project

General

Profile

Collect data in hooks

Added by Kevin Senechal over 11 years ago

Hi everybody!

I would like to collect some data and display them to the welcome page.

I succeeded to display some basic html via hook but I didn't success to collect data and give them to my view.

class PollsHookListener < Redmine::Hook::ViewListener
  def view_welcome_index_left(context={ })
        context[:controller].send(:render_to_string, {
          :partial => "polls/project_overview",
          :locals => context
        })
  end
end

Anyone could show me an example?

Thanks in advance!

Kevin


Replies (1)

RE: Collect data in hooks - Added by Samuel Ford over 11 years ago

I'm too much of a Rails n00b to know if this is "correct", but I was able to pass some data to my partial from the hook by adding it to the context.

class Hooks < Redmine::Hook::ViewListener
  def view_repositories_show_contextual(context={ })
    controller = context[:controller]
    project = context[:project]
    repo_id = controller.params[:repository_id]

    if (repo_id)
      repos = project.repositories.detect{ |repo| repo.identifier == repo_id }
    else
      repos = project.repository
    end

    host_name = Setting.host_name
    protocol = Setting.protocol

    repo_url = "#{protocol}://#{host_name}/git/#{repos.identifier}" 
    context[:repo_url] = repo_url

    controller.send(:render_to_string, {
      :partial => "show_repo_url/repo_url",
      :locals => context
    })
  end
end

Then in the partial, I was able to reference it like so:

<div class="clone-url"><input type="text" readonly="true" size="60" value="<%= repo_url %>"></div>

Now if only I could figure out how to get my stylesheet hooked in ...

    (1-1/1)