Skip to content

Commit

Permalink
pass CRUD in boat management specs for nested routes
Browse files Browse the repository at this point in the history
  • Loading branch information
christinalcole committed Nov 9, 2017
1 parent e70ef27 commit 9cfc71f
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 12 deletions.
44 changes: 43 additions & 1 deletion app/controllers/users/boats_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
class Users::BoatsController < ApplicationController
# before_action :authenticate_user!
before_action :authenticate_user!

def index
@boats = current_user.boats
end

def new
@boat = Boat.new
end

def create
@boat = current_user.boats.build(boat_params)

if @boat.save
redirect_to user_boats_path
flash[:notice] = "Boat successfully added to the database"
else
render :new
end
end

def show
end

def edit
@boat = Boat.find(params[:id])
end

def update
@boat = Boat.find(params[:id])

if @boat.update(boat_params)
redirect_to user_boats_path
flash[:notice] = "Boat successfully updated"
else
render :edit
end
end

def destroy
Boat.find(params[:id]).destroy
redirect_to user_boats_path(current_user.id)
end

private
def boat_params
params.require(:boat).permit(:name, :make, :length, :rating)
end
end

{"utf8"=>"✓",
"authenticity_token"=>"KZxUKMe2QH1ZFczIWYLaEZ1M2Bv+ZpEFptWevivUDSHnswldF7KBPYYqdRLCYMmBa+/DYCOElKoG4htw/T4OSQ==",
"boat"=>{"name"=>"Pipe", "make"=>"Beneteau", "length"=>"40", "rating"=>""},
"commit"=>"Create Boat",
"user_id"=>"2"}
3 changes: 2 additions & 1 deletion app/views/layouts/_navigation.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<% if user_signed_in? %>
<ul>
<li><%= link_to "Crew Positions", user_positions_path(current_user)%></li>
<li><%= link_to "Crew Positions", user_positions_path(current_user) %></li>
<li><%= link_to "Boat Management", user_boats_path(current_user) %></li>
</ul>
<% end %>
33 changes: 32 additions & 1 deletion app/views/users/boats/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
user::boats#edit
<hr>
user::boats#edit <br></br>

<% if @boat.errors.any? %>
<h3><%= pluralize(@boat.errors.count, "error") %> occurred: </h3>
<% @boat.errors.full_messages.each do |msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>
<% end %>

<h2>Edit boat details:</h2>
<%= form_for [:user, @boat] do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<br/>

<%= f.label :make %>
<%= f.text_field :make %>
<br/>

<%= f.label :length %>
<%= f.number_field :length %>
<br/>

<%= f.label :rating %>
<%= f.number_field :rating%>
<br/>

<%= f.submit %>
<% end %>
13 changes: 13 additions & 0 deletions app/views/users/boats/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<hr>
users::boats#index

<h2><%= current_user.name %>'s boats</h2>
<ul>
<% @boats.each do |boat| %>
<li>
<%= boat.name %> | <%= boat.make %> | <%= boat.length %> | <%= boat.rating %> | <%= link_to "Delete", [current_user, boat], method: :delete, data:{confirm: "Are you sure?"} %>
</li>
<% end %>
</ul>

<%= link_to "Add a new boat", new_user_boat_path(current_user.id) %>
32 changes: 31 additions & 1 deletion app/views/users/boats/new.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
user::boats#new
user::boats#new <br></br>

<% if @boat.errors.any? %>
<h3><%= pluralize(@boat.errors.count, "error") %> occurred: </h3>
<% @boat.errors.full_messages.each do |msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>
<% end %>

<h2>Own a new boat? Add its details here:</h2>
<%= form_for [:user, @boat] do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<br/>

<%= f.label :make %>
<%= f.text_field :make %>
<br/>

<%= f.label :length %>
<%= f.number_field :length %>
<br/>

<%= f.label :rating %>
<%= f.number_field :rating%>
<br/>

<%= f.submit %>
<% end %>
28 changes: 20 additions & 8 deletions spec/features/boats/boat_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,26 @@

expect(page.status_code).to eq(200)
end
scenario 'a page to add a new boat that is not nested under the current user does not exist'
scenario 'a page to add a new boat that is not nested under the current user does not exist'
scenario 'a page to edit a specific boat that is not nested under the current user does not exist'
end

context 'create boats' do
scenario 'a form exists to add a new boat to the db' do
visit new_boat_path
user = create(:user)
signin(user.email, user.password)
visit new_user_boat_path(user.id)

expect(page).to have_css('form#new_boat')
end
scenario 'a new boat is created when the form is submitted' do
visit new_boat_path
user = create(:user)
signin(user.email, user.password)
visit new_user_boat_path(user.id)

fill_in 'boat[name]', with: 'Sea Witch'
fill_in 'boat[make]', with: 'J105'
fill_in 'boat[length]', with: '10'
click_button 'Create Boat'

expect(Boat.last.name).to eq("Sea Witch")
Expand All @@ -64,9 +72,11 @@

context 'editing boats' do
scenario 'an existing boat can be updated' do
boat = FactoryGirl.create(:boat, name: "Feelin Good")
user = create(:user)
boat = FactoryGirl.create(:boat, name: "Feelin Good", owner_id: user.id)
signin(user.email, user.password)
visit edit_user_boat_path(user.id, boat.id)

vist edit_boat_path(boat.id)
expect(page).to have_css("form#edit_boat_#{boat.id}")

fill_in 'boat[name]', with: 'Feline Good'
Expand All @@ -77,13 +87,15 @@
end

scenario 'an existing boat can be removed' do
boat = FactoryGirl.create(:boat)
user = create(:user)
boat = FactoryGirl.create(:boat, owner_id: user.id)
signin(user.email, user.password)

visit boats_path
visit user_boats_path(user.id)
click_link "Delete"

expect(Boat.count).to eq(0)
expect(current_path).to eq(boats_path)
expect(current_path).to eq(user_boats_path(user.id))
end
end

Expand Down

0 comments on commit 9cfc71f

Please sign in to comment.