From 3f9f642692b78dfb365fb379750b873bfe4304fb Mon Sep 17 00:00:00 2001 From: Noah Over Date: Fri, 2 Feb 2024 10:49:38 -0500 Subject: [PATCH 1/2] [#238] Add duplicate story button --- app/controllers/adventures_controller.rb | 15 ++++++++++++++- app/views/adventures/details.html.erb | 2 ++ config/routes.rb | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/controllers/adventures_controller.rb b/app/controllers/adventures_controller.rb index f3de55d..3a7623c 100644 --- a/app/controllers/adventures_controller.rb +++ b/app/controllers/adventures_controller.rb @@ -1,5 +1,5 @@ class AdventuresController < ApplicationController - before_action :set_adventure, only: [:show, :edit, :update, :destroy, :details, :source, :offline, :authenticate] + before_action :set_adventure, only: [:show, :edit, :update, :destroy, :details, :source, :offline, :authenticate, :duplicate] before_action :check_authentication, only: [:show] def index @@ -132,6 +132,19 @@ def csv end end + def duplicate + if !@adventure.editable_by?(current_user) + flash[:alert] = "You can't modify that Adventure" + return redirect_to root_url + end + + duplicated_adventure = @adventure.dup + duplicated_adventure.title = "Copy of #{@adventure.title}" + duplicated_adventure.save + + redirect_to [:edit, duplicated_adventure], notice: "Adventure was successfully copied." + end + private def set_adventure diff --git a/app/views/adventures/details.html.erb b/app/views/adventures/details.html.erb index 7199d3f..2c8bdbd 100644 --- a/app/views/adventures/details.html.erb +++ b/app/views/adventures/details.html.erb @@ -3,6 +3,8 @@ <%= render "shared/adventure_form", is_update: true, correct_user: @adventure.user && @adventure.user == current_user %> + <%= link_to "Duplicate Story", duplicate_adventure_path(@adventure), method: :post, class: "SlantButton" %> +