From d322ba1a832c0f1c511f73a45a6fcbc350e1d5c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20B=C3=B6hm?= Date: Wed, 31 Jul 2024 06:48:43 +0200 Subject: [PATCH] Added user model and fixed reference in migration --- app/controllers/jobs_controller.rb | 32 +++++----- app/models/job.rb | 4 +- app/models/user.rb | 3 + app/views/jobs/_form.html.erb | 74 +++++++++-------------- app/views/jobs/_job.html.erb | 4 +- app/views/jobs/_job.json.jbuilder | 2 +- db/migrate/20240727101347_create_jobs.rb | 4 +- db/migrate/20240730214152_create_users.rb | 13 ++++ db/schema.rb | 21 ++++--- test/fixtures/users.yml | 9 +++ test/models/user_test.rb | 7 +++ 11 files changed, 98 insertions(+), 75 deletions(-) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20240730214152_create_users.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index a3336fe..2d0f07f 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -1,5 +1,5 @@ class JobsController < ApplicationController - before_action :set_job, only: %i[ show edit update destroy ] + before_action :set_job, only: %i[show edit update destroy] # GET /jobs or /jobs.json def index @@ -7,8 +7,7 @@ class JobsController < ApplicationController end # GET /jobs/1 or /jobs/1.json - def show - end + def show; end # GET /jobs/new def new @@ -16,8 +15,7 @@ class JobsController < ApplicationController end # GET /jobs/1/edit - def edit - end + def edit; end # POST /jobs or /jobs.json def create @@ -25,7 +23,7 @@ class JobsController < ApplicationController respond_to do |format| if @job.save - format.html { redirect_to job_url(@job), notice: "Job was successfully created." } + format.html { redirect_to job_url(@job), notice: 'Job was successfully created.' } format.json { render :show, status: :created, location: @job } else format.html { render :new, status: :unprocessable_entity } @@ -38,7 +36,7 @@ class JobsController < ApplicationController def update respond_to do |format| if @job.update(job_params) - format.html { redirect_to job_url(@job), notice: "Job was successfully updated." } + format.html { redirect_to job_url(@job), notice: 'Job was successfully updated.' } format.json { render :show, status: :ok, location: @job } else format.html { render :edit, status: :unprocessable_entity } @@ -52,19 +50,21 @@ class JobsController < ApplicationController @job.destroy! respond_to do |format| - format.html { redirect_to jobs_url, notice: "Job was successfully destroyed." } + format.html { redirect_to jobs_url, notice: 'Job was successfully destroyed.' } format.json { head :no_content } end end private - # Use callbacks to share common setup or constraints between actions. - def set_job - @job = Job.find(params[:id]) - end - # Only allow a list of trusted parameters through. - def job_params - params.require(:job).permit(:operator_id_id, :costumer_id_id, :operator_firstname, :operator_lastname, :costumer_firstname, :costumer_lastname, :paid, :printed_at, :intern, :cost_center, :number_of_plans_a0, :number_of_plans_a1, :number_of_plans_a2, :number_of_plans_a3, :costum_qm_plan) - end + # Use callbacks to share common setup or constraints between actions. + def set_job + @job = Job.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def job_params + params.require(:job).permit(:operator_id, :costumer_id, :operator_firstname, :operator_lastname, + :costumer_firstname, :costumer_lastname, :paid, :printed_at, :intern, :cost_center, :number_of_plans_a0, :number_of_plans_a1, :number_of_plans_a2, :number_of_plans_a3, :costum_qm_plan) + end end diff --git a/app/models/job.rb b/app/models/job.rb index 0c77567..e5a594e 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -1,4 +1,4 @@ class Job < ApplicationRecord - belongs_to :operator_id - belongs_to :costumer_id + belongs_to :operator, class_name: 'User', optional: true + belongs_to :costumer, class_name: 'User', optional: true end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..def4e28 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,3 @@ +class User < ApplicationRecord + has_many :Jobs, foreign_key: :costumer_id +end diff --git a/app/views/jobs/_form.html.erb b/app/views/jobs/_form.html.erb index 22fcc88..e55b324 100644 --- a/app/views/jobs/_form.html.erb +++ b/app/views/jobs/_form.html.erb @@ -1,7 +1,11 @@ <%= form_with(model: job, class: "contents") do |form| %> <% if job.errors.any? %> -
-

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

+
+

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

    <% job.errors.each do |error| %> @@ -11,44 +15,18 @@
<% end %> -
- <%= form.label :operator_id_id %> - <%= form.text_field :operator_id_id, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> -
- -
- <%= form.label :costumer_id_id %> - <%= form.text_field :costumer_id_id, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> -
- -
- <%= form.label :operator_firstname %> - <%= form.text_field :operator_firstname, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> -
- -
- <%= form.label :operator_lastname %> - <%= form.text_field :operator_lastname, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> -
-
<%= form.label :costumer_firstname %> - <%= form.text_field :costumer_firstname, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> + <%= form.text_field :costumer_firstname, + class: + "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
<%= form.label :costumer_lastname %> - <%= form.text_field :costumer_lastname, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> -
- -
- <%= form.label :paid %> - <%= form.check_box :paid, class: "block mt-2 h-5 w-5" %> -
- -
- <%= form.label :printed_at %> - <%= form.datetime_field :printed_at, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> + <%= form.text_field :costumer_lastname, + class: + "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
@@ -58,35 +36,41 @@
<%= form.label :cost_center %> - <%= form.text_field :cost_center, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> + <%= form.text_field :cost_center, + class: + "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
<%= form.label :number_of_plans_a0 %> - <%= form.number_field :number_of_plans_a0, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> + <%= form.number_field :number_of_plans_a0, + class: + "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
<%= form.label :number_of_plans_a1 %> - <%= form.number_field :number_of_plans_a1, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> + <%= form.number_field :number_of_plans_a1, + class: + "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
<%= form.label :number_of_plans_a2 %> - <%= form.number_field :number_of_plans_a2, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> + <%= form.number_field :number_of_plans_a2, + class: + "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
<%= form.label :number_of_plans_a3 %> - <%= form.number_field :number_of_plans_a3, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> -
- -
- <%= form.label :costum_qm_plan %> - <%= form.text_field :costum_qm_plan, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> + <%= form.number_field :number_of_plans_a3, + 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" %> + <%= 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/jobs/_job.html.erb b/app/views/jobs/_job.html.erb index dbb9932..09334b9 100644 --- a/app/views/jobs/_job.html.erb +++ b/app/views/jobs/_job.html.erb @@ -1,12 +1,12 @@

Operator: - <%= job.operator_id_id %> + <%= job.operator_id %>

Costumer: - <%= job.costumer_id_id %> + <%= job.costumer_id %>

diff --git a/app/views/jobs/_job.json.jbuilder b/app/views/jobs/_job.json.jbuilder index 324eb8c..56bf029 100644 --- a/app/views/jobs/_job.json.jbuilder +++ b/app/views/jobs/_job.json.jbuilder @@ -1,2 +1,2 @@ -json.extract! job, :id, :operator_id_id, :costumer_id_id, :operator_firstname, :operator_lastname, :costumer_firstname, :costumer_lastname, :paid, :printed_at, :intern, :cost_center, :number_of_plans_a0, :number_of_plans_a1, :number_of_plans_a2, :number_of_plans_a3, :costum_qm_plan, :created_at, :updated_at +json.extract! job, :id, :operator_id, :costumer_id, :operator_firstname, :operator_lastname, :costumer_firstname, :costumer_lastname, :paid, :printed_at, :intern, :cost_center, :number_of_plans_a0, :number_of_plans_a1, :number_of_plans_a2, :number_of_plans_a3, :costum_qm_plan, :created_at, :updated_at json.url job_url(job, format: :json) diff --git a/db/migrate/20240727101347_create_jobs.rb b/db/migrate/20240727101347_create_jobs.rb index 9497dde..9949596 100644 --- a/db/migrate/20240727101347_create_jobs.rb +++ b/db/migrate/20240727101347_create_jobs.rb @@ -1,8 +1,8 @@ class CreateJobs < ActiveRecord::Migration[7.1] def change create_table :jobs do |t| - t.references :operator_id, null: false, foreign_key: true - t.references :costumer_id, null: false, foreign_key: true + t.references :operator, null: true + t.references :costumer, null: true t.string :operator_firstname t.string :operator_lastname t.string :costumer_firstname diff --git a/db/migrate/20240730214152_create_users.rb b/db/migrate/20240730214152_create_users.rb new file mode 100644 index 0000000..81b5a9f --- /dev/null +++ b/db/migrate/20240730214152_create_users.rb @@ -0,0 +1,13 @@ +class CreateUsers < ActiveRecord::Migration[7.1] + def change + create_table :users do |t| + t.string :firstname + t.string :lastname + + t.timestamps + end + + add_foreign_key :jobs, :users, column: :operator_id + add_foreign_key :jobs, :users, column: :costumer_id + end +end diff --git a/db/schema.rb b/db/schema.rb index ddd323a..58a4e89 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,10 +10,10 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_07_27_101347) do +ActiveRecord::Schema[7.1].define(version: 2024_07_30_214152) do create_table "jobs", force: :cascade do |t| - t.integer "operator_id_id", null: false - t.integer "costumer_id_id", null: false + t.integer "operator_id" + t.integer "costumer_id" t.string "operator_firstname" t.string "operator_lastname" t.string "costumer_firstname" @@ -29,10 +29,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_27_101347) do t.float "costum_qm_plan" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["costumer_id_id"], name: "index_jobs_on_costumer_id_id" - t.index ["operator_id_id"], name: "index_jobs_on_operator_id_id" + t.index ["costumer_id"], name: "index_jobs_on_costumer_id" + t.index ["operator_id"], name: "index_jobs_on_operator_id" end - add_foreign_key "jobs", "costumer_ids" - add_foreign_key "jobs", "operator_ids" + create_table "users", force: :cascade do |t| + t.string "firstname" + t.string "lastname" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_foreign_key "jobs", "users", column: "costumer_id" + add_foreign_key "jobs", "users", column: "operator_id" end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..e7ec08b --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + firstname: MyString + lastname: MyString + +two: + firstname: MyString + lastname: MyString diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..5c07f49 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end