Skip to content

Commit a8c7e9a

Browse files
authored
Remove "Un-Archive" action, prevent un-archiving (#97)
1 parent 2f06c81 commit a8c7e9a

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

app/models/puzzle.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Puzzle < ApplicationRecord
99

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

12+
validate :cannot_be_unarchived
13+
1214
def correct_answer_percentage
1315
total = answers.size
1416
return 0 if total.zero?
@@ -32,4 +34,10 @@ def self.without_cloned
3234
cloned_puzzles_ids = unscoped.where.not(original_puzzle_id: nil).pluck(:original_puzzle_id)
3335
where.not(id: cloned_puzzles_ids)
3436
end
37+
38+
private
39+
40+
def cannot_be_unarchived
41+
errors.add(:state, "cannot be unarchived") if state != state_was && state_was == "archived"
42+
end
3543
end

app/views/puzzles/_puzzles_table.html.erb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
<%= button_to 'Approve', puzzle_state_path(puzzle, state: :approved), method: :patch, form_class: 'inline-form', class: 'btn approve-btn' %>
4444
<%= button_to 'Pending', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
4545
<% elsif actions == :archived %>
46-
<%= button_to 'Un-Archive', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
4746
<%= button_to 'Clone', puzzle_clone_path(puzzle, state: :pending), method: :post, form_class: 'inline-form', class: 'btn pending-btn' %>
4847
<% end %>
4948
</td>

test/models/puzzle_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,17 @@ class PuzzleTest < ActiveSupport::TestCase
8181
assert_not_includes puzzles, puzzle3
8282
assert_includes puzzles, puzzle4
8383
end
84+
85+
test "once archived, it cannot be unarchived" do
86+
puzzle = puzzles(:archived_low_rate)
87+
assert puzzle.valid?
88+
89+
refute puzzle.update(state: :pending)
90+
refute puzzle.update(state: :approved)
91+
refute puzzle.update(state: :rejected)
92+
93+
assert_raises(ActiveRecord::RecordInvalid, match: /State cannot be unarchived/) { puzzle.pending! }
94+
assert_raises(ActiveRecord::RecordInvalid, match: /State cannot be unarchived/) { puzzle.approved! }
95+
assert_raises(ActiveRecord::RecordInvalid, match: /State cannot be unarchived/) { puzzle.rejected! }
96+
end
8497
end

0 commit comments

Comments
 (0)