2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-26 16:10:54 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
|
|
|
module CreatePeerToPeerCampaign
|
|
|
|
def self.create(campaign_params, profile_id)
|
|
|
|
begin
|
2019-07-30 21:29:24 +00:00
|
|
|
parent_campaign = Campaign.find(campaign_params[:parent_campaign_id])
|
2018-06-26 16:10:54 +00:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
return { errors: { parent_campaign_id: 'not found' } }.as_json
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
p2p_params = campaign_params.except(:nonprofit_id, :summary, :goal_amount)
|
2018-06-26 16:10:54 +00:00
|
|
|
p2p_params.merge!(parent_campaign.child_params)
|
|
|
|
|
|
|
|
profile = Profile.find(profile_id)
|
|
|
|
base_slug = Format::Url.convert_to_slug "#{p2p_params[:name]}-#{profile.name}"
|
|
|
|
algo = SlugP2pCampaignNamingAlgorithm.new(p2p_params[:nonprofit_id])
|
|
|
|
p2p_params[:slug] = algo.create_copy_name(base_slug)
|
|
|
|
|
|
|
|
campaign = Campaign.create(p2p_params)
|
|
|
|
|
|
|
|
campaign.published = true
|
|
|
|
campaign.profile = profile
|
|
|
|
campaign.save
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
begin
|
2020-05-18 21:05:25 +00:00
|
|
|
campaign.main_image.attach(parent_campaign.main_image.blob) if parent_campaign.main_image.attached?
|
2019-07-30 21:29:24 +00:00
|
|
|
rescue StandardError
|
|
|
|
AWS::S3::Errors::NoSuchKey
|
|
|
|
end
|
|
|
|
begin
|
2020-05-18 21:05:25 +00:00
|
|
|
campaign.background_image.attach(parent_campaign.background_image.blob) if parent_campaign.background_image.attached?
|
2019-07-30 21:29:24 +00:00
|
|
|
rescue StandardError
|
|
|
|
AWS::S3::Errors::NoSuchKey
|
|
|
|
end
|
|
|
|
begin
|
2020-05-18 21:05:25 +00:00
|
|
|
campaign.banner_image.attach(parent_campaign.banner_image.blob) if parent_campaign.banner_image.attached?
|
2019-07-30 21:29:24 +00:00
|
|
|
rescue StandardError
|
|
|
|
AWS::S3::Errors::NoSuchKey
|
|
|
|
end
|
2018-07-03 20:06:50 +00:00
|
|
|
|
2020-05-21 18:52:24 +00:00
|
|
|
campaign
|
2018-06-26 16:10:54 +00:00
|
|
|
end
|
|
|
|
end
|