summaryrefslogtreecommitdiffstats
path: root/app/models/workflow_permission.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2014-02-25 02:54:47 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2014-02-25 02:54:47 +0000
commitb6c794d16b47bf353a1a2dfc00e9cbd078525ee8 (patch)
tree7dec97be4c38b4e3b6321f7c55b81697ca6f5ee2 /app/models/workflow_permission.rb
parentc6f71f727bc73188ce7285c6e19bf50553246ec7 (diff)
downloadredmine-b6c794d16b47bf353a1a2dfc00e9cbd078525ee8.tar.gz
redmine-b6c794d16b47bf353a1a2dfc00e9cbd078525ee8.zip
Bulk edit workflows for multiple trackers/roles (#16164).
git-svn-id: http://svn.redmine.org/redmine/trunk@12924 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/workflow_permission.rb')
-rw-r--r--app/models/workflow_permission.rb43
1 files changed, 33 insertions, 10 deletions
diff --git a/app/models/workflow_permission.rb b/app/models/workflow_permission.rb
index 0338a3ea1..bf4430ec5 100644
--- a/app/models/workflow_permission.rb
+++ b/app/models/workflow_permission.rb
@@ -19,20 +19,43 @@ class WorkflowPermission < WorkflowRule
validates_inclusion_of :rule, :in => %w(readonly required)
validate :validate_field_name
- # Replaces the workflow permissions for the given tracker and role
+ # Returns the workflow permissions for the given trackers and roles
+ # grouped by status_id
#
# Example:
- # WorkflowPermission.replace_permissions role, tracker, {'due_date' => {'1' => 'readonly', '2' => 'required'}}
- def self.replace_permissions(tracker, role, permissions)
- destroy_all(:tracker_id => tracker.id, :role_id => role.id)
+ # WorkflowPermission.rules_by_status_id trackers, roles
+ # # => {1 => {'start_date' => 'required', 'due_date' => 'readonly'}}
+ def self.rules_by_status_id(trackers, roles)
+ WorkflowPermission.where(:tracker_id => trackers.map(&:id), :role_id => roles.map(&:id)).inject({}) do |h, w|
+ h[w.old_status_id] ||= {}
+ h[w.old_status_id][w.field_name] ||= []
+ h[w.old_status_id][w.field_name] << w.rule
+ h
+ end
+ end
- permissions.each { |field, rule_by_status_id|
- rule_by_status_id.each { |status_id, rule|
- if rule.present?
- WorkflowPermission.create(:role_id => role.id, :tracker_id => tracker.id, :old_status_id => status_id, :field_name => field, :rule => rule)
- end
+ # Replaces the workflow permissions for the given trackers and roles
+ #
+ # Example:
+ # WorkflowPermission.replace_permissions trackers, roles, {'1' => {'start_date' => 'required', 'due_date' => 'readonly'}}
+ def self.replace_permissions(trackers, roles, permissions)
+ trackers = Array.wrap trackers
+ roles = Array.wrap roles
+
+ transaction do
+ permissions.each { |status_id, rule_by_field|
+ rule_by_field.each { |field, rule|
+ destroy_all(:tracker_id => trackers.map(&:id), :role_id => roles.map(&:id), :old_status_id => status_id, :field_name => field)
+ if rule.present?
+ trackers.each do |tracker|
+ roles.each do |role|
+ WorkflowPermission.create(:role_id => role.id, :tracker_id => tracker.id, :old_status_id => status_id, :field_name => field, :rule => rule)
+ end
+ end
+ end
+ }
}
- }
+ end
end
protected