Skip to content

Commit f540e13

Browse files
authored
Merge pull request #27 from ombulabs/refactor
Refactor
2 parents 3a9ccbe + 8cf08b9 commit f540e13

File tree

6 files changed

+92
-23
lines changed

6 files changed

+92
-23
lines changed

app/assets/stylesheets/application.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,30 @@ th, td {
2121
th {
2222
background-color: #f2f2f2;
2323
}
24+
.inline-form {
25+
display: inline;
26+
}
27+
28+
/* Reusable button base style */
29+
.btn {
30+
color: white;
31+
border: none;
32+
padding: 6px 12px;
33+
margin-left: 4px;
34+
border-radius: 4px;
35+
cursor: pointer;
36+
}
37+
38+
/* Specific button variants */
39+
.approve-btn {
40+
background-color: #4CAF50;
41+
}
42+
43+
.reject-btn {
44+
background-color: #f44336;
45+
}
46+
47+
.pending-btn {
48+
background-color: yellow;
49+
color: black;
50+
}
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
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class PuzzlesController < ApplicationController
22
def index
3-
@puzzles = Puzzle.all
3+
@pending_puzzles = Puzzle.pending
4+
@approved_puzzles = Puzzle.approved
5+
@rejected_puzzles = Puzzle.rejected
46
end
57
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>Question</th>
5+
<th>Answer</th>
6+
<th>Explanation</th>
7+
<th>Link</th>
8+
<th>Actions</th>
9+
</tr>
10+
</thead>
11+
<tbody>
12+
<% puzzles.each do |puzzle| %>
13+
<tr>
14+
<td><%= puzzle.question %></td>
15+
<td><%= puzzle.answer %></td>
16+
<td><%= puzzle.explanation %></td>
17+
<td>
18+
<% if puzzle.link.present? %>
19+
<%= link_to 'View', puzzle.link, target: '_blank' %>
20+
&nbsp;
21+
<% end %>
22+
</td>
23+
<td>
24+
<% if actions == :pending %>
25+
<%= button_to 'Approve', puzzle_state_path(puzzle, state: :approved), method: :patch, form_class: 'inline-form', class: 'btn approve-btn' %>
26+
<%= button_to 'Reject', puzzle_state_path(puzzle, state: :rejected), method: :patch, form_class: 'inline-form', class: 'btn reject-btn' %>
27+
<% elsif actions == :approved %>
28+
<%= button_to 'Reject', puzzle_state_path(puzzle, state: :rejected), method: :patch, form_class: 'inline-form', class: 'btn reject-btn' %>
29+
<%= button_to 'Pending', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
30+
<% elsif actions == :rejected %>
31+
<%= button_to 'Approve', puzzle_state_path(puzzle, state: :approved), method: :patch, form_class: 'inline-form', class: 'btn approve-btn' %>
32+
<%= button_to 'Pending', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
33+
<% end %>
34+
</td>
35+
</tr>
36+
<% end %>
37+
</tbody>
38+
</table>

app/views/puzzles/index.html.erb

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
<% if session[:user_token] %>
22
<%= link_to 'Logout', session_path(1), data: { turbo_method: :delete } %>
3-
<h1>Puzzles</h1>
4-
<table>
5-
<thead>
6-
<tr>
7-
<th>Question</th>
8-
<th>Answer</th>
9-
<th>Explanation</th>
10-
<th>Link</th>
11-
</tr>
12-
</thead>
13-
<tbody>
14-
<% @puzzles.each do |puzzle| %>
15-
<tr>
16-
<td><%= puzzle.question %></td>
17-
<td><%= puzzle.answer %></td>
18-
<td><%= puzzle.explanation %></td>
19-
<td><%= link_to 'View', puzzle.link, target: '_blank' if puzzle.link.present? %></td>
20-
</tr>
21-
<% end %>
22-
</tbody>
23-
</table>
3+
4+
<h1>Pending Puzzles</h1>
5+
<%= render partial: 'puzzles_table', locals: { puzzles: @pending_puzzles, actions: :pending } %>
6+
7+
<h1>Approved Puzzles</h1>
8+
<%= render partial: 'puzzles_table', locals: { puzzles: @approved_puzzles, actions: :approved } %>
9+
10+
<h1>Rejected Puzzles</h1>
11+
<%= render partial: 'puzzles_table', locals: { puzzles: @rejected_puzzles, actions: :rejected } %>
2412
<% else %>
2513
<%= link_to 'Login with Google', '/auth/google_oauth2' %>
2614
<% end %>

config/routes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Rails.application.routes.draw do
2-
resources :puzzles, only: [:index]
2+
resources :puzzles, only: [:index] do
3+
resource :state, only: [:update], module: :puzzles
4+
end
35
resources :sessions, only: [:create, :destroy]
46

57
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

0 commit comments

Comments
 (0)