Skip to content

Commit

Permalink
Add edit, update and destroy actions to rooms controller
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlON committed Jan 29, 2017
1 parent 933f803 commit 88e0a37
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 12 deletions.
23 changes: 20 additions & 3 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class RoomsController < ApplicationController
before_action :authenticate_user!
before_action :set_room, only: [:edit, :update, :destroy]

def index
@rooms = Room.includes(:users).all
Expand All @@ -13,20 +14,36 @@ def create
@room = current_user.build_room(room_params)
if @room.save
@room.users << current_user
redirect_to rooms_path, notice: "You successfully created new room \"#{@room.name}\""
redirect_to rooms_path, notice: "You have successfully created new room \"#{@room.name}\"."
else
render :new
end
end

def edit; end

def update; end
def update
if @room.update(room_params)
redirect_to rooms_path, notice: 'You have successfully updated your room.'
else
render :edit
end
end

def destroy; end
def destroy
@room.users.each do |user|
user.update(room_id: nil)
end
@room.destroy
redirect_to root_path
end

private

def set_room
@room = current_user.room
end

def room_params
params.require(:room).permit(:name)
end
Expand Down
2 changes: 2 additions & 0 deletions app/views/layouts/_header.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ header
li = link_to 'Sign up', new_user_registration_path
li = link_to 'Sign in', new_user_session_path
- else
- if current_user.room?
li = link_to 'Edit your room', edit_room_path
li = link_to 'Sign out', destroy_user_session_path, method: :delete
4 changes: 2 additions & 2 deletions app/views/rooms/_form.html.slim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for(room) do |f|
= form_for(room, url: rooms_path) do |f|
.field
= f.label :name
br
= f.text_field :name
.actions
= f.submit 'Create room'
= f.submit submit_button_text
7 changes: 5 additions & 2 deletions app/views/rooms/edit.html.slim
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
h1 Rooms#edit
p Find me in app/views/rooms/edit.html.slim
h1 Edit your room

= render partial: 'form', locals: { room: @room, submit_button_text: 'Update room' }

= link_to 'Destroy your room', rooms_path, method: :delete
3 changes: 1 addition & 2 deletions app/views/rooms/new.html.slim
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
h3 New room

= render partial: 'form', locals: { room: @room }
= render partial: 'form', locals: { room: @room, submit_button_text: 'Create room' }
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Rails.application.routes.draw do
root 'pages#index'
resources :rooms
resources :rooms, only: [:index, :new, :create]
get '/rooms/edit' => 'rooms#edit', as: 'edit_room'
put '/rooms' => 'rooms#update'
delete '/rooms' => 'rooms#destroy'
devise_for :users
end
25 changes: 24 additions & 1 deletion spec/controllers/rooms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,35 @@
end
end

describe 'Post #create' do
describe 'GET #edit' do
it 'returns http success' do
room = create(:room)
room.users << subject.current_user

get :edit

expect(response).to have_http_status(:success)
expect(response).to render_template(:edit)
expect(assigns(:room)).to eq subject.current_user.room
end
end

describe 'POST #create' do
it 'creates new room and assings it as current_users room' do
post :create, params: { room: { name: 'Room538' } }

expect(subject.current_user.room.name).to eq 'Room538'
end
end

describe 'PUT #update' do
it 'updates current_users room' do
subject.current_user.room = create(:room)

put :update, params: { room: { name: 'Room538' } }

expect(subject.current_user.room.name).to eq 'Room538'
end
end
end
end
2 changes: 1 addition & 1 deletion spec/features/rooms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
fill_in 'Name', with: 'My room'
click_button 'Create room'

expect(page).to have_content 'You successfully created new room "My room"'
expect(page).to have_content 'You have successfully created new room "My room".'
end

scenario 'User looks at all rooms' do
Expand Down

0 comments on commit 88e0a37

Please sign in to comment.