Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/cars_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class CarsController < ApplicationController

def index
@cars = Car.all
end

def new
@car = Car.new
@car_models = CarModel.all
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class HomeController < ApplicationController

def index
@cars = Car.all
@available_cars = Car.last_available_cars
@cars_on_maintenance = Car.where(status: :on_maintenance).first(10)
@all_cars = Car.all - @available_cars - @cars_on_maintenance
end
end
4 changes: 4 additions & 0 deletions app/models/car.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def current_maintenance
maintenances.last if on_maintenance?
end

def self.last_available_cars
where(status: :available).order(:updated_at).last(10)
end

def car_km_can_not_be_less_than_actual
if car_km < car_km_was
errors.add(:car_km, 'Quilometragem não pode ser menor que a atual')
Expand Down
20 changes: 7 additions & 13 deletions app/views/cars/_car.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<h1>Detalhes do Veículo</h1>
<dl>
<strong><dt>Modelo do Veículo</dt></strong>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me parece meio brusca essa mudança ^^
Acredito que outros testes vão quebrar nas demais branches, talvez fosse uma boa alinhar com todo time

<dd><%= car.car_model.name %></dd>
<strong><dt>Placa</dt></strong>
<dd><%= car.license_plate %></dd>
<strong><dt>Ano</dt></strong>
<dd><%= car.car_model.year%></dd>
<strong><dt>Cor</dt></strong>
<dd><%= car.color %>
</dd><strong><dt>Informaçōes complementares sobre o veículo</dt></strong>
<dd><%= car.car_model.car_options %></dd>
</dl>
<div class="col-md-2 mb-3 mt-3" >
<div class="card p-3">
<strong><dt>Modelo do Veículo</dt></strong>
<dd><%= link_to car.car_identification, car %></dd>
<%= link_to "Ver detalhes", car %>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/cars/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render @cars %>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poderia ter um titulo nessa tela

12 changes: 7 additions & 5 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<% @cars.each do |car|%>
<%= link_to "#{car.car_model.name} - #{car.license_plate}", car %>
<% end %>

<h2>Últimos carros disponíveis</h2>
<div class="row last-available-cars">
<%= render @available_cars %>
</div>
<%= link_to 'Ver todos disponíveis', cars_path, class: "btn btn-info float-right" %>
<h2>Todos os carros</h2>
<%= render @all_cars %>

<div id="maintenance">
<h1>Carros em manutenção</h1>
Expand All @@ -14,4 +17,3 @@
<% end %>
<%= link_to "Ver todos os carros em manutenção", maintenances_path %>
</div>

2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end
end
resources :subsidiary_car_models, only: %i[show new create]
resources :cars, only: %i[show new create] do
resources :cars, only: %i[index show new create] do
resources :fines, only: %i[show new create]
resources :maintenances, only: %i[show new create edit update]
resources :inspections, only: %i[ new create]
Expand Down
5 changes: 2 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
t.datetime "updated_at", null: false
t.string "license_plate"
t.string "color"
t.integer "subsidiary_id"
t.integer "status", default: 0
t.integer "subsidiary_id"
t.index ["car_model_id"], name: "index_cars_on_car_model_id"
t.index ["subsidiary_id"], name: "index_cars_on_subsidiary_id"
end
Expand Down Expand Up @@ -110,9 +110,8 @@
t.integer "customer_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "start_at"
t.datetime "finish_at"
t.datetime "finished_at"
t.datetime "start_at"
t.index ["car_id"], name: "index_rentals_on_car_id"
t.index ["customer_id"], name: "index_rentals_on_customer_id"
t.index ["user_id"], name: "index_rentals_on_user_id"
Expand Down
4 changes: 2 additions & 2 deletions spec/factories/car_models.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FactoryBot.define do
factory :car_model do
name { Faker::Vehicle.make_and_model }
year { "2008" }
year { Faker::Vehicle.year }
manufacture
car_options { "3 portas" }
car_options { Faker::Color.color_name }
end
end
2 changes: 1 addition & 1 deletion spec/factories/manufactures.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :manufacture do
name { Faker::Vehicle.manufacture }
name { Faker::Company.name }
end
end
43 changes: 43 additions & 0 deletions spec/features/admin/admin_view_cars_in_home_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rails_helper'

feature 'List available cars in home' do

scenario 'successfully' do

#arrange
user = create(:user)
login_as(user)

create_list(:car, 10, status: :available, car_model: create(:car_model, name: 'Palio'))
create_list(:car, 5, status: :available, car_model: create(:car_model, name: 'X1'))

#act
visit root_path

#assert
within("div.last-available-cars") do
expect(page).to have_content('X1', count: 5)
expect(page).to have_content('Palio', count: 5)
expect(page).not_to have_content('Palio', count: 10)
end


end

scenario 'has an option to view all cars' do

user = create(:user)
login_as(user)

create_list(:car, 10, car_model: create(:car_model, name: 'Palio'))
create_list(:car, 5, car_model: create(:car_model, name: 'X1'))

visit root_path
click_on "Ver todos disponíveis"

expect(current_path).to eq cars_path
expect(page).to have_content('X1', count: 5)
expect(page).to have_content('Palio', count: 10)

end
end
16 changes: 16 additions & 0 deletions spec/models/car_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

RSpec.describe Car, type: :model do

describe "#last_available_cars" do

it "should list only available cars" do

create_list(:car, 5, status: :available, car_model: create(:car_model, name: 'Palio'))
create_list(:car, 5, status: :on_maintenance, car_model: create(:car_model, name: 'Palio'))

cars = Car.last_available_cars

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falta nesse teste garantir que os carros devolvidos estao corretos. O teste só garante que temos 5 itens na lista.

expect(cars.select{|car| car[:status] == 'available' }.size).to eq 5
end
end
end