diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 3ae3f82..19c3649 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -239,3 +239,8 @@ input.error, textarea.error { background-color: yellow; color: black; } + +section.filters { + text-align: start; + padding-inline-start: 1rem; +} diff --git a/app/controllers/puzzles_controller.rb b/app/controllers/puzzles_controller.rb index 91b84ba..4ca360b 100644 --- a/app/controllers/puzzles_controller.rb +++ b/app/controllers/puzzles_controller.rb @@ -4,6 +4,7 @@ def index @approved_puzzles = Puzzle.approved @rejected_puzzles = Puzzle.rejected @archived_puzzles = Puzzle.archived + @archived_puzzles = @archived_puzzles.only_low_success_rate if params[:low_success_rate] end def edit diff --git a/app/models/puzzle.rb b/app/models/puzzle.rb index 6815d81..ad2c7d8 100644 --- a/app/models/puzzle.rb +++ b/app/models/puzzle.rb @@ -10,9 +10,15 @@ class Puzzle < ApplicationRecord scope :archived, -> { where(state: :archived).order(sent_at: :desc) } def correct_answer_percentage - total = answers.count + total = answers.size return 0 if total.zero? - (answers.where(is_correct: true).count * 100.0 / total).round(1) + correct_count = answers.loaded ? answers.select(&:is_correct).count : answers.where(is_correct: true).count + + (correct_count * 100.0 / total).round(1) + end + + def self.only_low_success_rate + includes(:answers).to_a.select { |ans| ans.correct_answer_percentage <= 80 } end end diff --git a/app/views/puzzles/index.html.erb b/app/views/puzzles/index.html.erb index ead1a1e..7635f04 100644 --- a/app/views/puzzles/index.html.erb +++ b/app/views/puzzles/index.html.erb @@ -16,4 +16,9 @@

Archive Puzzles - Total: <%= @archived_puzzles.length %>

These puzzles have already been sent to the users.

+
+ +
+ <%= link_to "Show low success rate only", url_for(low_success_rate: true) %> +
<%= render partial: 'puzzles_table', locals: { puzzles: @archived_puzzles, actions: :archived } %> diff --git a/test/fixtures/answers.yml b/test/fixtures/answers.yml new file mode 100644 index 0000000..8022ed2 --- /dev/null +++ b/test/fixtures/answers.yml @@ -0,0 +1,47 @@ +answer1: + puzzle: archived_low_rate + user: user1 + is_correct: true + server_id: <%= ActiveRecord::FixtureSet::identify(:server1) %> + +answer2: + puzzle: archived_low_rate + user: user2 + is_correct: true + server_id: <%= ActiveRecord::FixtureSet::identify(:server1) %> + +answer3: + puzzle: archived_low_rate + user: user3 + is_correct: false + server_id: <%= ActiveRecord::FixtureSet::identify(:server1) %> + +answer4: + puzzle: archived_low_rate + user: user4 + is_correct: false + server_id: <%= ActiveRecord::FixtureSet::identify(:server1) %> + +answer5: + puzzle: archived_high_rate + user: user1 + is_correct: true + server_id: <%= ActiveRecord::FixtureSet::identify(:server1) %> + +answer6: + puzzle: archived_high_rate + user: user2 + is_correct: true + server_id: <%= ActiveRecord::FixtureSet::identify(:server1) %> + +answer7: + puzzle: archived_high_rate + user: user3 + is_correct: true + server_id: <%= ActiveRecord::FixtureSet::identify(:server1) %> + +answer8: + puzzle: archived_high_rate + user: user4 + is_correct: true + server_id: <%= ActiveRecord::FixtureSet::identify(:server1) %> diff --git a/test/fixtures/puzzles.yml b/test/fixtures/puzzles.yml index 2471fec..45588a1 100644 --- a/test/fixtures/puzzles.yml +++ b/test/fixtures/puzzles.yml @@ -4,7 +4,6 @@ one: explanation: "This is a test puzzle" link: "https://example.com" state: "approved" - sent_at: <%= 1.day.ago %> suggested_by: "test_user" two: @@ -13,5 +12,22 @@ two: explanation: "This is a test puzzle 2" link: "" state: "pending" + suggested_by: "test_user" + +archived_low_rate: + question: "Some question with low answer rate" + answer: "ruby" + explanation: "This is a question with low answer rate" + link: "" + state: "archived" + sent_at: <%= 2.days.ago %> + suggested_by: "test_user" + +archived_high_rate: + question: "Some question with high answer rate" + answer: "ruby" + explanation: "This is a question with high answer rate" + link: "" + state: "archived" sent_at: <%= 2.days.ago %> suggested_by: "test_user" diff --git a/test/fixtures/servers.yml b/test/fixtures/servers.yml new file mode 100644 index 0000000..b509ab5 --- /dev/null +++ b/test/fixtures/servers.yml @@ -0,0 +1,3 @@ +server1: + name: Server1 + server_id: id1 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..fcc62cf --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,19 @@ +user1: + role: 1 + user_id: id1 + username: user1 + +user2: + role: 1 + user_id: id2 + username: user2 + +user3: + role: 1 + user_id: id3 + username: user3 + +user4: + role: 1 + user_id: id4 + username: user4 diff --git a/test/models/puzzle_test.rb b/test/models/puzzle_test.rb index 420e0bb..7654daf 100644 --- a/test/models/puzzle_test.rb +++ b/test/models/puzzle_test.rb @@ -49,4 +49,14 @@ class PuzzleTest < ActiveSupport::TestCase test "has many answers" do assert_respond_to Puzzle.new, :answers end + + test "only_low_success_rate includes only puzzles with less than or equal 80%" do + puzzles = Puzzle.archived + assert_includes puzzles, puzzles(:archived_low_rate) + assert_includes puzzles, puzzles(:archived_high_rate) + + puzzles = Puzzle.archived.only_low_success_rate + assert_includes puzzles, puzzles(:archived_low_rate) + assert_not_includes puzzles, puzzles(:archived_high_rate) + end end