Adds a optgroup for groups in users/groups select tags.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6307 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-07-23 19:05:22 +00:00
parent 578fdc62f2
commit ed01ae121d
7 changed files with 42 additions and 5 deletions

View File

@ -291,6 +291,20 @@ module ApplicationHelper
end end
s s
end end
# Returns a string for users/groups option tags
def principals_options_for_select(collection, selected=nil)
s = ''
groups = ''
collection.sort.each do |element|
selected_attribute = ' selected="selected"' if option_value_selected?(element, selected)
(element.is_a?(Group) ? groups : s) << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
end
unless groups.empty?
s << %(<optgroup label="#{h(l(:label_group_plural))}">#{groups}</optgroup>)
end
s
end
# Truncates and returns the string as a single line # Truncates and returns the string as a single line
def truncate_single_line(string, *args) def truncate_single_line(string, *args)

View File

@ -2,5 +2,5 @@
<div class="box"> <div class="box">
<p><%= f.text_field :name, :size => 30, :required => true %></p> <p><%= f.text_field :name, :size => 30, :required => true %></p>
<p><%= f.select :assigned_to_id, @project.assignable_users.sort.collect{|u| [u.name, u.id]}, :include_blank => true %></p> <p><%= f.select :assigned_to_id, principals_options_for_select(@project.assignable_users, @category.assigned_to), :include_blank => true %></p>
</div> </div>

View File

@ -39,7 +39,7 @@
<label><%= l(:field_assigned_to) %></label> <label><%= l(:field_assigned_to) %></label>
<%= select_tag('assigned_to_id', content_tag('option', l(:label_no_change_option), :value => '') + <%= select_tag('assigned_to_id', content_tag('option', l(:label_no_change_option), :value => '') +
content_tag('option', l(:label_nobody), :value => 'none') + content_tag('option', l(:label_nobody), :value => 'none') +
options_from_collection_for_select(@target_project.assignable_users, :id, :name)) %> principals_options_for_select(@target_project.assignable_users)) %>
</p> </p>
</div> </div>

View File

@ -8,7 +8,7 @@
<% end %> <% end %>
<p><%= f.select :priority_id, (@priorities.collect {|p| [p.name, p.id]}), {:required => true}, :disabled => !@issue.leaf? %></p> <p><%= f.select :priority_id, (@priorities.collect {|p| [p.name, p.id]}), {:required => true}, :disabled => !@issue.leaf? %></p>
<p><%= f.select :assigned_to_id, (@issue.assignable_users.collect {|m| [m.name, m.id]}), :include_blank => true %></p> <p><%= f.select :assigned_to_id, principals_options_for_select(@issue.assignable_users, @issue.assigned_to), :include_blank => true %></p>
<% unless @project.issue_categories.empty? %> <% unless @project.issue_categories.empty? %>
<p><%= f.select :category_id, (@project.issue_categories.collect {|c| [c.name, c.id]}), :include_blank => true %> <p><%= f.select :category_id, (@project.issue_categories.collect {|c| [c.name, c.id]}), :include_blank => true %>
<%= prompt_to_remote(image_tag('add.png', :style => 'vertical-align: middle;'), <%= prompt_to_remote(image_tag('add.png', :style => 'vertical-align: middle;'),

View File

@ -1,7 +1,7 @@
<div class="attributes"> <div class="attributes">
<div class="splitcontentleft"> <div class="splitcontentleft">
<p><%= f.select :status_id, (@allowed_statuses.collect {|p| [p.name, p.id]}), :required => true %></p> <p><%= f.select :status_id, (@allowed_statuses.collect {|p| [p.name, p.id]}), :required => true %></p>
<p><%= f.select :assigned_to_id, (@issue.assignable_users.collect {|m| [m.name, m.id]}), :include_blank => true %></p> <p><%= f.select :assigned_to_id, principals_options_for_select(@issue.assignable_users, @issue.assigned_to), :include_blank => true %></p>
</div> </div>
<div class="splitcontentright"> <div class="splitcontentright">
<% if Issue.use_field_for_done_ratio? %> <% if Issue.use_field_for_done_ratio? %>

View File

@ -27,7 +27,7 @@
<label><%= l(:field_assigned_to) %></label> <label><%= l(:field_assigned_to) %></label>
<%= select_tag('issue[assigned_to_id]', content_tag('option', l(:label_no_change_option), :value => '') + <%= select_tag('issue[assigned_to_id]', content_tag('option', l(:label_no_change_option), :value => '') +
content_tag('option', l(:label_nobody), :value => 'none') + content_tag('option', l(:label_nobody), :value => 'none') +
options_from_collection_for_select(@assignables, :id, :name)) %> principals_options_for_select(@assignables)) %>
</p> </p>
<% if @project %> <% if @project %>
<p> <p>

View File

@ -675,4 +675,27 @@ RAW
assert_equal %(<a href="/projects/ecookbook/settings" class="project">eCookbook</a>), assert_equal %(<a href="/projects/ecookbook/settings" class="project">eCookbook</a>),
link_to_project(project, {:action => 'settings'}, :class => "project") link_to_project(project, {:action => 'settings'}, :class => "project")
end end
def test_principals_options_for_select_with_users
users = [User.find(2), User.find(4)]
assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>),
principals_options_for_select(users)
end
def test_principals_options_for_select_with_selected
users = [User.find(2), User.find(4)]
assert_equal %(<option value="2">John Smith</option><option value="4" selected="selected">Robert Hill</option>),
principals_options_for_select(users, User.find(4))
end
def test_principals_options_for_select_with_users_and_groups
users = [User.find(2), Group.find(11), User.find(4), Group.find(10)]
assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>) +
%(<optgroup label="Groups"><option value="10">A Team</option><option value="11">B Team</option></optgroup>),
principals_options_for_select(users)
end
def test_principals_options_for_select_with_empty_collection
assert_equal '', principals_options_for_select([])
end
end end