Skip to content

Commit

Permalink
Workflow enhancement: editable and required fields configurable by ro…
Browse files Browse the repository at this point in the history
…le, tracker and status (#703, #3521).

git-svn-id: svn://rubyforge.org/var/svn/redmine/trunk@9977 e93f8b46-1217-0410-a6f0-8f06a7374b81
  • Loading branch information
jplang committed Jul 15, 2012
1 parent 18a463e commit 16e9a81
Show file tree
Hide file tree
Showing 35 changed files with 1,405 additions and 431 deletions.
6 changes: 1 addition & 5 deletions app/controllers/issues_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ def new
format.html { render :action => 'new', :layout => !request.xhr? }
format.js {
render(:update) { |page|
if params[:project_change]
page.replace_html 'all_attributes', :partial => 'form'
else
page.replace_html 'attributes', :partial => 'attributes'
end
page.replace_html 'all_attributes', :partial => 'form'
m = User.current.allowed_to?(:log_time, @issue.project) ? 'show' : 'hide'
page << "if ($('log_time')) {Element.#{m}('log_time');}"
}
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/roles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def create
if request.post? && @role.save
# workflow copy
if !params[:copy_workflow_from].blank? && (copy_from = Role.find_by_id(params[:copy_workflow_from]))
@role.workflows.copy(copy_from)
@role.workflow_rules.copy(copy_from)
end
flash[:notice] = l(:notice_successful_create)
redirect_to :action => 'index'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/trackers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create
if request.post? and @tracker.save
# workflow copy
if !params[:copy_workflow_from].blank? && (copy_from = Tracker.find_by_id(params[:copy_workflow_from]))
@tracker.workflows.copy(copy_from)
@tracker.workflow_rules.copy(copy_from)
end
flash[:notice] = l(:notice_successful_create)
redirect_to :action => 'index'
Expand Down
42 changes: 36 additions & 6 deletions app/controllers/workflows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,23 @@ class WorkflowsController < ApplicationController
before_filter :find_trackers

def index
@workflow_counts = Workflow.count_by_tracker_and_role
@workflow_counts = WorkflowTransition.count_by_tracker_and_role
end

def edit
@role = Role.find_by_id(params[:role_id])
@tracker = Tracker.find_by_id(params[:tracker_id])

if request.post?
Workflow.destroy_all( ["role_id=? and tracker_id=?", @role.id, @tracker.id])
WorkflowTransition.destroy_all( ["role_id=? and tracker_id=?", @role.id, @tracker.id])
(params[:issue_status] || []).each { |status_id, transitions|
transitions.each { |new_status_id, options|
author = options.is_a?(Array) && options.include?('author') && !options.include?('always')
assignee = options.is_a?(Array) && options.include?('assignee') && !options.include?('always')
@role.workflows.build(:tracker_id => @tracker.id, :old_status_id => status_id, :new_status_id => new_status_id, :author => author, :assignee => assignee)
WorkflowTransition.create(:role_id => @role.id, :tracker_id => @tracker.id, :old_status_id => status_id, :new_status_id => new_status_id, :author => author, :assignee => assignee)
}
}
if @role.save
flash[:notice] = l(:notice_successful_update)
redirect_to :action => 'edit', :role_id => @role, :tracker_id => @tracker
return
end
Expand All @@ -53,14 +52,45 @@ def edit
@statuses ||= IssueStatus.find(:all, :order => 'position')

if @tracker && @role && @statuses.any?
workflows = Workflow.all(:conditions => {:role_id => @role.id, :tracker_id => @tracker.id})
workflows = WorkflowTransition.all(:conditions => {:role_id => @role.id, :tracker_id => @tracker.id})
@workflows = {}
@workflows['always'] = workflows.select {|w| !w.author && !w.assignee}
@workflows['author'] = workflows.select {|w| w.author}
@workflows['assignee'] = workflows.select {|w| w.assignee}
end
end

def permissions
@role = Role.find_by_id(params[:role_id])
@tracker = Tracker.find_by_id(params[:tracker_id])

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
}
}
redirect_to :action => 'permissions', :role_id => @role, :tracker_id => @tracker
return
end

@statuses = @tracker.issue_statuses
@fields = (Tracker::CORE_FIELDS_ALL - @tracker.disabled_core_fields).map {|field| [field, l("field_"+field.sub(/_id$/, ''))]}
@custom_fields = @tracker.custom_fields

@permissions = WorkflowPermission.where(:tracker_id => @tracker.id, :role_id => @role.id).all.inject({}) do |h, w|
h[w.old_status_id] ||= {}
h[w.old_status_id][w.field_name] = w.rule
h
end
@statuses.each {|status| @permissions[status.id] ||= {}}
end
end

def copy

if params[:source_tracker_id].blank? || params[:source_tracker_id] == 'any'
Expand All @@ -83,7 +113,7 @@ def copy
elsif @target_trackers.nil? || @target_roles.nil?
flash.now[:error] = l(:error_workflow_copy_target)
else
Workflow.copy(@source_tracker, @source_role, @target_trackers, @target_roles)
WorkflowRule.copy(@source_tracker, @source_role, @target_trackers, @target_roles)
flash[:notice] = l(:notice_successful_update)
redirect_to :action => 'copy', :source_tracker_id => @source_tracker, :source_role_id => @source_role
end
Expand Down
12 changes: 7 additions & 5 deletions app/helpers/custom_fields_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ def custom_field_tag(name, custom_value)
end

# Return custom field label tag
def custom_field_label_tag(name, custom_value)
def custom_field_label_tag(name, custom_value, options={})
required = options[:required] || custom_value.custom_field.is_required?

content_tag "label", h(custom_value.custom_field.name) +
(custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>".html_safe : ""),
:for => "#{name}_custom_field_values_#{custom_value.custom_field.id}"
(required ? " <span class=\"required\">*</span>".html_safe : ""),
:for => "#{name}_custom_field_values_#{custom_value.custom_field.id}"
end

# Return custom field tag with its label tag
def custom_field_tag_with_label(name, custom_value)
custom_field_label_tag(name, custom_value) + custom_field_tag(name, custom_value)
def custom_field_tag_with_label(name, custom_value, options={})
custom_field_label_tag(name, custom_value, options) + custom_field_tag(name, custom_value)
end

def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil)
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/workflows_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

module WorkflowsHelper
def field_permission_tag(permissions, status, field)
name = field.is_a?(CustomField) ? field.id.to_s : field
select_tag("permissions[#{name}][#{status.id}]",
options_for_select([["", ""], ["Read-only", "readonly"], ["Required", "required"]], permissions[status.id][name])
)
end
end
126 changes: 116 additions & 10 deletions app/models/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Issue < ActiveRecord::Base
validates_length_of :subject, :maximum => 255
validates_inclusion_of :done_ratio, :in => 0..100
validates_numericality_of :estimated_hours, :allow_nil => true
validate :validate_issue
validate :validate_issue, :validate_required_fields

scope :visible,
lambda {|*args| { :include => :project,
Expand Down Expand Up @@ -146,6 +146,11 @@ def destroy
super
end

def reload(*args)
@workflow_rule_by_attribute = nil
super
end

# Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
def available_custom_fields
(project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : []
Expand Down Expand Up @@ -208,7 +213,9 @@ def move_to_project(new_project, new_tracker=nil, options={})

def status_id=(sid)
self.status = nil
write_attribute(:status_id, sid)
result = write_attribute(:status_id, sid)
@workflow_rule_by_attribute = nil
result
end

def priority_id=(pid)
Expand All @@ -230,6 +237,7 @@ def tracker_id=(tid)
self.tracker = nil
result = write_attribute(:tracker_id, tid)
@custom_field_values = nil
@workflow_rule_by_attribute = nil
result
end

Expand Down Expand Up @@ -336,9 +344,10 @@ def estimated_hours=(h)
:if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) &&
user.allowed_to?(:manage_subtasks, issue.project)}

def safe_attribute_names(*args)
names = super(*args)
def safe_attribute_names(user=nil)
names = super
names -= disabled_core_fields
names -= read_only_attribute_names(user)
names
end

Expand All @@ -362,15 +371,15 @@ def safe_attributes=(attrs, user=User.current)
self.tracker_id = t
end

attrs = delete_unsafe_attributes(attrs, user)
return if attrs.empty?

if attrs['status_id']
unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i)
attrs.delete('status_id')
if (s = attrs.delete('status_id')) && safe_attribute?('status_id')
if new_statuses_allowed_to(user).collect(&:id).include?(s.to_i)
self.status_id = s
end
end

attrs = delete_unsafe_attributes(attrs, user)
return if attrs.empty?

unless leaf?
attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)}
end
Expand All @@ -379,6 +388,14 @@ def safe_attributes=(attrs, user=User.current)
attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'].to_i)
end

if attrs['custom_field_values'].present?
attrs['custom_field_values'] = attrs['custom_field_values'].reject {|k, v| read_only_attribute_names(user).include? k.to_s}
end

if attrs['custom_fields'].present?
attrs['custom_fields'] = attrs['custom_fields'].reject {|c| read_only_attribute_names(user).include? c['id'].to_s}
end

# mass-assignment security bypass
assign_attributes attrs, :without_protection => true
end
Expand All @@ -387,6 +404,76 @@ def disabled_core_fields
tracker ? tracker.disabled_core_fields : []
end

# Returns the custom_field_values that can be edited by the given user
def editable_custom_field_values(user=nil)
custom_field_values.reject do |value|
read_only_attribute_names(user).include?(value.custom_field_id.to_s)
end
end

# Returns the names of attributes that are read-only for user or the current user
# For users with multiple roles, the read-only fields are the intersection of
# read-only fields of each role
# The result is an array of strings where sustom fields are represented with their ids
#
# Examples:
# issue.read_only_attribute_names # => ['due_date', '2']
# issue.read_only_attribute_names(user) # => []
def read_only_attribute_names(user=nil)
workflow_rule_by_attribute(user).select {|attr, rule| rule == 'readonly'}.keys
end

# Returns the names of required attributes for user or the current user
# For users with multiple roles, the required fields are the intersection of
# required fields of each role
# The result is an array of strings where sustom fields are represented with their ids
#
# Examples:
# issue.required_attribute_names # => ['due_date', '2']
# issue.required_attribute_names(user) # => []
def required_attribute_names(user=nil)
workflow_rule_by_attribute(user).select {|attr, rule| rule == 'required'}.keys
end

# Returns true if the attribute is required for user
def required_attribute?(name, user=nil)
required_attribute_names(user).include?(name.to_s)
end

# Returns a hash of the workflow rule by attribute for the given user
#
# Examples:
# issue.workflow_rule_by_attribute # => {'due_date' => 'required', 'start_date' => 'readonly'}
def workflow_rule_by_attribute(user=nil)
return @workflow_rule_by_attribute if @workflow_rule_by_attribute && user.nil?

user_real = user || User.current
roles = user_real.admin ? Role.all : user_real.roles_for_project(project)
return {} if roles.empty?

result = {}
workflow_permissions = WorkflowPermission.where(:tracker_id => tracker_id, :old_status_id => status_id, :role_id => roles.map(&:id)).all
if workflow_permissions.any?
workflow_rules = workflow_permissions.inject({}) do |h, wp|
h[wp.field_name] ||= []
h[wp.field_name] << wp.rule
h
end
workflow_rules.each do |attr, rules|
next if rules.size < roles.size
uniq_rules = rules.uniq
if uniq_rules.size == 1
result[attr] = uniq_rules.first
else
result[attr] = 'required'
end
end
end
@workflow_rule_by_attribute = result if user.nil?
result
end
private :workflow_rule_by_attribute

def done_ratio
if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
status.default_done_ratio
Expand Down Expand Up @@ -448,6 +535,25 @@ def validate_issue
end
end

# Validates the issue against additional workflow requirements
def validate_required_fields
user = new_record? ? author : current_journal.try(:user)

required_attribute_names(user).each do |attribute|
if attribute =~ /^\d+$/
attribute = attribute.to_i
v = custom_field_values.detect {|v| v.custom_field_id == attribute }
if v && v.value.blank?
errors.add :base, v.custom_field.name + ' ' + l('activerecord.errors.messages.blank')
end
else
if respond_to?(attribute) && send(attribute).blank?
errors.add attribute, :blank
end
end
end
end

# Set the done_ratio using the status if that setting is set. This will keep the done_ratios
# even if the user turns off the setting later
def update_done_ratio_from_issue_status
Expand Down
8 changes: 4 additions & 4 deletions app/models/issue_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

class IssueStatus < ActiveRecord::Base
before_destroy :check_integrity
has_many :workflows, :foreign_key => "old_status_id"
has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id"
acts_as_list

before_destroy :delete_workflows
before_destroy :delete_workflow_rules
after_save :update_default

validates_presence_of :name
Expand Down Expand Up @@ -98,7 +98,7 @@ def check_integrity
end

# Deletes associated workflows
def delete_workflows
Workflow.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
def delete_workflow_rules
WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
end
end
4 changes: 2 additions & 2 deletions app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def self.dump(value)
}

before_destroy :check_deletable
has_many :workflows, :dependent => :delete_all do
has_many :workflow_rules, :dependent => :delete_all do
def copy(source_role)
Workflow.copy(nil, source_role, nil, proxy_association.owner)
WorkflowRule.copy(nil, source_role, nil, proxy_association.owner)
end
end

Expand Down
Loading

0 comments on commit 16e9a81

Please sign in to comment.