Commit a1b60748 authored by Jean-Baptiste Barth's avatar Jean-Baptiste Barth

Refactor: added link_to_project helper to handle links to projects

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3924 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent 5b64f0ff
...@@ -103,6 +103,23 @@ module ApplicationHelper ...@@ -103,6 +103,23 @@ module ApplicationHelper
link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => revision}, :title => l(:label_revision_id, revision)) link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => revision}, :title => l(:label_revision_id, revision))
end end
# Generates a link to a project if active
# Examples:
#
# link_to_project(project) # => link to the specified project overview
# link_to_project(project, :action=>'settings') # => link to project settings
# link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
# link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
#
def link_to_project(project, options={}, html_options = nil)
if project.active?
url = {:controller => 'projects', :action => 'show', :id => project}.merge(options)
link_to(h(project), url, html_options)
else
h(project)
end
end
def toggle_link(name, id, options={}) def toggle_link(name, id, options={})
onclick = "Element.toggle('#{id}'); " onclick = "Element.toggle('#{id}'); "
onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
...@@ -368,12 +385,12 @@ module ApplicationHelper ...@@ -368,12 +385,12 @@ module ApplicationHelper
ancestors = (@project.root? ? [] : @project.ancestors.visible) ancestors = (@project.root? ? [] : @project.ancestors.visible)
if ancestors.any? if ancestors.any?
root = ancestors.shift root = ancestors.shift
b << link_to(h(root), {:controller => 'projects', :action => 'show', :id => root, :jump => current_menu_item}, :class => 'root') b << link_to_project(root, {:jump => current_menu_item}, :class => 'root')
if ancestors.size > 2 if ancestors.size > 2
b << '&#8230;' b << '&#8230;'
ancestors = ancestors[-2, 2] ancestors = ancestors[-2, 2]
end end
b += ancestors.collect {|p| link_to(h(p), {:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item}, :class => 'ancestor') } b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') }
end end
b << h(@project) b << h(@project)
b.join(' &#187; ') b.join(' &#187; ')
...@@ -605,8 +622,7 @@ module ApplicationHelper ...@@ -605,8 +622,7 @@ module ApplicationHelper
end end
when 'project' when 'project'
if p = Project.visible.find_by_id(oid) if p = Project.visible.find_by_id(oid)
link = link_to h(p.name), {:only_path => only_path, :controller => 'projects', :action => 'show', :id => p}, link = link_to_project(p, {:only_path => only_path}, :class => 'project')
:class => 'project'
end end
end end
elsif sep == ':' elsif sep == ':'
...@@ -648,8 +664,7 @@ module ApplicationHelper ...@@ -648,8 +664,7 @@ module ApplicationHelper
end end
when 'project' when 'project'
if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}]) if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}])
link = link_to h(p.name), {:only_path => only_path, :controller => 'projects', :action => 'show', :id => p}, link = link_to_project(p, {:only_path => only_path}, :class => 'project')
:class => 'project'
end end
end end
end end
......
...@@ -72,7 +72,7 @@ module ProjectsHelper ...@@ -72,7 +72,7 @@ module ProjectsHelper
end end
classes = (ancestors.empty? ? 'root' : 'child') classes = (ancestors.empty? ? 'root' : 'child')
s << "<li class='#{classes}'><div class='#{classes}'>" + s << "<li class='#{classes}'><div class='#{classes}'>" +
link_to(h(project), {:controller => 'projects', :action => 'show', :id => project}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}") link_to_project(project, {}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}")
s << "<div class='wiki description'>#{textilizable(project.short_description, :project => project)}</div>" unless project.description.blank? s << "<div class='wiki description'>#{textilizable(project.short_description, :project => project)}</div>" unless project.description.blank?
s << "</div>\n" s << "</div>\n"
ancestors << project ancestors << project
......
...@@ -50,7 +50,7 @@ module QueriesHelper ...@@ -50,7 +50,7 @@ module QueriesHelper
when 'User' when 'User'
link_to_user value link_to_user value
when 'Project' when 'Project'
link_to(h(value), :controller => 'projects', :action => 'show', :id => value) link_to_project value
when 'Version' when 'Version'
link_to(h(value), :controller => 'versions', :action => 'show', :id => value) link_to(h(value), :controller => 'versions', :action => 'show', :id => value)
when 'TrueClass' when 'TrueClass'
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
<tbody> <tbody>
<% project_tree(@projects) do |project, level| %> <% project_tree(@projects) do |project, level| %>
<tr class="<%= cycle("odd", "even") %> <%= css_project_classes(project) %> <%= level > 0 ? "idnt idnt-#{level}" : nil %>"> <tr class="<%= cycle("odd", "even") %> <%= css_project_classes(project) %> <%= level > 0 ? "idnt idnt-#{level}" : nil %>">
<td class="name"><%= project.active? ? link_to(h(project.name), :controller => 'projects', :action => 'settings', :id => project) : h(project.name) %></td> <td class="name"><%= link_to_project(project, :action => 'settings') %></td>
<td><%= textilizable project.short_description, :project => project %></td> <td><%= textilizable project.short_description, :project => project %></td>
<td align="center"><%= checked_image project.is_public? %></td> <td align="center"><%= checked_image project.is_public? %></td>
<td align="center"><%= format_date(project.created_on) %></td> <td align="center"><%= format_date(project.created_on) %></td>
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<%= check_box_tag("ids[]", issue.id, false, :style => 'display:none;') %> <%= check_box_tag("ids[]", issue.id, false, :style => 'display:none;') %>
<%= link_to issue.id, :controller => 'issues', :action => 'show', :id => issue %> <%= link_to issue.id, :controller => 'issues', :action => 'show', :id => issue %>
</td> </td>
<td class="project"><%= link_to(h(issue.project), :controller => 'projects', :action => 'show', :id => issue.project) %></td> <td class="project"><%= link_to_project(issue.project) %></td>
<td class="tracker"><%=h issue.tracker %></td> <td class="tracker"><%=h issue.tracker %></td>
<td class="subject"> <td class="subject">
<%= link_to h(truncate(issue.subject, :length => 60)), :controller => 'issues', :action => 'show', :id => issue %> (<%=h issue.status %>) <%= link_to h(truncate(issue.subject, :length => 60)), :controller => 'issues', :action => 'show', :id => issue %> (<%=h issue.status %>)
......
<p><%= link_to(h(news.project.name), :controller => 'projects', :action => 'show', :id => news.project) + ': ' unless @project %> <p><%= link_to_project(news.project) + ': ' unless @project %>
<%= link_to h(news.title), :controller => 'news', :action => 'show', :id => news %> <%= link_to h(news.title), :controller => 'news', :action => 'show', :id => news %>
<%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count > 0 %> <%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count > 0 %>
<br /> <br />
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
<p class="nodata"><%= l(:label_no_data) %></p> <p class="nodata"><%= l(:label_no_data) %></p>
<% else %> <% else %>
<% @newss.each do |news| %> <% @newss.each do |news| %>
<h3><%= link_to(h(news.project.name), :controller => 'projects', :action => 'show', :id => news.project) + ': ' unless news.project == @project %> <h3><%= link_to_project(news.project) + ': ' unless news.project == @project %>
<%= link_to h(news.title), :controller => 'news', :action => 'show', :id => news %> <%= link_to h(news.title), :controller => 'news', :action => 'show', :id => news %>
<%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count > 0 %></h3> <%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count > 0 %></h3>
<p class="author"><%= authoring news.created_on, news.author %></p> <p class="author"><%= authoring news.created_on, news.author %></p>
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<% next if membership.new_record? %> <% next if membership.new_record? %>
<tr id="member-<%= membership.id %>" class="<%= cycle 'odd', 'even' %> class"> <tr id="member-<%= membership.id %>" class="<%= cycle 'odd', 'even' %> class">
<td class="project"> <td class="project">
<%= link_to h(membership.project), {:controller => 'projects', :action => 'show', :id => membership.project} %> <%= link_to_project membership.project %>
</td> </td>
<td class="roles"> <td class="roles">
<span id="member-<%= membership.id %>-roles"><%=h membership.roles.sort.collect(&:to_s).join(', ') %></span> <span id="member-<%= membership.id %>-roles"><%=h membership.roles.sort.collect(&:to_s).join(', ') %></span>
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<h3><%=l(:label_project_plural)%></h3> <h3><%=l(:label_project_plural)%></h3>
<ul> <ul>
<% for membership in @memberships %> <% for membership in @memberships %>
<li><%= link_to(h(membership.project.name), :controller => 'projects', :action => 'show', :id => membership.project) %> <li><%= link_to_project(membership.project) %>
(<%=h membership.roles.sort.collect(&:to_s).join(', ') %>, <%= format_date(membership.created_on) %>)</li> (<%=h membership.roles.sort.collect(&:to_s).join(', ') %>, <%= format_date(membership.created_on) %>)</li>
<% end %> <% end %>
</ul> </ul>
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<% for project in @projects %> <% for project in @projects %>
<% @project = project %> <% @project = project %>
<li> <li>
<%= link_to h(project.name), :controller => 'projects', :action => 'show', :id => project %> (<%= format_time(project.created_on) %>) <%= link_to_project project %> (<%= format_time(project.created_on) %>)
<%= textilizable project.short_description, :project => project %> <%= textilizable project.short_description, :project => project %>
</li> </li>
<% end %> <% end %>
......
...@@ -597,4 +597,16 @@ EXPECTED ...@@ -597,4 +597,16 @@ EXPECTED
t = link_to_user(user) t = link_to_user(user)
assert_equal ::I18n.t(:label_user_anonymous), t assert_equal ::I18n.t(:label_user_anonymous), t
end end
def test_link_to_project
project = Project.find(1)
assert_equal %(<a href="/projects/ecookbook">eCookbook</a>),
link_to_project(project)
assert_equal %(<a href="/projects/ecookbook/settings">eCookbook</a>),
link_to_project(project, :action => 'settings')
assert_equal %(<a href="http://test.host/projects/ecookbook?jump=blah">eCookbook</a>),
link_to_project(project, {:only_path => false, :jump => 'blah'})
assert_equal %(<a href="/projects/ecookbook/settings" class="project">eCookbook</a>),
link_to_project(project, {:action => 'settings'}, :class => "project")
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment