From 3e9af4a67bfe8b90cc7e408b534dbf756a324af3 Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Fri, 3 Jan 2025 16:34:26 +0900 Subject: [PATCH] Fix NoMethodError in IssuePriority#high and #low when both default and active priorities are absent --- app/models/issue_priority.rb | 6 ++++-- test/unit/issue_priority_test.rb | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/models/issue_priority.rb b/app/models/issue_priority.rb index d60098c06..0c6276b5f 100644 --- a/app/models/issue_priority.rb +++ b/app/models/issue_priority.rb @@ -60,11 +60,13 @@ class IssuePriority < Enumeration end def high? - position > self.class.default_or_middle.position + return false unless (default_or_middle_position = self.class.default_or_middle&.position) + position > default_or_middle_position end def low? - position < self.class.default_or_middle.position + return false unless (default_or_middle_position = self.class.default_or_middle&.position) + position < default_or_middle_position end # Updates position_name for active priorities diff --git a/test/unit/issue_priority_test.rb b/test/unit/issue_priority_test.rb index e076afe67..80dc11e1c 100644 --- a/test/unit/issue_priority_test.rb +++ b/test/unit/issue_priority_test.rb @@ -156,4 +156,20 @@ class IssuePriorityTest < ActiveSupport::TestCase IssuePriority.find_by_position_name('highest').destroy assert_equal %w(lowest default high2 highest), IssuePriority.active.to_a.sort.map(&:position_name) end + + def test_high_should_return_false_when_no_default_priority_and_no_active_priorities + IssuePriority.update_all(active: false, is_default: false) + priority = IssuePriority.order(:position).last # Highest priority + assert_nothing_raised do + assert_equal false, priority.high? + end + end + + def test_low_should_return_false_when_no_default_priority_and_no_active_priorities + IssuePriority.update_all(active: false, is_default: false) + priority = IssuePriority.order(:position).first # Lowest priority + assert_nothing_raised do + assert_equal false, priority.low? + end + end end -- 2.47.1