Skip to content

Commit

Permalink
Add index action to rooms controller
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlON committed Jan 29, 2017
1 parent c22eb85 commit ac8380b
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ gem 'uglifier', '>= 1.3.0'
group :development, :test do
gem 'byebug', platform: :mri
gem 'factory_girl_rails'
gem 'rails-controller-testing'
gem 'rspec-rails'
end

Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ GEM
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.1)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.1)
actionpack (~> 5.x)
actionview (~> 5.x)
activesupport (~> 5.x)
rails-dom-testing (2.0.2)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6)
Expand Down Expand Up @@ -311,6 +315,7 @@ DEPENDENCIES
pg (~> 0.18)
puma (~> 3.0)
rails (~> 5.0.1)
rails-controller-testing
rainbow (>= 2.1.0, < 2.2.0)
reek
rspec-rails
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class RoomsController < ApplicationController
before_action :authenticate_user!

def index; end
def index
@rooms = Room.includes(:users).all
end

def new
@room = Room.new
Expand Down
6 changes: 4 additions & 2 deletions app/views/pages/user_has_no_room.html.slim
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
h1 Welcome to room538

= link_to 'Create new room', new_room_path
p
= link_to 'Create new room', new_room_path
p
= link_to 'All rooms', rooms_path
4 changes: 4 additions & 0 deletions app/views/rooms/_room.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.room
h3 = room.name
- room.users.each do |user|
p = user.name
4 changes: 2 additions & 2 deletions app/views/rooms/index.html.slim
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
h1 Rooms#index
p Find me in app/views/rooms/index.html.slim
h1 All rooms
= render @rooms
22 changes: 22 additions & 0 deletions spec/controllers/rooms_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
RSpec.describe RoomsController, type: :controller do
include Devise::Test::ControllerHelpers

context 'when user signed in' do
let(:user) { create(:user) }
before(:each) do
sign_in user
end

describe 'GET #index' do
it 'returns http success' do
create_list(:room, 5, users: create_list(:user, 4))

get :index

expect(response).to have_http_status(:success)
expect(response).to render_template(:index)
expect(assigns(:rooms).length).to eq 5
end
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/rooms.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryGirl.define do
factory :room do
name { Faker::Hipster.sentence(2) }
name { Faker::Hipster.word }
end
end
18 changes: 17 additions & 1 deletion spec/features/rooms_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
RSpec.feature 'Rooms' do
let(:user) { create(:user) }

scenario 'User creates new room' do
background do
sign_in user
visit root_path
end

scenario 'User creates new room' do
click_link 'Create new room'

fill_in 'Name', with: 'My room'
click_on 'Create room'

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

scenario 'User looks at all rooms' do
rooms = create_list(:room, 3, users: create_list(:user, 3))

click_link 'All rooms'

rooms.each do |room|
expect(page).to have_content room.name
room.users.each do |user|
expect(page).to have_content user.name
end
end
end
end

0 comments on commit ac8380b

Please sign in to comment.