diff --git a/app/controllers/campaigns_controller.rb b/app/controllers/campaigns_controller.rb index 10f89278..74773232 100644 --- a/app/controllers/campaigns_controller.rb +++ b/app/controllers/campaigns_controller.rb @@ -60,7 +60,13 @@ class CampaignsController < ApplicationController end def create - render json: CreateCampaign.create(params, current_nonprofit) + @campaign = CreateCampaign.create(params, current_nonprofit) + if (@campaign.errors.empty?) + render 'campaigns/create', campaign: @campaign + else + render json: { errors: @campaign.errors.messages }.as_json + end + end def update diff --git a/app/views/campaigns/_create.jbuilder b/app/views/campaigns/_create.jbuilder new file mode 100644 index 00000000..a887badd --- /dev/null +++ b/app/views/campaigns/_create.jbuilder @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +json.extract! campaign, :id, :name, #basics + :nonprofit_id, :profile_id, :parent_campaign_id # references + :reason_for_supporting, :default_reason_for_supporting, + :published, :deleted + + +json.url campaign_url(nonprofit) + +if campaign.main_image.attached? + json.main_image do + json.full url_for(campaign.main_image) + json.normal url_for(campaign.main_image_by_size(:normal)) + json.thumb url_for(campaign.main_image_by_size(:thumb)) + end +end + +if campaign.background_image.attached? + json.background_image do + json.full url_for(campaign.background_image) + json.normal url_for(campaign.background_image_by_size(:normal)) + end +end \ No newline at end of file diff --git a/lib/create/create_campaign.rb b/lib/create/create_campaign.rb index 29c47196..45131ff8 100644 --- a/lib/create/create_campaign.rb +++ b/lib/create/create_campaign.rb @@ -12,11 +12,7 @@ module CreateCampaign if !params[:campaign][:parent_campaign_id] campaign = nonprofit.campaigns.create params[:campaign] - - return { errors: campaign.errors.messages }.as_json unless campaign.errors.empty? - - return campaign.as_json - # json_saved campaign, 'Campaign created! Well done.' + return campaign else profile_id = params[:campaign][:profile_id] Profile.find(profile_id).update params[:profile] diff --git a/lib/create/create_peer_to_peer_campaign.rb b/lib/create/create_peer_to_peer_campaign.rb index f7ac746f..7fe01ee3 100644 --- a/lib/create/create_peer_to_peer_campaign.rb +++ b/lib/create/create_peer_to_peer_campaign.rb @@ -39,8 +39,6 @@ module CreatePeerToPeerCampaign AWS::S3::Errors::NoSuchKey end - return { errors: campaign.errors.messages }.as_json unless campaign.errors.empty? - - campaign.as_json['campaign'] + campaign end end diff --git a/spec/lib/create/create_peer_to_peer_campaign_spec.rb b/spec/lib/create/create_peer_to_peer_campaign_spec.rb index 4ab262a7..a53b5f9f 100644 --- a/spec/lib/create/create_peer_to_peer_campaign_spec.rb +++ b/spec/lib/create/create_peer_to_peer_campaign_spec.rb @@ -9,12 +9,12 @@ describe CreatePeerToPeerCampaign do let!(:parent_campaign) { force_create(:campaign, name: 'Parent campaign', nonprofit: force_create(:nm_justice)) } context 'on success' do - it 'returns a hash' do + it 'returns a campaign' do campaign_params = { name: 'Child campaign', parent_campaign_id: parent_campaign.id, goal_amount_dollars: '1000' } Timecop.freeze(2020, 4, 5) do result = CreatePeerToPeerCampaign.create(campaign_params, profile.id) - expect(result).to be_kind_of Hash + expect(result).to be_kind_of Campaign end end @@ -23,9 +23,10 @@ describe CreatePeerToPeerCampaign do Timecop.freeze(2020, 4, 5) do result = CreatePeerToPeerCampaign.create(campaign_params, profile.id) - expect(result).not_to include 'errors' - expect(result['parent_campaign_id']).to eq parent_campaign.id - expect(result['created_at']).to eq 'Sun, 05 Apr 2020 00:00:00 UTC +00:00' + expect(result).to be_kind_of Campaign + expect(result.errors.empty?).to be true + expect(result.parent_campaign_id).to eq parent_campaign.id + expect(result.created_at).to eq 'Sun, 05 Apr 2020 00:00:00 UTC +00:00' end end @@ -34,8 +35,8 @@ describe CreatePeerToPeerCampaign do Timecop.freeze(2020, 4, 5) do result = CreatePeerToPeerCampaign.create(campaign_params, profile.id) - expect(result).not_to include 'errors' - expect(result['slug']).to eq 'child-campaign_000' + expect(result.errors.empty?).to be true + expect(result.slug).to eq 'child-campaign_000' end end @@ -63,9 +64,9 @@ describe CreatePeerToPeerCampaign do Timecop.freeze(2020, 4, 5) do result = CreatePeerToPeerCampaign.create(campaign_params, profile.id) - expect(result).to be_kind_of Hash - expect(result).to include 'errors' - expect(result['errors']['goal_amount']).to match ["can't be blank", 'is not a number'] + expect(result).to be_kind_of Campaign + expect(result.errors.empty?).to be false + expect(result.errors['goal_amount']).to match ["can't be blank", 'is not a number'] end end