diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index 790eb28..5f38852 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -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 diff --git a/app/models/role.rb b/app/models/role.rb index 412e5a6..5fd4376 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -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) diff --git a/app/views/roles/_form.html.erb b/app/views/roles/_form.html.erb index 45f8b0d..8ae0a60 100644 --- a/app/views/roles/_form.html.erb +++ b/app/views/roles/_form.html.erb @@ -8,7 +8,7 @@
<%= f.select :issues_visibility, Role::ISSUES_VISIBILITY_OPTIONS.collect {|v| [l(v.last), v.first]} %>
<% if @role.new_record? && @roles.any? %>-<%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@roles, :id, :name)) %>
+<%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@roles, :id, :name, params[:copy_workflow_from] || @copy_from.try(:id))) %> <% end %> diff --git a/app/views/roles/index.html.erb b/app/views/roles/index.html.erb index 4a09cdf..5326c56 100644 --- a/app/views/roles/index.html.erb +++ b/app/views/roles/index.html.erb @@ -21,6 +21,7 @@ <% end %>