Update Campaign creation to use jbuilder

This commit is contained in:
Eric 2020-05-21 13:52:24 -05:00
parent 6156e67d46
commit 26be1858d3
5 changed files with 45 additions and 19 deletions

View file

@ -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

View file

@ -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

View file

@ -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]

View file

@ -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

View file

@ -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