From ed01ae121daf44d1df43332b01f3d5f9d627a3d5 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 23 Jul 2011 19:05:22 +0000 Subject: [PATCH] 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 --- app/helpers/application_helper.rb | 14 ++++++++++++ app/views/issue_categories/_form.rhtml | 2 +- app/views/issue_moves/new.rhtml | 2 +- app/views/issues/_attributes.rhtml | 2 +- app/views/issues/_form_update.rhtml | 2 +- app/views/issues/bulk_edit.rhtml | 2 +- test/unit/helpers/application_helper_test.rb | 23 ++++++++++++++++++++ 7 files changed, 42 insertions(+), 5 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a1832882e..6d5a058dc 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -291,6 +291,20 @@ module ApplicationHelper end s 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) << %() + end + unless groups.empty? + s << %(#{groups}) + end + s + end # Truncates and returns the string as a single line def truncate_single_line(string, *args) diff --git a/app/views/issue_categories/_form.rhtml b/app/views/issue_categories/_form.rhtml index fc5f158d3..fae0a75e5 100644 --- a/app/views/issue_categories/_form.rhtml +++ b/app/views/issue_categories/_form.rhtml @@ -2,5 +2,5 @@

<%= f.text_field :name, :size => 30, :required => true %>

-

<%= f.select :assigned_to_id, @project.assignable_users.sort.collect{|u| [u.name, u.id]}, :include_blank => true %>

+

<%= f.select :assigned_to_id, principals_options_for_select(@project.assignable_users, @category.assigned_to), :include_blank => true %>

diff --git a/app/views/issue_moves/new.rhtml b/app/views/issue_moves/new.rhtml index a8a053c4c..48d62da48 100644 --- a/app/views/issue_moves/new.rhtml +++ b/app/views/issue_moves/new.rhtml @@ -39,7 +39,7 @@ <%= select_tag('assigned_to_id', content_tag('option', l(:label_no_change_option), :value => '') + 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)) %>

diff --git a/app/views/issues/_attributes.rhtml b/app/views/issues/_attributes.rhtml index 555a65653..e2d04ee51 100644 --- a/app/views/issues/_attributes.rhtml +++ b/app/views/issues/_attributes.rhtml @@ -8,7 +8,7 @@ <% end %>

<%= f.select :priority_id, (@priorities.collect {|p| [p.name, p.id]}), {:required => true}, :disabled => !@issue.leaf? %>

-

<%= f.select :assigned_to_id, (@issue.assignable_users.collect {|m| [m.name, m.id]}), :include_blank => true %>

+

<%= f.select :assigned_to_id, principals_options_for_select(@issue.assignable_users, @issue.assigned_to), :include_blank => true %>

<% unless @project.issue_categories.empty? %>

<%= 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;'), diff --git a/app/views/issues/_form_update.rhtml b/app/views/issues/_form_update.rhtml index e29c41a7c..7c96317f7 100644 --- a/app/views/issues/_form_update.rhtml +++ b/app/views/issues/_form_update.rhtml @@ -1,7 +1,7 @@

<%= f.select :status_id, (@allowed_statuses.collect {|p| [p.name, p.id]}), :required => true %>

-

<%= f.select :assigned_to_id, (@issue.assignable_users.collect {|m| [m.name, m.id]}), :include_blank => true %>

+

<%= f.select :assigned_to_id, principals_options_for_select(@issue.assignable_users, @issue.assigned_to), :include_blank => true %>

<% if Issue.use_field_for_done_ratio? %> diff --git a/app/views/issues/bulk_edit.rhtml b/app/views/issues/bulk_edit.rhtml index eebce21b4..37b50ee86 100644 --- a/app/views/issues/bulk_edit.rhtml +++ b/app/views/issues/bulk_edit.rhtml @@ -27,7 +27,7 @@ <%= select_tag('issue[assigned_to_id]', content_tag('option', l(:label_no_change_option), :value => '') + content_tag('option', l(:label_nobody), :value => 'none') + - options_from_collection_for_select(@assignables, :id, :name)) %> + principals_options_for_select(@assignables)) %>

<% if @project %>

diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index 1d82dcb0e..d2e7cee62 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -675,4 +675,27 @@ RAW assert_equal %(eCookbook), link_to_project(project, {:action => 'settings'}, :class => "project") end + + def test_principals_options_for_select_with_users + users = [User.find(2), User.find(4)] + assert_equal %(), + principals_options_for_select(users) + end + + def test_principals_options_for_select_with_selected + users = [User.find(2), User.find(4)] + assert_equal %(), + 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 %() + + %(), + principals_options_for_select(users) + end + + def test_principals_options_for_select_with_empty_collection + assert_equal '', principals_options_for_select([]) + end end -- 2.39.5