Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,8 @@ input.error, textarea.error {
background-color: yellow;
color: black;
}

section.filters {
text-align: start;
padding-inline-start: 1rem;
}
1 change: 1 addition & 0 deletions app/controllers/puzzles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions app/models/puzzle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose changing from ans to p because is a puzzle sending correct_answer_percentage not answer.

Suggested change
includes(:answers).to_a.select { |ans| ans.correct_answer_percentage <= 80 }
includes(:answers).to_a.select { |p| p.correct_answer_percentage <= 80 }

And one question: Is this equivalent to a scope or should we use the scope syntax here?

end
end
5 changes: 5 additions & 0 deletions app/views/puzzles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@

<h1>Archive Puzzles - Total: <%= @archived_puzzles.length %></h1>
<p>These puzzles have already been sent to the users.</p>
<section class="filters">
<label>Filters:</label>
<br>
<%= link_to "Show low success rate only", url_for(low_success_rate: true) %>
</section>
<%= render partial: 'puzzles_table', locals: { puzzles: @archived_puzzles, actions: :archived } %>
47 changes: 47 additions & 0 deletions test/fixtures/answers.yml
Original file line number Diff line number Diff line change
@@ -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) %>
18 changes: 17 additions & 1 deletion test/fixtures/puzzles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ one:
explanation: "This is a test puzzle"
link: "https://example.com"
state: "approved"
sent_at: <%= 1.day.ago %>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved puzzles shouldn't have a sent_at value, they are not sent yet

suggested_by: "test_user"

two:
Expand All @@ -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"
3 changes: 3 additions & 0 deletions test/fixtures/servers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server1:
name: Server1
server_id: id1
19 changes: 19 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions test/models/puzzle_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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