Added vehicle scaffold
Some checks are pending
CI / scan_ruby (push) Waiting to run
CI / scan_js (push) Waiting to run
CI / lint (push) Waiting to run
CI / test (push) Waiting to run

This commit is contained in:
2024-08-20 19:50:29 +02:00
parent 1bd24e08d0
commit 132ea2c51d
20 changed files with 379 additions and 1 deletions

View File

@@ -0,0 +1,73 @@
class VehiclesController < ApplicationController
before_action :set_vehicle, only: %i[ show edit update destroy ]
skip_before_action :authenticate
#has_many_attached :images
# GET /vehicles or /vehicles.json
def index
@vehicles = Vehicle.all
end
# GET /vehicles/1 or /vehicles/1.json
def show
end
# GET /vehicles/new
def new
@vehicle = Vehicle.new
end
# GET /vehicles/1/edit
def edit
end
# POST /vehicles or /vehicles.json
def create
@vehicle = Vehicle.new(vehicle_params)
respond_to do |format|
if @vehicle.save
format.html { redirect_to vehicle_url(@vehicle), notice: "Vehicle was successfully created." }
format.json { render :show, status: :created, location: @vehicle }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @vehicle.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /vehicles/1 or /vehicles/1.json
def update
respond_to do |format|
if @vehicle.update(vehicle_params)
format.html { redirect_to vehicle_url(@vehicle), notice: "Vehicle was successfully updated." }
format.json { render :show, status: :ok, location: @vehicle }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @vehicle.errors, status: :unprocessable_entity }
end
end
end
# DELETE /vehicles/1 or /vehicles/1.json
def destroy
@vehicle.destroy!
respond_to do |format|
format.html { redirect_to vehicles_url, notice: "Vehicle was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_vehicle
@vehicle = Vehicle.find(params[:id])
end
# Only allow a list of trusted parameters through.
def vehicle_params
params.require(:vehicle).permit(:vehicle_type, :car_brand, :model, :model_year, :fuel_type, :power_ps, :power_kw, :registered, :license_plate, :color, :note)
end
end

View File

@@ -0,0 +1,2 @@
module VehiclesHelper
end

3
app/models/vehicle.rb Normal file
View File

@@ -0,0 +1,3 @@
class Vehicle < ApplicationRecord
enum vehicle_type: { car: 0, truck: 1, motorcycle: 2, van: 3}
end

View File

@@ -0,0 +1,39 @@
<%= form_with(model: vehicle, class: "contents") do |form| %>
<% if vehicle.errors.any? %>
<div id="error_explanation" class="px-3 py-2 mt-3 font-medium text-red-500 rounded-lg bg-red-50">
<h2><%= pluralize(vehicle.errors.count, "error") %> prohibited this vehicle from being saved:</h2>
<ul>
<% vehicle.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :license_plate %>
<%= form.text_field :license_plate, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :vehicle_type %>
<%= form.text_field :vehicle_type, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :model_year %>
<%= form.text_field :model_year, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :car_brand %>
<%= form.text_field :car_brand, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :model %>
<%= form.text_field :model, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :fuel_type %>
<%= form.text_field :fuel_type, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>

View File

@@ -0,0 +1,46 @@
<div id="<%= dom_id vehicle %>">
<p class="my-5">
<strong class="block mb-1 font-medium">Angemeldet:</strong>
<%= vehicle.registered %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">License plate:</strong>
<%= vehicle.license_plate %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">Type:</strong>
<%= vehicle.vehicle_type %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">Baujahr:</strong>
<%= vehicle.model_year %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">Marke:</strong>
<%= vehicle.car_brand %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">Modell:</strong>
<%= vehicle.model %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">Treibstoffart:</strong>
<%= vehicle.fuel_type %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">PS:</strong>
<%= vehicle.power_ps %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">KW:</strong>
<%= vehicle.power_kw %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">Farbe:</strong>
<%= vehicle.color %>
</p>
<p class="my-5">
<strong class="block mb-1 font-medium">Notizen:</strong>
<%= vehicle.note %>
</p>
</div>

View File

@@ -0,0 +1,2 @@
json.extract! vehicle, :id, :license_plate, :type, :model_year, :car_brand, :model, :fuel_type, :created_at, :updated_at
json.url vehicle_url(vehicle, format: :json)

View File

@@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing vehicle</h1>
<%= render "form", vehicle: @vehicle %>
<%= link_to "Show this vehicle", @vehicle, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to vehicles", vehicles_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,21 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<% content_for :title, "Vehicles" %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Vehicles</h1>
<%= link_to "New vehicle", new_vehicle_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="vehicles" class="min-w-full">
<% @vehicles.each do |vehicle| %>
<%= render vehicle %>
<p>
<%= link_to "Show this vehicle", vehicle, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</p>
<% end %>
</div>
</div>

View File

@@ -0,0 +1 @@
json.array! @vehicles, partial: "vehicles/vehicle", as: :vehicle

View File

@@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New vehicle</h1>
<%= render "form", vehicle: @vehicle %>
<%= link_to "Back to vehicles", vehicles_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @vehicle %>
<%= link_to "Edit this vehicle", edit_vehicle_path(@vehicle), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to vehicles", vehicles_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to "Destroy this vehicle", @vehicle, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
</div>
</div>

View File

@@ -0,0 +1 @@
json.partial! "vehicles/vehicle", vehicle: @vehicle

View File

@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :vehicles
get "sign_in", to: "sessions#new"
post "sign_in", to: "sessions#create"
get "sign_up", to: "registrations#new"

View File

@@ -0,0 +1,19 @@
class CreateVehicles < ActiveRecord::Migration[7.2]
def change
create_table :vehicles do |t|
t.integer :vehicle_type
t.string :car_brand
t.string :model
t.integer :model_year
t.string :fuel_type
t.integer :power_ps
t.integer :power_kw
t.boolean :registered
t.string :license_plate
t.string :color
t.text :note
t.timestamps
end
end
end

18
db/schema.rb generated
View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2024_08_19_011351) do
ActiveRecord::Schema[7.2].define(version: 2024_08_20_165836) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -57,6 +57,22 @@ ActiveRecord::Schema[7.2].define(version: 2024_08_19_011351) do
t.index ["email"], name: "index_users_on_email", unique: true
end
create_table "vehicles", force: :cascade do |t|
t.integer "vehicle_type"
t.string "car_brand"
t.string "model"
t.integer "model_year"
t.string "fuel_type"
t.integer "power_ps"
t.integer "power_kw"
t.boolean "registered"
t.string "license_plate"
t.string "color"
t.text "note"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "sessions", "users"

View File

@@ -9,3 +9,4 @@
# end
User.create(email: "admin@admin.de", password_digest: BCrypt::Password.create("admin"), verified: true)
Vehicle.create(vehicle_type: :car, car_brand: "VW", model: "Golf 1 Cabrio", model_year: 1992, fuel_type: "Benzin", power_ps: 90, power_kw: 72, registered: true, license_plate: "SWA-DN-10", color: "Tornado Red")

View File

@@ -0,0 +1,48 @@
require "test_helper"
class VehiclesControllerTest < ActionDispatch::IntegrationTest
setup do
@vehicle = vehicles(:one)
end
test "should get index" do
get vehicles_url
assert_response :success
end
test "should get new" do
get new_vehicle_url
assert_response :success
end
test "should create vehicle" do
assert_difference("Vehicle.count") do
post vehicles_url, params: { vehicle: { car_brand: @vehicle.car_brand, fuel_type: @vehicle.fuel_type, license_plate: @vehicle.license_plate, model: @vehicle.model, model_year: @vehicle.model_year, type: @vehicle.type } }
end
assert_redirected_to vehicle_url(Vehicle.last)
end
test "should show vehicle" do
get vehicle_url(@vehicle)
assert_response :success
end
test "should get edit" do
get edit_vehicle_url(@vehicle)
assert_response :success
end
test "should update vehicle" do
patch vehicle_url(@vehicle), params: { vehicle: { car_brand: @vehicle.car_brand, fuel_type: @vehicle.fuel_type, license_plate: @vehicle.license_plate, model: @vehicle.model, model_year: @vehicle.model_year, type: @vehicle.type } }
assert_redirected_to vehicle_url(@vehicle)
end
test "should destroy vehicle" do
assert_difference("Vehicle.count", -1) do
delete vehicle_url(@vehicle)
end
assert_redirected_to vehicles_url
end
end

17
test/fixtures/vehicles.yml vendored Normal file
View File

@@ -0,0 +1,17 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
license_plate: MyString
type:
model_year: 2024-08-20 18:58:36
car_brand: MyString
model: MyString
fuel_type: MyString
two:
license_plate: MyString
type:
model_year: 2024-08-20 18:58:36
car_brand: MyString
model: MyString
fuel_type: MyString

View File

@@ -0,0 +1,7 @@
require "test_helper"
class VehicleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@@ -0,0 +1,51 @@
require "application_system_test_case"
class VehiclesTest < ApplicationSystemTestCase
setup do
@vehicle = vehicles(:one)
end
test "visiting the index" do
visit vehicles_url
assert_selector "h1", text: "Vehicles"
end
test "should create vehicle" do
visit vehicles_url
click_on "New vehicle"
fill_in "Car brand", with: @vehicle.car_brand
fill_in "Fuel type", with: @vehicle.fuel_type
fill_in "License plate", with: @vehicle.license_plate
fill_in "Model", with: @vehicle.model
fill_in "Model year", with: @vehicle.model_year
fill_in "Type", with: @vehicle.type
click_on "Create Vehicle"
assert_text "Vehicle was successfully created"
click_on "Back"
end
test "should update Vehicle" do
visit vehicle_url(@vehicle)
click_on "Edit this vehicle", match: :first
fill_in "Car brand", with: @vehicle.car_brand
fill_in "Fuel type", with: @vehicle.fuel_type
fill_in "License plate", with: @vehicle.license_plate
fill_in "Model", with: @vehicle.model
fill_in "Model year", with: @vehicle.model_year.to_s
fill_in "Type", with: @vehicle.type
click_on "Update Vehicle"
assert_text "Vehicle was successfully updated"
click_on "Back"
end
test "should destroy Vehicle" do
visit vehicle_url(@vehicle)
click_on "Destroy this vehicle", match: :first
assert_text "Vehicle was successfully destroyed"
end
end