if @role && @tracker
if request.post?
- WorkflowPermission.destroy_all({:role_id => @role.id, :tracker_id => @tracker.id})
- (params[: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
- }
- }
+ WorkflowPermission.replace_permissions(@tracker, @role, params[:permissions] || {})
redirect_to :action => 'permissions', :role_id => @role, :tracker_id => @tracker
return
end
validates_inclusion_of :rule, :in => %w(readonly required)
validate :validate_field_name
+ # Replaces the workflow permissions for the given tracker and role
+ #
+ # 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)
+
+ 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
+ }
+ }
+ end
+
protected
def validate_field_name