Skip to content

Commit 470bfca

Browse files
committed
IIRR-31(puzzles): Add clone action for puzzles
Implement a clone action in Puzzles::ClonesController to duplicate existing puzzles. Add a "Clone" button to the archived puzzles table. Include tests for cloning functionality and authentication checks. Ref: https://ombulabs.atlassian.net/browse/IIRR-31
1 parent fe09489 commit 470bfca

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Puzzles::ClonesController < ApplicationController
2+
def create
3+
original_puzzle = Puzzle.find(params[:puzzle_id])
4+
attributes = original_puzzle.attributes.slice("question", "answer", "explanation", "link", "suggested_by")
5+
cloned_puzzle = Puzzle.new(attributes.merge(state: params.fetch(:state, "pending")))
6+
7+
if cloned_puzzle.save
8+
redirect_to puzzles_path, notice: "Puzzle cloned. You can now edit the new puzzle."
9+
else
10+
redirect_to puzzles_path, alert: "Failed to clone puzzle."
11+
end
12+
end
13+
end

app/views/puzzles/_puzzles_table.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<%= button_to 'Pending', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
4343
<% elsif actions == :archived %>
4444
<%= button_to 'Un-Archive', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
45+
<%= button_to 'Clone', puzzle_clone_path(puzzle, state: :pending), method: :post, form_class: 'inline-form', class: 'btn pending-btn' %>
4546
<% end %>
4647
</td>
4748
</tr>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Rails.application.routes.draw do
22
resources :puzzles, only: [ :index, :edit, :update ] do
33
resource :state, only: [ :update ], module: :puzzles
4+
resource :clone, only: [ :create ], module: :puzzles
45
end
56
resources :sessions, only: [ :create, :destroy ]
67

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require "test_helper"
2+
3+
class Puzzles::ClonesControllerTest < ActionDispatch::IntegrationTest
4+
test "creates a cloned puzzle and redirects to index" do
5+
original = puzzles(:one)
6+
7+
sign_in
8+
assert_difference("Puzzle.count", 1) do
9+
post puzzle_clone_path(original)
10+
end
11+
12+
cloned = Puzzle.order(:id).last
13+
assert_redirected_to puzzles_path
14+
15+
assert_equal original.question, cloned.question
16+
assert_equal original.answer, cloned.answer
17+
assert_equal original.explanation, cloned.explanation
18+
assert_equal original.link, cloned.link
19+
assert_equal original.suggested_by, cloned.suggested_by
20+
assert_nil cloned.sent_at
21+
assert_equal "pending", cloned.state
22+
end
23+
24+
test "does not allow unauthenticated users to create a clone" do
25+
original = puzzles(:one)
26+
27+
assert_no_difference("Puzzle.count") do
28+
post puzzle_clone_path(original)
29+
end
30+
31+
assert_dom "p", "Log in to access the Ruby or Rails admin panel."
32+
end
33+
end

0 commit comments

Comments
 (0)