Skip to content

Commit

Permalink
[#238] Add duplicate story button
Browse files Browse the repository at this point in the history
  • Loading branch information
noahko96 committed Feb 2, 2024
1 parent 0e7af35 commit 3f9f642
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/controllers/adventures_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/views/adventures/details.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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" %>

<footer class="AccountFormFooter">
<%= link_to 'Back to Edit', [:edit, @adventure] %>
<br>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
get "source", on: :member
get "offline", on: :member
post "authenticate", on: :member
post "duplicate", on: :member
get "mine", on: :collection, as: :my
get "archived", on: :collection, as: :my_archived
get "csv", on: :collection
Expand Down

0 comments on commit 3f9f642

Please sign in to comment.