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
8 changes: 8 additions & 0 deletions app/models/puzzle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Puzzle < ApplicationRecord

scope :archived, -> { where(state: :archived).order(sent_at: :desc) }

validate :cannot_be_unarchived

def correct_answer_percentage
total = answers.size
return 0 if total.zero?
Expand All @@ -32,4 +34,10 @@ def self.without_cloned
cloned_puzzles_ids = unscoped.where.not(original_puzzle_id: nil).pluck(:original_puzzle_id)
where.not(id: cloned_puzzles_ids)
end

private

def cannot_be_unarchived
errors.add(:state, "cannot be unarchived") if state != state_was && state_was == "archived"
end
end
1 change: 0 additions & 1 deletion app/views/puzzles/_puzzles_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
<%= button_to 'Approve', puzzle_state_path(puzzle, state: :approved), method: :patch, form_class: 'inline-form', class: 'btn approve-btn' %>
<%= button_to 'Pending', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
<% elsif actions == :archived %>
<%= button_to 'Un-Archive', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
<%= button_to 'Clone', puzzle_clone_path(puzzle, state: :pending), method: :post, form_class: 'inline-form', class: 'btn pending-btn' %>
<% end %>
</td>
Expand Down
13 changes: 13 additions & 0 deletions test/models/puzzle_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ class PuzzleTest < ActiveSupport::TestCase
assert_not_includes puzzles, puzzle3
assert_includes puzzles, puzzle4
end

test "once archived, it cannot be unarchived" do
puzzle = puzzles(:archived_low_rate)
assert puzzle.valid?

refute puzzle.update(state: :pending)
refute puzzle.update(state: :approved)
refute puzzle.update(state: :rejected)

assert_raises(ActiveRecord::RecordInvalid, match: /State cannot be unarchived/) { puzzle.pending! }
assert_raises(ActiveRecord::RecordInvalid, match: /State cannot be unarchived/) { puzzle.approved! }
assert_raises(ActiveRecord::RecordInvalid, match: /State cannot be unarchived/) { puzzle.rejected! }
end
end