Skip to content

Commit ca099da

Browse files
committed
shorten long line of app/models/issue.rb
git-svn-id: http://svn.redmine.org/redmine/trunk@20663 e93f8b46-1217-0410-a6f0-8f06a7374b81
1 parent c305423 commit ca099da

File tree

1 file changed

+107
-25
lines changed

1 file changed

+107
-25
lines changed

app/models/issue.rb

Lines changed: 107 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ class Issue < ActiveRecord::Base
110110
before_validation :clear_disabled_fields
111111
before_save :close_duplicates, :update_done_ratio_from_issue_status,
112112
:force_updated_on_change, :update_closed_on
113-
after_save {|issue| issue.send :after_project_change if !issue.saved_change_to_id? && issue.saved_change_to_project_id?}
113+
after_save do |issue|
114+
if !issue.saved_change_to_id? && issue.saved_change_to_project_id?
115+
issue.send :after_project_change
116+
end
117+
end
114118
after_save :reschedule_following_issues, :update_nested_set_attributes,
115119
:update_parent_attributes, :delete_selected_attachments, :create_journal
116120
# Should be after_create but would be called before previous after_save callbacks
@@ -128,10 +132,13 @@ def self.visible_condition(user, options={})
128132
'1=1'
129133
when 'default'
130134
user_ids = [user.id] + user.groups.pluck(:id).compact
131-
"(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
135+
"(#{table_name}.is_private = #{connection.quoted_false} " \
136+
"OR #{table_name}.author_id = #{user.id} " \
137+
"OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
132138
when 'own'
133139
user_ids = [user.id] + user.groups.pluck(:id).compact
134-
"(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
140+
"(#{table_name}.author_id = #{user.id} OR " \
141+
"#{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
135142
else
136143
'1=0'
137144
end
@@ -273,8 +280,16 @@ def set_custom_field_default?(custom_value)
273280
# Copies attributes from another issue, arg can be an id or an Issue
274281
def copy_from(arg, options={})
275282
issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
276-
self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on", "status_id", "closed_on")
277-
self.custom_field_values = issue.custom_field_values.inject({}) {|h, v| h[v.custom_field_id] = v.value; h}
283+
self.attributes =
284+
issue.attributes.dup.except(
285+
"id", "root_id", "parent_id", "lft", "rgt",
286+
"created_on", "updated_on", "status_id", "closed_on"
287+
)
288+
self.custom_field_values =
289+
issue.custom_field_values.inject({}) do |h, v|
290+
h[v.custom_field_id] = v.value
291+
h
292+
end
278293
if options[:keep_status]
279294
self.status = issue.status
280295
end
@@ -662,15 +677,21 @@ def workflow_rule_by_attribute(user=nil)
662677
return {} if roles.empty?
663678

664679
result = {}
665-
workflow_permissions = WorkflowPermission.where(:tracker_id => tracker_id, :old_status_id => status_id, :role_id => roles.map(&:id)).to_a
680+
workflow_permissions =
681+
WorkflowPermission.where(
682+
:tracker_id => tracker_id, :old_status_id => status_id,
683+
:role_id => roles.map(&:id)
684+
).to_a
666685
if workflow_permissions.any?
667686
workflow_rules = workflow_permissions.inject({}) do |h, wp|
668687
h[wp.field_name] ||= {}
669688
h[wp.field_name][wp.role_id] = wp.rule
670689
h
671690
end
672691
fields_with_roles = {}
673-
IssueCustomField.where(:visible => false).joins(:roles).pluck(:id, "role_id").each do |field_id, role_id|
692+
IssueCustomField.where(:visible => false).
693+
joins(:roles).pluck(:id, "role_id").
694+
each do |field_id, role_id|
674695
fields_with_roles[field_id] ||= []
675696
fields_with_roles[field_id] << role_id
676697
end
@@ -753,7 +774,12 @@ def validate_issue
753774
errors.add :parent_issue_id, :invalid
754775
elsif (@parent_issue != parent) && (
755776
self.would_reschedule?(@parent_issue) ||
756-
@parent_issue.self_and_ancestors.any? {|a| a.relations_from.any? {|r| r.relation_type == IssueRelation::TYPE_PRECEDES && r.issue_to.would_reschedule?(self)}}
777+
@parent_issue.self_and_ancestors.any? do |a|
778+
a.relations_from.any? do |r|
779+
r.relation_type == IssueRelation::TYPE_PRECEDES &&
780+
r.issue_to.would_reschedule?(self)
781+
end
782+
end
757783
)
758784
errors.add :parent_issue_id, :invalid
759785
elsif !closed? && @parent_issue.closed?
@@ -1056,7 +1082,13 @@ def tracker_was
10561082
# Returns the previous assignee whenever we're before the save
10571083
# or in after_* callbacks
10581084
def previous_assignee
1059-
if previous_assigned_to_id = assigned_to_id_change_to_be_saved.nil? ? assigned_to_id_before_last_save : assigned_to_id_in_database
1085+
previous_assigned_to_id =
1086+
if assigned_to_id_change_to_be_saved.nil?
1087+
assigned_to_id_before_last_save
1088+
else
1089+
assigned_to_id_in_database
1090+
end
1091+
if previous_assigned_to_id
10601092
Principal.find_by_id(previous_assigned_to_id)
10611093
end
10621094
end
@@ -1137,9 +1169,15 @@ def last_notes
11371169
# Preloads relations for a collection of issues
11381170
def self.load_relations(issues)
11391171
if issues.any?
1140-
relations = IssueRelation.where("issue_from_id IN (:ids) OR issue_to_id IN (:ids)", :ids => issues.map(&:id)).all
1172+
relations =
1173+
IssueRelation.where(
1174+
"issue_from_id IN (:ids) OR issue_to_id IN (:ids)", :ids => issues.map(&:id)
1175+
).all
11411176
issues.each do |issue|
1142-
issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id}
1177+
issue.instance_variable_set(
1178+
"@relations",
1179+
relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id}
1180+
)
11431181
end
11441182
end
11451183
end
@@ -1247,7 +1285,10 @@ def blocks?(other)
12471285
all = [self]
12481286
last = [self]
12491287
while last.any?
1250-
current = last.map {|i| i.relations_from.where(:relation_type => IssueRelation::TYPE_BLOCKS).map(&:issue_to)}.flatten.uniq
1288+
current =
1289+
last.map do |i|
1290+
i.relations_from.where(:relation_type => IssueRelation::TYPE_BLOCKS).map(&:issue_to)
1291+
end.flatten.uniq
12511292
current -= last
12521293
current -= all
12531294
return true if current.include?(other)
@@ -1483,8 +1524,13 @@ def self.cross_project_scope(project, scope=nil)
14831524
Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)",
14841525
:lft => project.root.lft, :rgt => project.root.rgt)
14851526
when 'hierarchy'
1486-
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)",
1487-
:lft => project.lft, :rgt => project.rgt)
1527+
Issue.joins(:project).
1528+
where(
1529+
"(#{Project.table_name}.lft >= :lft AND " \
1530+
"#{Project.table_name}.rgt <= :rgt) OR " \
1531+
"(#{Project.table_name}.lft < :lft AND #{Project.table_name}.rgt > :rgt)",
1532+
:lft => project.lft, :rgt => project.rgt
1533+
)
14881534
when 'descendants'
14891535
Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)",
14901536
:lft => project.lft, :rgt => project.rgt)
@@ -1612,7 +1658,10 @@ def user_tracker_permission?(user, permission)
16121658
true
16131659
else
16141660
roles = user.roles_for_project(project).select {|r| r.has_permission?(permission)}
1615-
roles.any? {|r| r.permissions_all_trackers?(permission) || r.permissions_tracker_ids?(permission, tracker_id)}
1661+
roles.any? do |r|
1662+
r.permissions_all_trackers?(permission) ||
1663+
r.permissions_tracker_ids?(permission, tracker_id)
1664+
end
16161665
end
16171666
end
16181667

@@ -1633,7 +1682,12 @@ def after_project_change
16331682
# Change project and keep project
16341683
child.send :project=, project, true
16351684
unless child.save
1636-
errors.add :base, l(:error_move_of_child_not_possible, :child => "##{child.id}", :errors => child.errors.full_messages.join(", "))
1685+
errors.add(
1686+
:base,
1687+
l(:error_move_of_child_not_possible,
1688+
:child => "##{child.id}",
1689+
:errors => child.errors.full_messages.join(", "))
1690+
)
16371691
raise ActiveRecord::Rollback
16381692
end
16391693
end
@@ -1645,13 +1699,22 @@ def after_project_change
16451699
def after_create_from_copy
16461700
return unless copy? && !@after_create_from_copy_handled
16471701

1648-
if (@copied_from.project_id == project_id || Setting.cross_project_issue_relations?) && @copy_options[:link] != false
1702+
if (@copied_from.project_id == project_id ||
1703+
Setting.cross_project_issue_relations?) &&
1704+
@copy_options[:link] != false
16491705
if @current_journal
16501706
@copied_from.init_journal(@current_journal.user)
16511707
end
1652-
relation = IssueRelation.new(:issue_from => @copied_from, :issue_to => self, :relation_type => IssueRelation::TYPE_COPIED_TO)
1708+
relation =
1709+
IssueRelation.new(:issue_from => @copied_from, :issue_to => self,
1710+
:relation_type => IssueRelation::TYPE_COPIED_TO)
16531711
unless relation.save
1654-
logger.error "Could not create relation while copying ##{@copied_from.id} to ##{id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger
1712+
if logger
1713+
logger.error(
1714+
"Could not create relation while copying ##{@copied_from.id} to ##{id} " \
1715+
"due to validation errors: #{relation.errors.full_messages.join(', ')}"
1716+
)
1717+
end
16551718
end
16561719
end
16571720

@@ -1666,7 +1729,12 @@ def after_create_from_copy
16661729

16671730
# Do not copy subtasks that are not visible to avoid potential disclosure of private data
16681731
unless child.visible?
1669-
logger.error "Subtask ##{child.id} was not copied during ##{@copied_from.id} copy because it is not visible to the current user" if logger
1732+
if logger
1733+
logger.error(
1734+
"Subtask ##{child.id} was not copied during ##{@copied_from.id} copy " \
1735+
"because it is not visible to the current user"
1736+
)
1737+
end
16701738
next
16711739
end
16721740
copy = Issue.new.copy_from(child, copy_options)
@@ -1676,10 +1744,21 @@ def after_create_from_copy
16761744
copy.author = author
16771745
copy.project = project
16781746
copy.parent_issue_id = copied_issue_ids[child.parent_id]
1679-
copy.fixed_version_id = nil unless child.fixed_version.present? && child.fixed_version.status == 'open'
1680-
copy.assigned_to = nil unless child.assigned_to_id.present? && child.assigned_to.status == User::STATUS_ACTIVE
1747+
unless child.fixed_version.present? && child.fixed_version.status == 'open'
1748+
copy.fixed_version_id = nil
1749+
end
1750+
unless child.assigned_to_id.present? &&
1751+
child.assigned_to.status == User::STATUS_ACTIVE
1752+
copy.assigned_to = nil
1753+
end
16811754
unless copy.save
1682-
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
1755+
if logger
1756+
logger.error(
1757+
"Could not copy subtask ##{child.id} " \
1758+
"while copying ##{@copied_from.id} to ##{id} due to validation errors: " \
1759+
"#{copy.errors.full_messages.join(', ')}"
1760+
)
1761+
end
16831762
next
16841763
end
16851764
copied_issue_ids[child.id] = copy.id
@@ -1720,7 +1799,8 @@ def recalculate_attributes_for(issue_id)
17201799
if p.priority_derived?
17211800
# priority = highest priority of open children
17221801
# priority is left unchanged if all children are closed and there's no default priority defined
1723-
if priority_position = p.children.open.joins(:priority).maximum("#{IssuePriority.table_name}.position")
1802+
if priority_position =
1803+
p.children.open.joins(:priority).maximum("#{IssuePriority.table_name}.position")
17241804
p.priority = IssuePriority.find_by_position(priority_position)
17251805
elsif default_priority = IssuePriority.default
17261806
p.priority = default_priority
@@ -1743,7 +1823,9 @@ def recalculate_attributes_for(issue_id)
17431823
if children.any?
17441824
child_with_total_estimated_hours = children.select {|c| c.total_estimated_hours.to_f > 0.0}
17451825
if child_with_total_estimated_hours.any?
1746-
average = child_with_total_estimated_hours.sum(&:total_estimated_hours).to_d / child_with_total_estimated_hours.count
1826+
average =
1827+
child_with_total_estimated_hours.sum(&:total_estimated_hours).to_d /
1828+
child_with_total_estimated_hours.count
17471829
else
17481830
average = 1.0.to_d
17491831
end

0 commit comments

Comments
 (0)