Skip to content

Commit

Permalink
merge bugs/create-game-night-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
npauzenga committed Jan 30, 2016
2 parents 7db54a6 + a431bbd commit 33f0e74
Show file tree
Hide file tree
Showing 14 changed files with 594 additions and 3 deletions.
52 changes: 52 additions & 0 deletions app/controllers/game_nights_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class GameNightsController < AuthenticationController
def create
result = CreateGameNight.call(user: current_user,
game_night_params: game_night_params)

if result.success?
render json: result.game_night, status: :created
else
render json: result.game_night.errors, status: :unprocessable_entity
end
end

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

if result.success?
render json: result.game_night, status: :ok
else
render json: result.game_night.errors, status: :unprocessable_entity
end
end

def show
result = ShowGameNight.call(id: params[:id])

if result.success?
render json: result.game_night, status: :ok
else
render json: result.errors, status: :not_found
end
end

def destroy
result = DeleteGameNight.call(id: params[:id])

if result.success?
head :no_content
else
render json: result.errors, status: :internal_server_error
end
end

private

def game_night_params
params.require(:game_night).permit(:name,
:time,
:location_name,
:location_address)
end
end
27 changes: 27 additions & 0 deletions app/interactors/create_game_night.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class CreateGameNight < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless valid_input?
end

def execute
context.game_night = create_game_night
end

def validate_output
context.fail!(errors: context.game_night.errors) unless game_night_saved?
end

private

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
19 changes: 19 additions & 0 deletions app/interactors/delete_game_night.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class DeleteGameNight < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless context.id
end

def execute
context.game_night = GameNight.destroy(context.id)
end

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

private

def game_night_destroyed?
context.game_night.destroyed?
end
end
9 changes: 9 additions & 0 deletions app/interactors/show_game_night.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ShowGameNight < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless context.id
end

def execute
context.game_night = GameNight.find(context.id)
end
end
19 changes: 19 additions & 0 deletions app/interactors/update_game_night.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class UpdateGameNight < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless valid_input?
end

def execute
context.fail!(errors: "game_night update failed") unless update_game_night
end

private

def update_game_night
GameNight.update(context.id, context.game_night_params)
end

def valid_input?
context.game_night_params && context.id
end
end
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Rails.application.routes.draw do
mount Knock::Engine => "/signin"

resource :user, except: [:new, :edit]
resource :user, except: [:new, :edit]
resources :game_nights, except: [:new, :edit]
resources :password_resets, only: [:create, :update]
resources :groups, except: [:new, :edit]
resources :profiles, only: [:show, :index]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160110154158_add_name_to_game_nights.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNameToGameNights < ActiveRecord::Migration
def change
add_column :game_nights, :name, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151219215255) do
ActiveRecord::Schema.define(version: 20160110154158) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -24,6 +24,7 @@
t.datetime "updated_at", null: false
t.integer "group_id"
t.integer "organizer_id"
t.string "name"
end

add_index "game_nights", ["group_id"], name: "index_game_nights_on_group_id", using: :btree
Expand Down
230 changes: 230 additions & 0 deletions spec/controllers/game_nights_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
RSpec.describe GameNightsController do
let(:user) { create(:confirmed_user) }
let(:game_night) { create(:game_night) }

before do
authenticate
end

describe "POST #create" do
let(:params) do
{ game_night: create_game_night_input.fetch(:game_night_params) }
end

let(:create_game_night_input) do
{
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

let(:create_game_night_context) do
Interactor::Context.new(errors: :val, game_night: game_night)
end

before do
allow(CreateGameNight).to receive(:call).with(create_game_night_input).
and_return(create_game_night_context)
end

context "when called" do
it "calls the CreateGameNight interactor" do
expect(CreateGameNight).to receive(:call)
post :create, params
end
end

context "when CreateGameNight is a success" do
it "returns HTTP status 201" do
post :create, params
expect(response).to have_http_status(201)
end

it "renders the game_night as JSON" do
post :create, params
expect(serialize(game_night)).to eq(response.body)
end
end

context "when CreateGameNight is a failure" do
let(:create_game_night_context) do
double(:context, success?: false, game_night: game_night)
end

it "returns HTTP status 422" do
post :create, params
expect(response).to have_http_status(422)
end

it "returns an error" do
game_night.errors.add(:name, "error")
post :create, params
expect(json["name"]).to eq(["error"])
end
end
end

describe "PATCH #update" do
let(:params) do
{ id: game_night.id,
game_night: update_game_night_input.fetch(:game_night_params) }
end

let(:update_game_night_input) do
{
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

let(:update_game_night_context) do
Interactor::Context.new(errors: :val, game_night: game_night)
end

before do
allow(UpdateGameNight).to receive(:call).with(update_game_night_input).
and_return(update_game_night_context)
end

context "when called" do
it "calls the UpdateGameNight Interactor" do
expect(UpdateGameNight).to receive(:call)
patch :update, params
end
end

context "when UpdateGameNight is a success" do
it "returns HTTP status 200" do
patch :update, params
expect(response).to have_http_status(200)
end

it "renders the GameNight as JSON" do
patch :update, params
expect(serialize(game_night)).to eq(response.body)
end
end

context "when UpdateGameNight is a failure" do
let(:update_game_night_context) do
double(:context, success?: false, game_night: game_night)
end

it "renders an error" do
game_night.errors.add(:name, "error")
patch :update, params
expect(json["name"]).to eq(["error"])
end

it "returns HTTP status 422" do
patch :update, params
expect(response).to have_http_status(422)
end
end
end

describe "GET #show" do
let(:params) { { id: game_night.id } }
let(:show_game_night_input) { { id: params.fetch(:id).to_s } }

let(:show_game_night_context) do
Interactor::Context.new(errors: :val, game_night: game_night)
end

before do
allow(ShowGameNight).to receive(:call).with(show_game_night_input).
and_return(show_game_night_context)
end

context "when called" do
it "calls the ShowGameNight Interactor" do
expect(ShowGameNight).to receive(:call)
get :show, params
end
end

context "when ShowGameNight is a success" do
it "returns HTTP status 200" do
get :show, params
expect(response).to have_http_status(200)
end

it "renders the GameNight as JSON" do
get :show, params
expect(serialize(game_night)).to eq(response.body)
end
end

context "when ShowGameNight is a failure" do
let(:show_game_night_context) do
double(:context, errors: { name: ["invalid"] },
success?: false,
game_night: game_night)
end

it "returns HTTP status 404" do
get :show, params
expect(response).to have_http_status(404)
end

it "renders an error" do
get :show, params
expect(json["name"]).to eq(["invalid"])
end
end
end

describe "DELETE #destroy" do
let(:params) { { id: game_night.id } }
let(:delete_game_night_input) { { id: params.fetch(:id).to_s } }

let(:delete_game_night_context) do
Interactor::Context.new(errors: :val, game_night: game_night)
end

before do
allow(DeleteGameNight).to receive(:call).with(delete_game_night_input).
and_return(delete_game_night_context)
end

context "when called" do
it "calls the DeleteGameNight Interactor" do
expect(DeleteGameNight).to receive(:call)
delete :destroy, params
end
end

context "when DeleteGameNight is a success" do
it "returns HTTP status 204" do
delete :destroy, params
expect(response).to have_http_status(204)
end
end

context "when DeleteGameNight is a failure" do
let(:delete_game_night_context) do
double(:context, success?: false,
game_night: game_night,
errors: { id: ["invalid"] })
end

it "returns HTTP status 500" do
delete :destroy, params
expect(response).to have_http_status(500)
end

it "renders an error" do
delete :destroy, params
expect(json["id"]).to eq(["invalid"])
end
end
end
end
Loading

0 comments on commit 33f0e74

Please sign in to comment.