]> source.dussan.org Git - redmine.git/commitdiff
Create role by copy (#9258).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 3 Sep 2012 17:04:28 +0000 (17:04 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 3 Sep 2012 17:04:28 +0000 (17:04 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10285 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/roles_controller.rb
app/models/role.rb
app/views/roles/_form.html.erb
app/views/roles/index.html.erb
test/functional/roles_controller_test.rb
test/unit/role_test.rb

index 790eb28d4ac6b7ba7687e415f414b87ba025e6bd..5f38852670191c296b0dbc49fef1053a17ad9e1d 100644 (file)
@@ -36,8 +36,11 @@ class RolesController < ApplicationController
   end
 
   def new
-    # Prefills the form with 'Non member' role permissions
+    # Prefills the form with 'Non member' role permissions by default
     @role = Role.new(params[:role] || {:permissions => Role.non_member.permissions})
+    if params[:copy].present? && @copy_from = Role.find_by_id(params[:copy])
+      @role.copy_from(@copy_from)
+    end
     @roles = Role.sorted.all
   end
 
index 412e5a63cd759a8cf00d8589a37050ec29e80c7a..5fd437648e753b6ad53ac37e519bf0e56598e43a 100644 (file)
@@ -67,6 +67,15 @@ class Role < ActiveRecord::Base
     :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first),
     :if => lambda {|role| role.respond_to?(:issues_visibility)}
 
+  # Copies attributes from another role, arg can be an id or a Role
+  def copy_from(arg, options={})
+    return unless arg.present?
+    role = arg.is_a?(Role) ? arg : Role.find_by_id(arg.to_s)
+    self.attributes = role.attributes.dup.except("id", "name", "position", "builtin", "permissions")
+    self.permissions = role.permissions.dup
+    self
+  end
+
   def permissions=(perms)
     perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
     write_attribute(:permissions, perms)
index 45f8b0d86f14e3a0ec85bd5401157977e0fec0b5..8ae0a604fe2ef2ba7e9e92e3152057da244c8607 100644 (file)
@@ -8,7 +8,7 @@
 <p><%= f.select :issues_visibility, Role::ISSUES_VISIBILITY_OPTIONS.collect {|v| [l(v.last), v.first]} %></p>
 <% if @role.new_record? && @roles.any? %>
 <p><label for="copy_workflow_from"><%= l(:label_copy_workflow_from) %></label>
-<%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@roles, :id, :name)) %></p>
+<%= 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>
 <% end %>
 </div>
 
index 4a09cdfdd3531d412b5eb1b7bf3085e177ebea21..5326c5638fca6c02e4b205f144d519d2c8608240 100644 (file)
@@ -21,6 +21,7 @@
   <% end %>
   </td>
   <td class="buttons">
+    <%= link_to l(:button_copy), new_role_path(:copy => role), :class => 'icon icon-copy' %>
     <%= delete_link role_path(role) unless role.builtin? %>
   </td>
   </tr>
index 327fad51211ecd3b3ddf7a88254984df4f681aef..868c987a6209b9bb2d1a81b76dc8c4c83b07c51e 100644 (file)
@@ -46,6 +46,31 @@ class RolesControllerTest < ActionController::TestCase
     assert_template 'new'
   end
 
+  def test_new_with_copy
+    copy_from = Role.find(2)
+
+    get :new, :copy => copy_from.id.to_s
+    assert_response :success
+    assert_template 'new'
+
+    role = assigns(:role)
+    assert_equal copy_from.permissions, role.permissions
+
+    assert_select 'form' do
+      # blank name
+      assert_select 'input[name=?][value=]', 'role[name]'
+      # edit_project permission checked
+      assert_select 'input[type=checkbox][name=?][value=edit_project][checked=checked]', 'role[permissions][]'
+      # add_project permission not checked
+      assert_select 'input[type=checkbox][name=?][value=add_project]', 'role[permissions][]'
+      assert_select 'input[type=checkbox][name=?][value=add_project][checked=checked]', 'role[permissions][]', 0
+      # workflow copy selected
+      assert_select 'select[name=?]', 'copy_workflow_from' do
+        assert_select 'option[value=2][selected=selected]'
+      end
+    end
+  end
+
   def test_create_with_validaton_failure
     post :create, :role => {:name => '',
                          :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
index e61280fc90595006d189839da0ad1eaadcbda4c9..e93d76c84e02e4a348b4484b6314e1bf8811597e 100644 (file)
@@ -33,6 +33,18 @@ class RoleTest < ActiveSupport::TestCase
     assert_equal Role.all.reject(&:builtin?).sort, Role.builtin(false).all.sort
   end
 
+  def test_copy_from
+    role = Role.find(1)
+    copy = Role.new.copy_from(role)
+
+    assert_nil copy.id
+    assert_equal '', copy.name
+    assert_equal role.permissions, copy.permissions
+
+    copy.name = 'Copy'
+    assert copy.save
+  end
+
   def test_copy_workflows
     source = Role.find(1)
     assert_equal 90, source.workflow_rules.size