Skip to content

Commit

Permalink
refactor max severity method and add list of severities
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Nov 16, 2023
1 parent 70f870e commit 24c204d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
17 changes: 8 additions & 9 deletions app/helpers/problems_helper.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
module ProblemsHelper
def problem_severity(problem)
{
missing: "danger",
empty: "info",
nesting: "warning",
inefficient: "info",
duplicate: "warning"
}[problem.category.to_sym] || "info"
missing: :danger,
empty: :info,
nesting: :warning,
inefficient: :info,
duplicate: :warning
}[problem.category.to_sym] || :silent
end

def max_problem_severity
return "danger" if Problem.where(category: :missing).count > 0
return "warning" if Problem.where(category: [:nesting, :duplicate]).count > 0
"info"
severities = Problem.select(:category).distinct.map { |p| problem_severity(p) }
severities.max_by { |p| Problem::SEVERITIES.find_index(p) }
end
end
7 changes: 7 additions & 0 deletions app/models/problem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class Problem < ApplicationRecord
:duplicate
]

SEVERITIES = [
:silent,
:info,
:warning,
:danger
]

def self.create_or_clear(problematic, cat, present, options = {})
if present
problematic.problems.create(options.merge(category: cat))
Expand Down
6 changes: 3 additions & 3 deletions spec/helpers/problems_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
it "converts a problem to a severity level" do
expect(helper.problem_severity(
Problem.new(category: :duplicate, problematic: model)
)).to eq "warning"
)).to eq :warning
end

it "works out the maximum severity from a set of problems (warning)" do
Problem.create(category: :duplicate, problematic: model)
Problem.create(category: :inefficient, problematic: model)
expect(helper.max_problem_severity).to eq "warning"
expect(helper.max_problem_severity).to eq :warning
end

it "works out the maximum severity from a set of problems (danger)" do
Problem.create(category: :missing, problematic: model)
Problem.create(category: :duplicate, problematic: model)
Problem.create(category: :inefficient, problematic: model)
expect(helper.max_problem_severity).to eq "danger"
expect(helper.max_problem_severity).to eq :danger
end
end

0 comments on commit 24c204d

Please sign in to comment.