Permalink
Browse files

Adds a textual css class for issue priorities (#12216).

git-svn-id: svn://rubyforge.org/var/svn/redmine/trunk@10720 e93f8b46-1217-0410-a6f0-8f06a7374b81
  • Loading branch information...
1 parent 43daaec commit 5c2a0af70898e55bbc047a1e761b79d99e0f1215 jplang committed Oct 26, 2012
View
@@ -906,7 +906,7 @@ def to_s
# Returns a string of css classes that apply to the issue
def css_classes
- s = "issue status-#{status_id} priority-#{priority_id}"
+ s = "issue status-#{status_id} #{priority.try(:css_classes)}"
s << ' closed' if closed?
s << ' overdue' if overdue?
s << ' child' if child?
@@ -18,6 +18,9 @@
class IssuePriority < Enumeration
has_many :issues, :foreign_key => 'priority_id'
+ after_destroy {|priority| priority.class.compute_position_names}
+ after_save {|priority| priority.class.compute_position_names if priority.position_changed? && priority.position}
+
OptionName = :enumeration_issue_priorities
def option_name
@@ -31,4 +34,35 @@ def objects_count
def transfer_relations(to)
issues.update_all("priority_id = #{to.id}")
end
+
+ def css_classes
+ "priority-#{id} priority-#{position_name}"
+ end
+
+ # Clears position_name for all priorities
+ # Called from migration 20121026003537_populate_enumerations_position_name
+ def self.clear_position_names
+ update_all :position_name => nil
+ end
+
+ # Updates position_name for active priorities
+ # Called from migration 20121026003537_populate_enumerations_position_name
+ def self.compute_position_names
+ priorities = where(:active => true).all.sort_by(&:position)
+ if priorities.any?
+ default = priorities.detect(&:is_default?) || priorities[(priorities.size - 1) / 2]
+ priorities.each_with_index do |priority, index|
+ name = case
+ when priority.position == default.position
+ "default"
+ when priority.position < default.position
+ index == 0 ? "lowest" : "low#{index+1}"
+ else
+ index == (priorities.size - 1) ? "highest" : "high#{priorities.size - index}"
+ end
+
+ update_all({:position_name => name}, :id => priority.id)
+ end
+ end
+ end
end
@@ -0,0 +1,9 @@
+class AddEnumerationsPositionName < ActiveRecord::Migration
+ def up
+ add_column :enumerations, :position_name, :string, :limit => 30
+ end
+
+ def down
+ remove_column :enumerations, :position_name
+ end
+end
@@ -0,0 +1,9 @@
+class PopulateEnumerationsPositionName < ActiveRecord::Migration
+ def up
+ IssuePriority.compute_position_names
+ end
+
+ def down
+ IssuePriority.clear_position_names
+ end
+end
@@ -23,31 +23,36 @@ enumerations_004:
type: IssuePriority
active: true
position: 1
+ position_name: lowest
enumerations_005:
name: Normal
id: 5
type: IssuePriority
is_default: true
active: true
position: 2
+ position_name: default
enumerations_006:
name: High
id: 6
type: IssuePriority
active: true
position: 3
+ position_name: high3
enumerations_007:
name: Urgent
id: 7
type: IssuePriority
active: true
position: 4
+ position_name: high2
enumerations_008:
name: Immediate
id: 8
type: IssuePriority
active: true
position: 5
+ position_name: highest
enumerations_009:
name: Design
id: 9
@@ -73,4 +73,34 @@ def test_moving_in_list_should_reset_positions
priority.expects(:reset_positions_in_list).once
priority.move_to = 'higher'
end
+
+ def test_clear_position_names_should_set_position_names_to_nil
+ IssuePriority.clear_position_names
+ assert IssuePriority.all.all? {|priority| priority.position_name.nil?}
+ end
+
+ def test_compute_position_names_with_default_priority
+ IssuePriority.clear_position_names
+
+ IssuePriority.compute_position_names
+ assert_equal %w(lowest default high3 high2 highest), IssuePriority.active.all.sort.map(&:position_name)
+ end
+
+ def test_compute_position_names_without_default_priority_should_split_priorities
+ IssuePriority.clear_position_names
+ IssuePriority.update_all :is_default => false
+
+ IssuePriority.compute_position_names
+ assert_equal %w(lowest low2 default high2 highest), IssuePriority.active.all.sort.map(&:position_name)
+ end
+
+ def test_adding_a_priority_should_update_position_names
+ priority = IssuePriority.create!(:name => 'New')
+ assert_equal %w(lowest default high4 high3 high2 highest), IssuePriority.active.all.sort.map(&:position_name)
+ end
+
+ def test_destroying_a_priority_should_update_position_names
+ IssuePriority.find_by_position_name('highest').destroy
+ assert_equal %w(lowest default high2 highest), IssuePriority.active.all.sort.map(&:position_name)
+ end
end
@@ -1821,4 +1821,11 @@ def test_journals_after_should_return_journals_with_greater_id
def test_journals_after_with_blank_arg_should_return_all_journals
assert_equal [Journal.find(1), Journal.find(2)], Issue.find(1).journals_after('')
end
+
+ def test_css_classes_should_include_priority
+ issue = Issue.new(:priority => IssuePriority.find(8))
+ classes = issue.css_classes.split(' ')
+ assert_include 'priority-8', classes
+ assert_include 'priority-highest', classes
+ end
end

0 comments on commit 5c2a0af

Please sign in to comment.