Skip to content

Commit 3e9ad54

Browse files
committed
created a puzzles states controller to handle puzzle states
1 parent c1f4c50 commit 3e9ad54

File tree

4 files changed

+19
-30
lines changed

4 files changed

+19
-30
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Puzzles
2+
class StatesController < ApplicationController
3+
def update
4+
puzzle = Puzzle.find(params[:puzzle_id])
5+
if puzzle.update(state: params[:state])
6+
redirect_to puzzles_path, notice: "Puzzle state updated to #{puzzle.state}."
7+
else
8+
redirect_to puzzles_path, alert: "Failed to update puzzle state."
9+
end
10+
end
11+
end
12+
end

app/controllers/puzzles_controller.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,4 @@ def index
44
@approved_puzzles = Puzzle.approved
55
@rejected_puzzles = Puzzle.rejected
66
end
7-
8-
def approve
9-
puzzle = Puzzle.find(params[:id])
10-
puzzle.update(state: :approved)
11-
redirect_to puzzles_path, notice: "Puzzle approved."
12-
end
13-
14-
def reject
15-
puzzle = Puzzle.find(params[:id])
16-
puzzle.update(state: :rejected)
17-
redirect_to puzzles_path, notice: "Puzzle rejected."
18-
end
19-
20-
def pending
21-
puzzle = Puzzle.find(params[:id])
22-
puzzle.update(state: :pending)
23-
redirect_to puzzles_path, notice: "Puzzle marked as pending."
24-
end
257
end

app/views/puzzles/index.html.erb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
<% end %>
2525
</td>
2626
<td>
27-
<%= button_to 'Approve', approve_puzzle_path(puzzle), method: :patch, form_class: 'inline-form', class: 'approve-btn' %>
28-
<%= button_to 'Reject', reject_puzzle_path(puzzle), method: :patch, form_class: 'inline-form', class: 'reject-btn' %>
27+
<%= button_to 'Approve', puzzle_state_path(puzzle, state: :approved), method: :patch, form_class: 'inline-form', class: 'approve-btn' %>
28+
<%= button_to 'Reject', puzzle_state_path(puzzle, state: :rejected), method: :patch, form_class: 'inline-form', class: 'reject-btn' %>
2929
</td>
3030
</tr>
3131
<% end %>
@@ -56,8 +56,8 @@
5656
<% end %>
5757
</td>
5858
<td>
59-
<%= button_to 'Reject', reject_puzzle_path(puzzle), method: :patch, form_class: 'inline-form', class: 'reject-btn' %>
60-
<%= button_to 'Pending', pending_puzzle_path(puzzle), method: :patch, form_class: 'inline-form', class: 'pending-btn' %>
59+
<%= button_to 'Reject', puzzle_state_path(puzzle, state: :rejected), method: :patch, form_class: 'inline-form', class: 'reject-btn' %>
60+
<%= button_to 'Pending', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'pending-btn' %>
6161
</td>
6262
</tr>
6363
<% end %>
@@ -88,14 +88,13 @@
8888
<% end %>
8989
</td>
9090
<td>
91-
<%= button_to 'Approve', approve_puzzle_path(puzzle), method: :patch, form_class: 'inline-form', class: 'approve-btn' %>
92-
<%= button_to 'Pending', pending_puzzle_path(puzzle), method: :patch, form_class: 'inline-form', class: 'pending-btn' %>
91+
<%= button_to 'Approve', puzzle_state_path(puzzle, state: :approved), method: :patch, form_class: 'inline-form', class: 'approve-btn' %>
92+
<%= button_to 'Pending', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'pending-btn' %>
9393
</td>
9494
</tr>
9595
<% end %>
9696
</tbody>
9797
</table>
98-
9998
<% else %>
10099
<%= link_to 'Login with Google', '/auth/google_oauth2' %>
101100
<% end %>

config/routes.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
Rails.application.routes.draw do
22
resources :puzzles, only: [:index] do
3-
member do
4-
patch :approve
5-
patch :reject
6-
patch :pending
7-
end
3+
resource :state, only: [:update], module: :puzzles
84
end
95
resources :sessions, only: [:create, :destroy]
106

0 commit comments

Comments
 (0)