Skip to content

Commit 30d27ca

Browse files
authored
Merge pull request #410 from cs169/Duplicate_Event
Duplicate an Event Only
2 parents 04a41c0 + 1b9e7a4 commit 30d27ca

8 files changed

Lines changed: 780 additions & 10 deletions

File tree

app/controllers/admin/events_controller.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,30 @@ def toggle_attendance
182182
end
183183
end
184184

185+
def duplicate
186+
count = params[:count].to_i # Invalid input will be treated as 0, which will be caught by validation below
187+
188+
# Validate count
189+
unless count.between?(1, 100)
190+
flash[:alert] = 'Invalid number of duplicates. Please enter a number between 1 and 100.'
191+
redirect_to admin_conference_program_event_path(@conference.short_title, @event)
192+
return
193+
end
194+
195+
duplicator = EventDuplicator.new(@event, current_user)
196+
duplicated_events = duplicator.duplicate(count)
197+
198+
flash[:notice] = if duplicated_events.length == 1
199+
"Event '#{duplicated_events.first.title}' duplicated successfully."
200+
else
201+
"#{duplicated_events.length} copies of '#{@event.title}' created successfully."
202+
end
203+
redirect_to admin_conference_program_events_path(@conference.short_title)
204+
rescue StandardError => e
205+
flash[:alert] = "Could not duplicate event"
206+
redirect_to admin_conference_program_event_path(@conference.short_title, @event)
207+
end
208+
185209
def destroy
186210
@event = Event.find(params[:id])
187211
if @event.destroy

app/services/event_duplicator.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
class EventDuplicator
4+
def initialize(event, submitter = nil)
5+
@event = event
6+
@submitter = submitter
7+
end
8+
9+
def duplicate(count = 1)
10+
duplicated_events = []
11+
@event.class.transaction do
12+
count.times do
13+
duplicated_events << create_duplicate
14+
end
15+
end
16+
duplicated_events
17+
end
18+
19+
private
20+
21+
def create_duplicate
22+
duplicate_event = Event.create!(
23+
# Content fields
24+
title: @event.title,
25+
subtitle: @event.subtitle,
26+
abstract: @event.abstract,
27+
description: @event.description,
28+
submission_text: @event.submission_text,
29+
committee_review: @event.committee_review,
30+
proposal_additional_speakers: @event.proposal_additional_speakers,
31+
event_type: @event.event_type,
32+
track: @event.track,
33+
difficulty_level: @event.difficulty_level,
34+
language: @event.language,
35+
presentation_mode: @event.presentation_mode,
36+
require_registration: @event.require_registration,
37+
max_attendees: @event.max_attendees,
38+
program: @event.program,
39+
state: 'new',
40+
progress: @event.progress,
41+
public: @event.public,
42+
is_highlight: @event.is_highlight,
43+
superevent: @event.superevent,
44+
45+
# Don't copy: start_time, room_id, parent_id (reset to nil)
46+
start_time: nil,
47+
room_id: nil,
48+
parent_id: nil,
49+
50+
# Submitter
51+
submitter: @submitter
52+
)
53+
54+
# Copy speakers and volunteers
55+
duplicate_event.speakers = @event.speakers
56+
duplicate_event.volunteers = @event.volunteers
57+
duplicate_event.save!
58+
59+
duplicate_event
60+
end
61+
end

app/views/admin/events/_proposal.html.haml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
- if @event.public
1111
= link_to 'Preview', conference_program_proposal_path(@conference.short_title, @event.id), class: 'btn btn-mini btn-primary'
1212
= link_to 'Registrations', registrations_admin_conference_program_event_path(@conference.short_title, @event), class: 'btn btn-success'
13+
%button.btn.btn-mini.btn-info{'data-toggle' => 'modal', 'data-target' => '#duplicateEventModal'}
14+
Duplicate
1315
= link_to 'Edit', edit_admin_conference_program_event_path(@conference.short_title, @event), class: 'btn btn-mini btn-primary'
1416
= link_to 'Delete', admin_conference_program_event_path(@conference.short_title, @event), method: :delete, data: { confirm: 'Are you sure you want to delete this event?' }, class: 'btn btn-mini btn-danger'
1517

app/views/admin/events/show.html.haml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,23 @@
6767

6868
#proposal-commercials.tab-pane
6969
= render partial: 'shared/media_items', locals: { commercials: @event.commercials }
70+
71+
#duplicateEventModal.modal.fade{tabindex: '-1', role: 'dialog', 'aria-labelledby' => 'duplicateEventModalLabel'}
72+
.modal-dialog{role: 'document'}
73+
.modal-content
74+
.modal-header
75+
%button.close{'data-dismiss' => 'modal', 'aria-label' => 'Close'}
76+
%span{'aria-hidden' => 'true'}
77+
&times;
78+
%h4#duplicateEventModalLabel.modal-title
79+
Duplicate Event
80+
= form_with url: duplicate_admin_conference_program_event_path(@conference.short_title, @event), method: :post, local: true do |f|
81+
.modal-body
82+
.form-group
83+
%label{for: 'duplicate_count'} Number of copies to create:
84+
%input#duplicate_count.form-control{type: 'number', name: 'count', value: '1', min: '1', max: '100', required: true}
85+
%small.form-text.text-muted
86+
You can create up to 100 copies at once. The new events will be created with you as the submitter.
87+
.modal-footer
88+
%button.btn.btn-secondary{'data-dismiss' => 'modal'} Cancel
89+
%button.btn.btn-primary{type: 'submit'} Create Copies

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
patch :unconfirm
120120
patch :restart
121121
get :vote
122+
post :duplicate
122123
end
123124
end
124125
resources :reports, only: :index

0 commit comments

Comments
 (0)