Skip to content

Commit 3f9f642

Browse files
committed
[#238] Add duplicate story button
1 parent 0e7af35 commit 3f9f642

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

app/controllers/adventures_controller.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class AdventuresController < ApplicationController
2-
before_action :set_adventure, only: [:show, :edit, :update, :destroy, :details, :source, :offline, :authenticate]
2+
before_action :set_adventure, only: [:show, :edit, :update, :destroy, :details, :source, :offline, :authenticate, :duplicate]
33
before_action :check_authentication, only: [:show]
44

55
def index
@@ -132,6 +132,19 @@ def csv
132132
end
133133
end
134134

135+
def duplicate
136+
if !@adventure.editable_by?(current_user)
137+
flash[:alert] = "You can't modify that Adventure"
138+
return redirect_to root_url
139+
end
140+
141+
duplicated_adventure = @adventure.dup
142+
duplicated_adventure.title = "Copy of #{@adventure.title}"
143+
duplicated_adventure.save
144+
145+
redirect_to [:edit, duplicated_adventure], notice: "Adventure was successfully copied."
146+
end
147+
135148
private
136149

137150
def set_adventure

app/views/adventures/details.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
<%= render "shared/adventure_form", is_update: true, correct_user: @adventure.user && @adventure.user == current_user %>
55

6+
<%= link_to "Duplicate Story", duplicate_adventure_path(@adventure), method: :post, class: "SlantButton" %>
7+
68
<footer class="AccountFormFooter">
79
<%= link_to 'Back to Edit', [:edit, @adventure] %>
810
<br>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
get "source", on: :member
3838
get "offline", on: :member
3939
post "authenticate", on: :member
40+
post "duplicate", on: :member
4041
get "mine", on: :collection, as: :my
4142
get "archived", on: :collection, as: :my_archived
4243
get "csv", on: :collection

0 commit comments

Comments
 (0)