@@ -36,8 +36,11 class RolesController < ApplicationController | |||
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def new |
|
39 | # Prefills the form with 'Non member' role permissions | |
|
39 | # Prefills the form with 'Non member' role permissions by default | |
|
40 | 40 | @role = Role.new(params[:role] || {:permissions => Role.non_member.permissions}) |
|
41 | if params[:copy].present? && @copy_from = Role.find_by_id(params[:copy]) | |
|
42 | @role.copy_from(@copy_from) | |
|
43 | end | |
|
41 | 44 | @roles = Role.sorted.all |
|
42 | 45 | end |
|
43 | 46 |
@@ -67,6 +67,15 class Role < ActiveRecord::Base | |||
|
67 | 67 | :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first), |
|
68 | 68 | :if => lambda {|role| role.respond_to?(:issues_visibility)} |
|
69 | 69 | |
|
70 | # Copies attributes from another role, arg can be an id or a Role | |
|
71 | def copy_from(arg, options={}) | |
|
72 | return unless arg.present? | |
|
73 | role = arg.is_a?(Role) ? arg : Role.find_by_id(arg.to_s) | |
|
74 | self.attributes = role.attributes.dup.except("id", "name", "position", "builtin", "permissions") | |
|
75 | self.permissions = role.permissions.dup | |
|
76 | self | |
|
77 | end | |
|
78 | ||
|
70 | 79 | def permissions=(perms) |
|
71 | 80 | perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms |
|
72 | 81 | write_attribute(:permissions, perms) |
@@ -8,7 +8,7 | |||
|
8 | 8 | <p><%= f.select :issues_visibility, Role::ISSUES_VISIBILITY_OPTIONS.collect {|v| [l(v.last), v.first]} %></p> |
|
9 | 9 | <% if @role.new_record? && @roles.any? %> |
|
10 | 10 | <p><label for="copy_workflow_from"><%= l(:label_copy_workflow_from) %></label> |
|
11 | <%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@roles, :id, :name)) %></p> | |
|
11 | <%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@roles, :id, :name, params[:copy_workflow_from] || @copy_from.try(:id))) %></p> | |
|
12 | 12 | <% end %> |
|
13 | 13 | </div> |
|
14 | 14 |
@@ -21,6 +21,7 | |||
|
21 | 21 | <% end %> |
|
22 | 22 | </td> |
|
23 | 23 | <td class="buttons"> |
|
24 | <%= link_to l(:button_copy), new_role_path(:copy => role), :class => 'icon icon-copy' %> | |
|
24 | 25 | <%= delete_link role_path(role) unless role.builtin? %> |
|
25 | 26 | </td> |
|
26 | 27 | </tr> |
@@ -46,6 +46,31 class RolesControllerTest < ActionController::TestCase | |||
|
46 | 46 | assert_template 'new' |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | def test_new_with_copy | |
|
50 | copy_from = Role.find(2) | |
|
51 | ||
|
52 | get :new, :copy => copy_from.id.to_s | |
|
53 | assert_response :success | |
|
54 | assert_template 'new' | |
|
55 | ||
|
56 | role = assigns(:role) | |
|
57 | assert_equal copy_from.permissions, role.permissions | |
|
58 | ||
|
59 | assert_select 'form' do | |
|
60 | # blank name | |
|
61 | assert_select 'input[name=?][value=]', 'role[name]' | |
|
62 | # edit_project permission checked | |
|
63 | assert_select 'input[type=checkbox][name=?][value=edit_project][checked=checked]', 'role[permissions][]' | |
|
64 | # add_project permission not checked | |
|
65 | assert_select 'input[type=checkbox][name=?][value=add_project]', 'role[permissions][]' | |
|
66 | assert_select 'input[type=checkbox][name=?][value=add_project][checked=checked]', 'role[permissions][]', 0 | |
|
67 | # workflow copy selected | |
|
68 | assert_select 'select[name=?]', 'copy_workflow_from' do | |
|
69 | assert_select 'option[value=2][selected=selected]' | |
|
70 | end | |
|
71 | end | |
|
72 | end | |
|
73 | ||
|
49 | 74 | def test_create_with_validaton_failure |
|
50 | 75 | post :create, :role => {:name => '', |
|
51 | 76 | :permissions => ['add_issues', 'edit_issues', 'log_time', ''], |
@@ -33,6 +33,18 class RoleTest < ActiveSupport::TestCase | |||
|
33 | 33 | assert_equal Role.all.reject(&:builtin?).sort, Role.builtin(false).all.sort |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | def test_copy_from | |
|
37 | role = Role.find(1) | |
|
38 | copy = Role.new.copy_from(role) | |
|
39 | ||
|
40 | assert_nil copy.id | |
|
41 | assert_equal '', copy.name | |
|
42 | assert_equal role.permissions, copy.permissions | |
|
43 | ||
|
44 | copy.name = 'Copy' | |
|
45 | assert copy.save | |
|
46 | end | |
|
47 | ||
|
36 | 48 | def test_copy_workflows |
|
37 | 49 | source = Role.find(1) |
|
38 | 50 | assert_equal 90, source.workflow_rules.size |
General Comments 0
You need to be logged in to leave comments.
Login now