This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #597 from SumOfUs/ak-campaigns-sync
Publish event after creating a Campaign
- Loading branch information
Showing
6 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class CampaignCreator | ||
def self.run(params) | ||
new(params).run | ||
end | ||
|
||
def initialize(params) | ||
@params = params | ||
end | ||
|
||
def run | ||
@campaign = Campaign.create(@params) | ||
if @campaign.persisted? | ||
publish_event | ||
end | ||
@campaign | ||
end | ||
|
||
private | ||
|
||
def publish_event | ||
ChampaignQueue.push({ | ||
type: 'create_campaign', | ||
name: @campaign.name, | ||
campaign_id: @campaign.id | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class CampaignUpdater | ||
def self.run(campaign, params) | ||
new(campaign, params).run | ||
end | ||
|
||
def initialize(campaign, params) | ||
@campaign = campaign | ||
@params = params | ||
end | ||
|
||
def run | ||
@campaign.update(@params).tap do |success| | ||
if success | ||
publish_event | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def publish_event | ||
ChampaignQueue.push({ | ||
type: 'update_campaign', | ||
name: @campaign.name, | ||
campaign_id: @campaign.id | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
require 'rails_helper' | ||
|
||
describe "Campaigns", type: :request do | ||
before do | ||
login_as(create(:user), scope: :user) | ||
end | ||
|
||
describe "#create" do | ||
context "given valid params" do | ||
let(:params) do | ||
{ campaign: { name: "Super Campaign" } } | ||
end | ||
|
||
it "creates a new campaign" do | ||
expect { | ||
post "/campaigns", params | ||
}.to change(Campaign, :count).by(1) | ||
end | ||
|
||
it "redirects to /campaigns" do | ||
post "/campaigns", params | ||
expect(response).to redirect_to "/campaigns" | ||
end | ||
|
||
it "publishes the event" do | ||
expect(ChampaignQueue).to receive(:push).with({ | ||
name: 'Super Campaign', | ||
type: 'create_campaign', | ||
campaign_id: be_a(Integer) | ||
}) | ||
post "/campaigns", params | ||
end | ||
end | ||
|
||
context "given invalid params" do | ||
let(:params) do | ||
{ campaign: { name: "" } } | ||
end | ||
|
||
it "returns 422 Unprocessable Entity" do | ||
post '/campaigns', params | ||
expect(response).to have_http_status(:unprocessable_entity) | ||
end | ||
|
||
it "displays an error message" do | ||
post '/campaigns', params | ||
expect(response.body).to include "There was an error" | ||
end | ||
end | ||
end | ||
|
||
describe "#update" do | ||
let!(:campaign) { create(:campaign) } | ||
context "given valid params" do | ||
let(:params) do | ||
{ campaign: { name: "Updated Campaign" } } | ||
end | ||
|
||
it "updates the campaign" do | ||
put "/campaigns/#{campaign.id}", params | ||
expect(campaign.reload.name).to eq "Updated Campaign" | ||
end | ||
|
||
it "redirects to /campaigns" do | ||
put "/campaigns/#{campaign.id}", params | ||
expect(response).to redirect_to "/campaigns" | ||
end | ||
|
||
it "publishes the event" do | ||
expect(ChampaignQueue).to receive(:push).with( | ||
type: 'update_campaign', | ||
name: 'Updated Campaign', | ||
campaign_id: campaign.id | ||
); | ||
put "/campaigns/#{campaign.id}", params | ||
end | ||
end | ||
|
||
context "given invalid params" do | ||
let(:params) do | ||
{ campaign: { name: "" } } | ||
end | ||
|
||
it "returns 422 Unprocessable Entity" do | ||
put "/campaigns/#{campaign.id}", params | ||
expect(response).to have_http_status(:unprocessable_entity) | ||
end | ||
|
||
it "displays an error message" do | ||
put "/campaigns/#{campaign.id}", params | ||
expect(response.body).to include "There was an error" | ||
end | ||
end | ||
|
||
end | ||
end |