1
0
mirror of https://github.com/meineerde/redmine.git synced 2025-12-23 17:01:13 +00:00

shorten long line of app/models/issue.rb

git-svn-id: http://svn.redmine.org/redmine/trunk@20663 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2020-12-17 13:11:05 +00:00
parent c3054233ab
commit ca099daae2

View File

@ -110,7 +110,11 @@ class Issue < ActiveRecord::Base
before_validation :clear_disabled_fields before_validation :clear_disabled_fields
before_save :close_duplicates, :update_done_ratio_from_issue_status, before_save :close_duplicates, :update_done_ratio_from_issue_status,
:force_updated_on_change, :update_closed_on :force_updated_on_change, :update_closed_on
after_save {|issue| issue.send :after_project_change if !issue.saved_change_to_id? && issue.saved_change_to_project_id?} after_save do |issue|
if !issue.saved_change_to_id? && issue.saved_change_to_project_id?
issue.send :after_project_change
end
end
after_save :reschedule_following_issues, :update_nested_set_attributes, after_save :reschedule_following_issues, :update_nested_set_attributes,
:update_parent_attributes, :delete_selected_attachments, :create_journal :update_parent_attributes, :delete_selected_attachments, :create_journal
# Should be after_create but would be called before previous after_save callbacks # Should be after_create but would be called before previous after_save callbacks
@ -128,10 +132,13 @@ class Issue < ActiveRecord::Base
'1=1' '1=1'
when 'default' when 'default'
user_ids = [user.id] + user.groups.pluck(:id).compact user_ids = [user.id] + user.groups.pluck(:id).compact
"(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" "(#{table_name}.is_private = #{connection.quoted_false} " \
"OR #{table_name}.author_id = #{user.id} " \
"OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
when 'own' when 'own'
user_ids = [user.id] + user.groups.pluck(:id).compact user_ids = [user.id] + user.groups.pluck(:id).compact
"(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" "(#{table_name}.author_id = #{user.id} OR " \
"#{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
else else
'1=0' '1=0'
end end
@ -273,8 +280,16 @@ class Issue < ActiveRecord::Base
# Copies attributes from another issue, arg can be an id or an Issue # Copies attributes from another issue, arg can be an id or an Issue
def copy_from(arg, options={}) def copy_from(arg, options={})
issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg) issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on", "status_id", "closed_on") self.attributes =
self.custom_field_values = issue.custom_field_values.inject({}) {|h, v| h[v.custom_field_id] = v.value; h} issue.attributes.dup.except(
"id", "root_id", "parent_id", "lft", "rgt",
"created_on", "updated_on", "status_id", "closed_on"
)
self.custom_field_values =
issue.custom_field_values.inject({}) do |h, v|
h[v.custom_field_id] = v.value
h
end
if options[:keep_status] if options[:keep_status]
self.status = issue.status self.status = issue.status
end end
@ -662,7 +677,11 @@ class Issue < ActiveRecord::Base
return {} if roles.empty? return {} if roles.empty?
result = {} result = {}
workflow_permissions = WorkflowPermission.where(:tracker_id => tracker_id, :old_status_id => status_id, :role_id => roles.map(&:id)).to_a workflow_permissions =
WorkflowPermission.where(
:tracker_id => tracker_id, :old_status_id => status_id,
:role_id => roles.map(&:id)
).to_a
if workflow_permissions.any? if workflow_permissions.any?
workflow_rules = workflow_permissions.inject({}) do |h, wp| workflow_rules = workflow_permissions.inject({}) do |h, wp|
h[wp.field_name] ||= {} h[wp.field_name] ||= {}
@ -670,7 +689,9 @@ class Issue < ActiveRecord::Base
h h
end end
fields_with_roles = {} fields_with_roles = {}
IssueCustomField.where(:visible => false).joins(:roles).pluck(:id, "role_id").each do |field_id, role_id| IssueCustomField.where(:visible => false).
joins(:roles).pluck(:id, "role_id").
each do |field_id, role_id|
fields_with_roles[field_id] ||= [] fields_with_roles[field_id] ||= []
fields_with_roles[field_id] << role_id fields_with_roles[field_id] << role_id
end end
@ -753,7 +774,12 @@ class Issue < ActiveRecord::Base
errors.add :parent_issue_id, :invalid errors.add :parent_issue_id, :invalid
elsif (@parent_issue != parent) && ( elsif (@parent_issue != parent) && (
self.would_reschedule?(@parent_issue) || self.would_reschedule?(@parent_issue) ||
@parent_issue.self_and_ancestors.any? {|a| a.relations_from.any? {|r| r.relation_type == IssueRelation::TYPE_PRECEDES && r.issue_to.would_reschedule?(self)}} @parent_issue.self_and_ancestors.any? do |a|
a.relations_from.any? do |r|
r.relation_type == IssueRelation::TYPE_PRECEDES &&
r.issue_to.would_reschedule?(self)
end
end
) )
errors.add :parent_issue_id, :invalid errors.add :parent_issue_id, :invalid
elsif !closed? && @parent_issue.closed? elsif !closed? && @parent_issue.closed?
@ -1056,7 +1082,13 @@ class Issue < ActiveRecord::Base
# Returns the previous assignee whenever we're before the save # Returns the previous assignee whenever we're before the save
# or in after_* callbacks # or in after_* callbacks
def previous_assignee def previous_assignee
if previous_assigned_to_id = assigned_to_id_change_to_be_saved.nil? ? assigned_to_id_before_last_save : assigned_to_id_in_database previous_assigned_to_id =
if assigned_to_id_change_to_be_saved.nil?
assigned_to_id_before_last_save
else
assigned_to_id_in_database
end
if previous_assigned_to_id
Principal.find_by_id(previous_assigned_to_id) Principal.find_by_id(previous_assigned_to_id)
end end
end end
@ -1137,9 +1169,15 @@ class Issue < ActiveRecord::Base
# Preloads relations for a collection of issues # Preloads relations for a collection of issues
def self.load_relations(issues) def self.load_relations(issues)
if issues.any? if issues.any?
relations = IssueRelation.where("issue_from_id IN (:ids) OR issue_to_id IN (:ids)", :ids => issues.map(&:id)).all relations =
IssueRelation.where(
"issue_from_id IN (:ids) OR issue_to_id IN (:ids)", :ids => issues.map(&:id)
).all
issues.each do |issue| issues.each do |issue|
issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id} issue.instance_variable_set(
"@relations",
relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id}
)
end end
end end
end end
@ -1247,7 +1285,10 @@ class Issue < ActiveRecord::Base
all = [self] all = [self]
last = [self] last = [self]
while last.any? while last.any?
current = last.map {|i| i.relations_from.where(:relation_type => IssueRelation::TYPE_BLOCKS).map(&:issue_to)}.flatten.uniq current =
last.map do |i|
i.relations_from.where(:relation_type => IssueRelation::TYPE_BLOCKS).map(&:issue_to)
end.flatten.uniq
current -= last current -= last
current -= all current -= all
return true if current.include?(other) return true if current.include?(other)
@ -1483,8 +1524,13 @@ class Issue < ActiveRecord::Base
Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)", Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)",
:lft => project.root.lft, :rgt => project.root.rgt) :lft => project.root.lft, :rgt => project.root.rgt)
when 'hierarchy' when 'hierarchy'
Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt) OR (#{Project.table_name}.lft < :lft AND #{Project.table_name}.rgt > :rgt)", Issue.joins(:project).
:lft => project.lft, :rgt => project.rgt) where(
"(#{Project.table_name}.lft >= :lft AND " \
"#{Project.table_name}.rgt <= :rgt) OR " \
"(#{Project.table_name}.lft < :lft AND #{Project.table_name}.rgt > :rgt)",
:lft => project.lft, :rgt => project.rgt
)
when 'descendants' when 'descendants'
Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)", Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)",
:lft => project.lft, :rgt => project.rgt) :lft => project.lft, :rgt => project.rgt)
@ -1612,7 +1658,10 @@ class Issue < ActiveRecord::Base
true true
else else
roles = user.roles_for_project(project).select {|r| r.has_permission?(permission)} roles = user.roles_for_project(project).select {|r| r.has_permission?(permission)}
roles.any? {|r| r.permissions_all_trackers?(permission) || r.permissions_tracker_ids?(permission, tracker_id)} roles.any? do |r|
r.permissions_all_trackers?(permission) ||
r.permissions_tracker_ids?(permission, tracker_id)
end
end end
end end
@ -1633,7 +1682,12 @@ class Issue < ActiveRecord::Base
# Change project and keep project # Change project and keep project
child.send :project=, project, true child.send :project=, project, true
unless child.save unless child.save
errors.add :base, l(:error_move_of_child_not_possible, :child => "##{child.id}", :errors => child.errors.full_messages.join(", ")) errors.add(
:base,
l(:error_move_of_child_not_possible,
:child => "##{child.id}",
:errors => child.errors.full_messages.join(", "))
)
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
end end
end end
@ -1645,13 +1699,22 @@ class Issue < ActiveRecord::Base
def after_create_from_copy def after_create_from_copy
return unless copy? && !@after_create_from_copy_handled return unless copy? && !@after_create_from_copy_handled
if (@copied_from.project_id == project_id || Setting.cross_project_issue_relations?) && @copy_options[:link] != false if (@copied_from.project_id == project_id ||
Setting.cross_project_issue_relations?) &&
@copy_options[:link] != false
if @current_journal if @current_journal
@copied_from.init_journal(@current_journal.user) @copied_from.init_journal(@current_journal.user)
end end
relation = IssueRelation.new(:issue_from => @copied_from, :issue_to => self, :relation_type => IssueRelation::TYPE_COPIED_TO) relation =
IssueRelation.new(:issue_from => @copied_from, :issue_to => self,
:relation_type => IssueRelation::TYPE_COPIED_TO)
unless relation.save unless relation.save
logger.error "Could not create relation while copying ##{@copied_from.id} to ##{id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger if logger
logger.error(
"Could not create relation while copying ##{@copied_from.id} to ##{id} " \
"due to validation errors: #{relation.errors.full_messages.join(', ')}"
)
end
end end
end end
@ -1666,7 +1729,12 @@ class Issue < ActiveRecord::Base
# Do not copy subtasks that are not visible to avoid potential disclosure of private data # Do not copy subtasks that are not visible to avoid potential disclosure of private data
unless child.visible? unless child.visible?
logger.error "Subtask ##{child.id} was not copied during ##{@copied_from.id} copy because it is not visible to the current user" if logger if logger
logger.error(
"Subtask ##{child.id} was not copied during ##{@copied_from.id} copy " \
"because it is not visible to the current user"
)
end
next next
end end
copy = Issue.new.copy_from(child, copy_options) copy = Issue.new.copy_from(child, copy_options)
@ -1676,10 +1744,21 @@ class Issue < ActiveRecord::Base
copy.author = author copy.author = author
copy.project = project copy.project = project
copy.parent_issue_id = copied_issue_ids[child.parent_id] copy.parent_issue_id = copied_issue_ids[child.parent_id]
copy.fixed_version_id = nil unless child.fixed_version.present? && child.fixed_version.status == 'open' unless child.fixed_version.present? && child.fixed_version.status == 'open'
copy.assigned_to = nil unless child.assigned_to_id.present? && child.assigned_to.status == User::STATUS_ACTIVE copy.fixed_version_id = nil
end
unless child.assigned_to_id.present? &&
child.assigned_to.status == User::STATUS_ACTIVE
copy.assigned_to = nil
end
unless copy.save unless copy.save
logger.error "Could not copy subtask ##{child.id} while copying ##{@copied_from.id} to ##{id} due to validation errors: #{copy.errors.full_messages.join(', ')}" if logger if logger
logger.error(
"Could not copy subtask ##{child.id} " \
"while copying ##{@copied_from.id} to ##{id} due to validation errors: " \
"#{copy.errors.full_messages.join(', ')}"
)
end
next next
end end
copied_issue_ids[child.id] = copy.id copied_issue_ids[child.id] = copy.id
@ -1720,7 +1799,8 @@ class Issue < ActiveRecord::Base
if p.priority_derived? if p.priority_derived?
# priority = highest priority of open children # priority = highest priority of open children
# priority is left unchanged if all children are closed and there's no default priority defined # priority is left unchanged if all children are closed and there's no default priority defined
if priority_position = p.children.open.joins(:priority).maximum("#{IssuePriority.table_name}.position") if priority_position =
p.children.open.joins(:priority).maximum("#{IssuePriority.table_name}.position")
p.priority = IssuePriority.find_by_position(priority_position) p.priority = IssuePriority.find_by_position(priority_position)
elsif default_priority = IssuePriority.default elsif default_priority = IssuePriority.default
p.priority = default_priority p.priority = default_priority
@ -1743,7 +1823,9 @@ class Issue < ActiveRecord::Base
if children.any? if children.any?
child_with_total_estimated_hours = children.select {|c| c.total_estimated_hours.to_f > 0.0} child_with_total_estimated_hours = children.select {|c| c.total_estimated_hours.to_f > 0.0}
if child_with_total_estimated_hours.any? if child_with_total_estimated_hours.any?
average = child_with_total_estimated_hours.sum(&:total_estimated_hours).to_d / child_with_total_estimated_hours.count average =
child_with_total_estimated_hours.sum(&:total_estimated_hours).to_d /
child_with_total_estimated_hours.count
else else
average = 1.0.to_d average = 1.0.to_d
end end