From 132ea2c51dca647287002bd379335c489b5b0aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20B=C3=B6hm?= Date: Tue, 20 Aug 2024 19:50:29 +0200 Subject: [PATCH] Added vehicle scaffold --- app/controllers/vehicles_controller.rb | 73 ++++++++++++++++++++ app/helpers/vehicles_helper.rb | 2 + app/models/vehicle.rb | 3 + app/views/vehicles/_form.html.erb | 39 +++++++++++ app/views/vehicles/_vehicle.html.erb | 46 ++++++++++++ app/views/vehicles/_vehicle.json.jbuilder | 2 + app/views/vehicles/edit.html.erb | 8 +++ app/views/vehicles/index.html.erb | 21 ++++++ app/views/vehicles/index.json.jbuilder | 1 + app/views/vehicles/new.html.erb | 7 ++ app/views/vehicles/show.html.erb | 15 ++++ app/views/vehicles/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20240820165836_create_vehicles.rb | 19 +++++ db/schema.rb | 18 ++++- db/seeds.rb | 1 + test/controllers/vehicles_controller_test.rb | 48 +++++++++++++ test/fixtures/vehicles.yml | 17 +++++ test/models/vehicle_test.rb | 7 ++ test/system/vehicles_test.rb | 51 ++++++++++++++ 20 files changed, 379 insertions(+), 1 deletion(-) create mode 100644 app/controllers/vehicles_controller.rb create mode 100644 app/helpers/vehicles_helper.rb create mode 100644 app/models/vehicle.rb create mode 100644 app/views/vehicles/_form.html.erb create mode 100644 app/views/vehicles/_vehicle.html.erb create mode 100644 app/views/vehicles/_vehicle.json.jbuilder create mode 100644 app/views/vehicles/edit.html.erb create mode 100644 app/views/vehicles/index.html.erb create mode 100644 app/views/vehicles/index.json.jbuilder create mode 100644 app/views/vehicles/new.html.erb create mode 100644 app/views/vehicles/show.html.erb create mode 100644 app/views/vehicles/show.json.jbuilder create mode 100644 db/migrate/20240820165836_create_vehicles.rb create mode 100644 test/controllers/vehicles_controller_test.rb create mode 100644 test/fixtures/vehicles.yml create mode 100644 test/models/vehicle_test.rb create mode 100644 test/system/vehicles_test.rb diff --git a/app/controllers/vehicles_controller.rb b/app/controllers/vehicles_controller.rb new file mode 100644 index 0000000..e9376f1 --- /dev/null +++ b/app/controllers/vehicles_controller.rb @@ -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 diff --git a/app/helpers/vehicles_helper.rb b/app/helpers/vehicles_helper.rb new file mode 100644 index 0000000..f8e7abd --- /dev/null +++ b/app/helpers/vehicles_helper.rb @@ -0,0 +1,2 @@ +module VehiclesHelper +end diff --git a/app/models/vehicle.rb b/app/models/vehicle.rb new file mode 100644 index 0000000..34ee24c --- /dev/null +++ b/app/models/vehicle.rb @@ -0,0 +1,3 @@ +class Vehicle < ApplicationRecord + enum vehicle_type: { car: 0, truck: 1, motorcycle: 2, van: 3} +end diff --git a/app/views/vehicles/_form.html.erb b/app/views/vehicles/_form.html.erb new file mode 100644 index 0000000..f49debc --- /dev/null +++ b/app/views/vehicles/_form.html.erb @@ -0,0 +1,39 @@ +<%= form_with(model: vehicle, class: "contents") do |form| %> + <% if vehicle.errors.any? %> +
+

<%= pluralize(vehicle.errors.count, "error") %> prohibited this vehicle from being saved:

+ +
+ <% end %> +
+ <%= 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" %> +
+
+ <%= 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" %> +
+
+ <%= 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" %> +
+
+ <%= 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" %> +
+
+ <%= 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" %> +
+
+ <%= 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" %> +
+
+ <%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> +
+<% end %> diff --git a/app/views/vehicles/_vehicle.html.erb b/app/views/vehicles/_vehicle.html.erb new file mode 100644 index 0000000..03fa4e1 --- /dev/null +++ b/app/views/vehicles/_vehicle.html.erb @@ -0,0 +1,46 @@ +
+

+ Angemeldet: + <%= vehicle.registered %> +

+

+ License plate: + <%= vehicle.license_plate %> +

+

+ Type: + <%= vehicle.vehicle_type %> +

+

+ Baujahr: + <%= vehicle.model_year %> +

+

+ Marke: + <%= vehicle.car_brand %> +

+

+ Modell: + <%= vehicle.model %> +

+

+ Treibstoffart: + <%= vehicle.fuel_type %> +

+

+ PS: + <%= vehicle.power_ps %> +

+

+ KW: + <%= vehicle.power_kw %> +

+

+ Farbe: + <%= vehicle.color %> +

+

+ Notizen: + <%= vehicle.note %> +

+
diff --git a/app/views/vehicles/_vehicle.json.jbuilder b/app/views/vehicles/_vehicle.json.jbuilder new file mode 100644 index 0000000..6cf989e --- /dev/null +++ b/app/views/vehicles/_vehicle.json.jbuilder @@ -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) diff --git a/app/views/vehicles/edit.html.erb b/app/views/vehicles/edit.html.erb new file mode 100644 index 0000000..63da4e0 --- /dev/null +++ b/app/views/vehicles/edit.html.erb @@ -0,0 +1,8 @@ +
+

Editing vehicle

+ + <%= 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" %> +
diff --git a/app/views/vehicles/index.html.erb b/app/views/vehicles/index.html.erb new file mode 100644 index 0000000..ee11aae --- /dev/null +++ b/app/views/vehicles/index.html.erb @@ -0,0 +1,21 @@ +
+ <% if notice.present? %> +

<%= notice %>

+ <% end %> + + <% content_for :title, "Vehicles" %> + +
+

Vehicles

+ <%= link_to "New vehicle", new_vehicle_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> +
+ +
+ <% @vehicles.each do |vehicle| %> + <%= render vehicle %> +

+ <%= link_to "Show this vehicle", vehicle, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +

+ <% end %> +
+
diff --git a/app/views/vehicles/index.json.jbuilder b/app/views/vehicles/index.json.jbuilder new file mode 100644 index 0000000..26e0dd7 --- /dev/null +++ b/app/views/vehicles/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @vehicles, partial: "vehicles/vehicle", as: :vehicle diff --git a/app/views/vehicles/new.html.erb b/app/views/vehicles/new.html.erb new file mode 100644 index 0000000..4a054a8 --- /dev/null +++ b/app/views/vehicles/new.html.erb @@ -0,0 +1,7 @@ +
+

New vehicle

+ + <%= 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" %> +
diff --git a/app/views/vehicles/show.html.erb b/app/views/vehicles/show.html.erb new file mode 100644 index 0000000..c3b1b2c --- /dev/null +++ b/app/views/vehicles/show.html.erb @@ -0,0 +1,15 @@ +
+
+ <% if notice.present? %> +

<%= notice %>

+ <% 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" %> +
+ <%= button_to "Destroy this vehicle", @vehicle, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %> +
+
+
diff --git a/app/views/vehicles/show.json.jbuilder b/app/views/vehicles/show.json.jbuilder new file mode 100644 index 0000000..e5a5475 --- /dev/null +++ b/app/views/vehicles/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "vehicles/vehicle", vehicle: @vehicle diff --git a/config/routes.rb b/config/routes.rb index 74131bd..8998483 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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" diff --git a/db/migrate/20240820165836_create_vehicles.rb b/db/migrate/20240820165836_create_vehicles.rb new file mode 100644 index 0000000..030acdc --- /dev/null +++ b/db/migrate/20240820165836_create_vehicles.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index aa8eb00..76c7f85 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/db/seeds.rb b/db/seeds.rb index 6069cbf..008364f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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") \ No newline at end of file diff --git a/test/controllers/vehicles_controller_test.rb b/test/controllers/vehicles_controller_test.rb new file mode 100644 index 0000000..9233ff8 --- /dev/null +++ b/test/controllers/vehicles_controller_test.rb @@ -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 diff --git a/test/fixtures/vehicles.yml b/test/fixtures/vehicles.yml new file mode 100644 index 0000000..fe476d4 --- /dev/null +++ b/test/fixtures/vehicles.yml @@ -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 diff --git a/test/models/vehicle_test.rb b/test/models/vehicle_test.rb new file mode 100644 index 0000000..71c13e1 --- /dev/null +++ b/test/models/vehicle_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class VehicleTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/vehicles_test.rb b/test/system/vehicles_test.rb new file mode 100644 index 0000000..0cbd94f --- /dev/null +++ b/test/system/vehicles_test.rb @@ -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