From 1656508ccdf55182a8d817fc36e5d455a702f37e Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 29 Jan 2024 22:04:56 -0600 Subject: [PATCH] Removed all entries for all models and changed Employer model to reflect first name and last name instead of Name --- app/controllers/employers_controller.rb | 2 +- app/models/employer.rb | 5 +++++ app/views/employers/_form.html.erb | 9 +++++++-- app/views/employers/index.html.erb | 5 +++-- app/views/employers/show.html.erb | 2 +- db/migrate/20240130033832_rename_name_in_employers.rb | 6 ++++++ db/schema.rb | 5 +++-- lib/tasks/update.rake | 9 +++++++++ 8 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20240130033832_rename_name_in_employers.rb create mode 100644 lib/tasks/update.rake diff --git a/app/controllers/employers_controller.rb b/app/controllers/employers_controller.rb index 1518fe3..e8dfa31 100644 --- a/app/controllers/employers_controller.rb +++ b/app/controllers/employers_controller.rb @@ -3,7 +3,7 @@ class EmployersController < ApplicationController # GET /employers def index - @employers = Employer.order(:name) + @employers = Employer.order(:last_name) end # GET /employers/:id diff --git a/app/models/employer.rb b/app/models/employer.rb index 91bccd1..8bacad4 100644 --- a/app/models/employer.rb +++ b/app/models/employer.rb @@ -2,5 +2,10 @@ class Employer < ApplicationRecord has_many :participants has_many :workers, through: :participants # Association with Vendor if needed + + def full_name + "#{first_name} #{last_name}" + end + end \ No newline at end of file diff --git a/app/views/employers/_form.html.erb b/app/views/employers/_form.html.erb index 82b61ce..ca171a0 100644 --- a/app/views/employers/_form.html.erb +++ b/app/views/employers/_form.html.erb @@ -11,8 +11,13 @@ <% end %>
- <%= form.label :name, class: 'form-label' %> - <%= form.text_field :name, class: 'form-control' %> + <%= form.label :first_name, class: 'form-label' %> + <%= form.text_field :first_name, class: 'form-control' %> +
+ +
+ <%= form.label :last_name, class: 'form-label' %> + <%= form.text_field :last_name, class: 'form-control' %>
diff --git a/app/views/employers/index.html.erb b/app/views/employers/index.html.erb index b2742d7..ab7724d 100644 --- a/app/views/employers/index.html.erb +++ b/app/views/employers/index.html.erb @@ -10,7 +10,8 @@ - + + @@ -21,7 +22,7 @@ <% @employers.each do |employer| %> - + diff --git a/app/views/employers/show.html.erb b/app/views/employers/show.html.erb index f0e76dc..fc21542 100644 --- a/app/views/employers/show.html.erb +++ b/app/views/employers/show.html.erb @@ -7,7 +7,7 @@

Name: - <%= @employer.name %> + <%= @employer.first_name %> <%= @employer.last_name %>

diff --git a/db/migrate/20240130033832_rename_name_in_employers.rb b/db/migrate/20240130033832_rename_name_in_employers.rb new file mode 100644 index 0000000..8c7629d --- /dev/null +++ b/db/migrate/20240130033832_rename_name_in_employers.rb @@ -0,0 +1,6 @@ +class RenameNameInEmployers < ActiveRecord::Migration[7.1] + def change + rename_column :employers, :name, :first_name + add_column :employers, :last_name, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 0e918c9..7ae1686 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,9 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_01_23_235419) do +ActiveRecord::Schema[7.1].define(version: 2024_01_30_033832) do create_table "employers", force: :cascade do |t| - t.string "name" + t.string "first_name" t.string "address" t.string "phone" t.string "email" @@ -22,6 +22,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_235419) do t.string "gender" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "last_name" end create_table "employers_vendors", id: false, force: :cascade do |t| diff --git a/lib/tasks/update.rake b/lib/tasks/update.rake new file mode 100644 index 0000000..ceffc9f --- /dev/null +++ b/lib/tasks/update.rake @@ -0,0 +1,9 @@ +namespace :update do + desc "Update employer names" + task employer_names: :environment do + Employer.find_each do |employer| + names = employer.name.split + employer.update(first_name: names.first, last_name: names.drop(1).join(' ')) + end + end +end

NameFirst NameLast Name Address Phone Email
<%= employer.name %><%= employer.first_name %> <%= employer.last_name %> <%= employer.address %> <%= employer.phone %> <%= employer.email %>