Skip to content

Commit 277248f

Browse files
committed
IIRR-31(puzzle): Add original_puzzle reference for clones
Added original_puzzle association to Puzzle model and migration to support tracking the source of cloned puzzles. Updated clone controller and tests to set and verify original_puzzle_id on clone creation. Will be handy when we want to calculate how many people got it right or wrong, if we ever want to show stats for the puzzles. Ref: https://ombulabs.atlassian.net/browse/IIRR-31
1 parent 38ceffe commit 277248f

5 files changed

Lines changed: 13 additions & 2 deletions

File tree

app/controllers/puzzles/clones_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Puzzles::ClonesController < ApplicationController
22
def create
33
original_puzzle = Puzzle.find(params[:puzzle_id])
44
attributes = original_puzzle.attributes.slice("question", "answer", "explanation", "link", "suggested_by")
5-
cloned_puzzle = Puzzle.new(attributes.merge(state: params.fetch(:state, "pending")))
5+
cloned_puzzle = Puzzle.new(attributes.merge(original_puzzle:, state: params.fetch(:state, "pending")))
66

77
if cloned_puzzle.save
88
redirect_to puzzles_path, notice: "Puzzle cloned. You can now edit the new puzzle."

app/models/puzzle.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ class Puzzle < ApplicationRecord
33
enum :state, { approved: 0, rejected: 1, pending: 2, archived: 3 }
44
has_many :answers
55

6+
belongs_to :original_puzzle, class_name: "Puzzle", optional: true
7+
68
validates :question, presence: true
79

810
scope :archived, -> { where(state: :archived).order(sent_at: :desc) }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddOriginalPuzzleToPuzzlesTable < ActiveRecord::Migration[8.0]
2+
def change
3+
add_reference :puzzles, :original_puzzle, foreign_key: { to_table: :puzzles }, index: true, null: true
4+
end
5+
end

db/schema.rb

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/controllers/puzzles/clones_controller_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Puzzles::ClonesControllerTest < ActionDispatch::IntegrationTest
1212
cloned = Puzzle.order(:id).last
1313
assert_redirected_to puzzles_path
1414

15+
assert_equal original.id, cloned.original_puzzle_id
1516
assert_equal original.question, cloned.question
1617
assert_equal original.answer, cloned.answer
1718
assert_equal original.explanation, cloned.explanation

0 commit comments

Comments
 (0)