Create from template campaigns handles name collisions

This commit is contained in:
Eric Schultz 2018-11-07 14:17:55 -06:00
parent 743fd2a15e
commit 4dddccdf46
3 changed files with 22 additions and 4 deletions

View file

@ -5,6 +5,7 @@ class CampaignsController < ApplicationController
helper_method :current_campaign_editor?
before_filter :authenticate_confirmed_user!, only: [:create, :name_and_id, :duplicate]
before_filter :authenticate_campaign_editor!, only: [:update, :soft_delete]
before_filter :authenticate_nonprofit_user!, only: [:create_via_template]
before_filter :check_nonprofit_status, only: [:index, :show]
def index
@ -65,6 +66,23 @@ class CampaignsController < ApplicationController
end
end
def create_from_template
Qx.transaction do
campaign_template = current_nonprofit.campaign_templates.find(params[:campaign_template_id])
save_params = campaign_template.create_campaign_params
save_params['slug'] = SlugCopyNamingAlgorithm.new(Campaign, current_nonprofit.id).create_copy_name(Format::Url.convert_to_slug(save_params['name']))
save_params['name'] = NameCopyNamingAlgorithm.new(Campaign, current_nonprofit.id).create_copy_name(save_params['name'])
save_params['profile_id'] = params[:profile_id]
campaign = current_nonprofit.campaigns.create save_params
campaign.update_attribute(:main_image, campaign_template.main_image) unless !campaign_template.main_image rescue AWS::S3::Errors::NoSuchKey
json_saved campaign, 'Campaign created! Well done.'
end
end
def update
Time.use_zone(current_nonprofit.timezone || 'UTC') do
params[:campaign][:end_datetime] = Chronic.parse(params[:campaign][:end_datetime]) if params[:campaign][:end_datetime].present?

View file

@ -54,7 +54,7 @@
</p>
<a class="button">Create campaign from template</a>
<!--= on 'click' (create_campaign_from_template '<%= raw(template.create_campaign_params.to_json) %>') -->
<!--= on 'click' (create_campaign_from_template '<%= raw({'campaign_template_id' => template.id}.to_json) %>') -->
<a class="button red">Delete template</a>
<!--= on 'click' (delete_template <%= template.id %>) -->

View file

@ -79,8 +79,8 @@ appl.def('create_campaign_from_template', function(campaign_params) {
var url = '/nonprofits/' + app.nonprofit_id + '/campaigns/create_from_template'
var params = new Object
params.campaign = JSON.parse(campaign_params)
params.campaign.profile_id = app.profile_id
params = JSON.parse(campaign_params)
params.profile_id = app.profile_id
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest()
@ -96,7 +96,7 @@ appl.def('create_campaign_from_template', function(campaign_params) {
appl.def('loading', false)
appl.notify('Redirecting you to your campaign…')
var campaign_id = JSON.parse(req.response).id
appl.redirect(url + '/' + campaign_id)
appl.redirect('/nonprofits/' + app.nonprofit_id + '/campaigns/' + campaign_id)
})
.catch(function(req) {
appl.def('loading', false)