Skip to content

Commit

Permalink
refactor CreateGameNight to scope to create join table
Browse files Browse the repository at this point in the history
  • Loading branch information
npauzenga committed Jan 27, 2016
1 parent 408342d commit a431bbd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 22 deletions.
6 changes: 4 additions & 2 deletions app/controllers/game_nights_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class GameNightsController < AuthenticationController
def create
result = CreateGameNight.call(game_night_params: game_night_params)
result = CreateGameNight.call(user: current_user,
game_night_params: game_night_params)

if result.success?
render json: result.game_night, status: :created
Expand All @@ -10,7 +11,8 @@ def create
end

def update
result = UpdateGameNight.call(id: params[:id], game_night_params: game_night_params)
result = UpdateGameNight.call(id: params[:id],
game_night_params: game_night_params)

if result.success?
render json: result.game_night, status: :ok
Expand Down
12 changes: 10 additions & 2 deletions app/interactors/create_game_night.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class CreateGameNight < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless context.game_night_params
context.fail!(errors: "invalid input") unless valid_input?
end

def execute
context.game_night = GameNight.create(context.game_night_params)
context.game_night = create_game_night
end

def validate_output
Expand All @@ -16,4 +16,12 @@ def validate_output
def game_night_saved?
context.game_night.persisted?
end

def valid_input?
context.game_night_params && context.user
end

def create_game_night
context.user.game_nights.create(context.game_night_params)
end
end
8 changes: 7 additions & 1 deletion app/interactors/delete_game_night.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ def execute
end

def validate_output
context.fail!(errors: "game night not deleted") unless context.game_night.destroyed?
context.fail!(errors: "game night not deleted") unless game_night_destroyed?
end

private

def game_night_destroyed?
context.game_night.destroyed?
end
end
24 changes: 14 additions & 10 deletions spec/controllers/game_nights_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
end

let(:create_game_night_input) do
{ game_night_params: {
name: game_night.name,
time: game_night.time,
location_name: game_night.location_name,
location_address: game_night.location_address }
{
user: user,
game_night_params: {
name: game_night.name,
time: game_night.time,
location_name: game_night.location_name,
location_address: game_night.location_address }
}
end

Expand Down Expand Up @@ -73,11 +75,13 @@
end

let(:update_game_night_input) do
{ game_night_params: {
name: game_night.name,
time: game_night.time,
location_name: game_night.location_name,
location_address: game_night.location_address }
{
id: game_night.id.to_s,
game_night_params: {
name: game_night.name,
time: game_night.time,
location_name: game_night.location_name,
location_address: game_night.location_address }
}
end

Expand Down
22 changes: 19 additions & 3 deletions spec/interactors/create_game_night_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
RSpec.describe CreateGameNight do
describe ".call" do
let(:user) { create(:confirmed_user) }

let(:game_night_params) do
{
time: DateTime.new(2016, 1, 22, 16, 02, 00),
time: DateTime.new.utc,
name: "Space Hulk Tournament",
location_name: "The library",
location_address: "123 fake st"
}
end

subject do
described_class.call(game_night_params: game_night_params)
described_class.call(user: user, game_night_params: game_night_params)
end

context "when successful" do
Expand All @@ -37,7 +39,7 @@
end
end

context "when invalid input is provided" do
context "when params not provided" do
subject do
described_class.call(game_night_params: nil)
end
Expand All @@ -50,5 +52,19 @@
expect(subject.errors).to eq("invalid input")
end
end

context "when user not provided" do
subject do
described_class.call(game_night_params: game_night_params)
end

it "fails" do
is_expected.to be_a_failure
end

it "returns an error" do
expect(subject.errors).to eq("invalid input")
end
end
end
end
3 changes: 2 additions & 1 deletion spec/interactors/delete_game_night_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

context "when destroy fails" do
before do
allow_any_instance_of(GameNight).to receive(:destroyed?).and_return(false)
allow_any_instance_of(GameNight).to receive(:destroyed?).
and_return(false)
end

subject do
Expand Down
8 changes: 5 additions & 3 deletions spec/interactors/update_game_night_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

let(:game_night_params) do
{
time: DateTime.new(2016, 1, 22, 16, 02, 00),
time: DateTime.new.utc,
name: "Space Hulk Tournament",
location_name: "The library",
location_address: "123 fake st"
Expand All @@ -13,7 +13,8 @@

context "when successful" do
subject do
described_class.call(id: game_night.id, game_night_params: game_night_params)
described_class.call(id: game_night.id,
game_night_params: game_night_params)
end

it "returns a successful context" do
Expand Down Expand Up @@ -55,7 +56,8 @@
end

subject do
described_class.call(id: game_night.id, game_night_params: game_night_params)
described_class.call(id: game_night.id,
game_night_params: game_night_params)
end

it "fails" do
Expand Down

0 comments on commit a431bbd

Please sign in to comment.