Skip to content

Commit

Permalink
Merge c50facb into 345ff2f
Browse files Browse the repository at this point in the history
  • Loading branch information
npauzenga committed Jan 16, 2016
2 parents 345ff2f + c50facb commit 2ae93ff
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 2 deletions.
48 changes: 48 additions & 0 deletions app/controllers/game_nights_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class GameNightsController < AuthenticationController
def create
result = CreateGameNight.call(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(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])

unless result.success?
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
2 changes: 2 additions & 0 deletions app/interactors/create_game_night.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class CreateGameNight < StandardInteraction
end
2 changes: 2 additions & 0 deletions app/interactors/delete_game_night.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class DeleteGameNight < StandardInteraction
end
2 changes: 2 additions & 0 deletions app/interactors/show_game_night.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ShowGameNight < StandardInteraction
end
2 changes: 2 additions & 0 deletions app/interactors/update_game_night.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class UpdateGameNight < StandardInteraction
end
2 changes: 2 additions & 0 deletions app/serializers/game_night_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class GameNightSerializer < ActiveModel::Serializer
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
mount Knock::Engine => "/signin"

resources :users, except: [:new, :edit]
resources :game_nights, except: [:new, :edit]
resources :password_resets, only: [:create, :update]
end
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
223 changes: 223 additions & 0 deletions spec/controllers/game_nights_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
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) { { game_night: create_game_night_input } }

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

let(:update_game_night_input) do
{
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 200" do
delete :destroy, params
expect(response).to have_http_status(200)
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
3 changes: 2 additions & 1 deletion spec/factories/game_night.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FactoryGirl.define do
factory :game_night do
time Time.zone.now
name Faker::Hipster.words(2)
time { rand(7).days.from_now.to_date }
location_name Faker::Hipster.words(2)
location_address Faker::Address.street_name
end
Expand Down

0 comments on commit 2ae93ff

Please sign in to comment.