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? %> -
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