diff --git a/app/admin/users_admin.rb b/app/admin/users_admin.rb index 8441608..f2ca127 100644 --- a/app/admin/users_admin.rb +++ b/app/admin/users_admin.rb @@ -12,6 +12,7 @@ Trestle.resource(:users) do # Customize the table columns shown on the index view. table do column :name + column :label column :created_at, align: :center actions do |toolbar, instance, admin| toolbar.edit if admin && admin.actions.include?(:edit) @@ -19,6 +20,7 @@ Trestle.resource(:users) do toolbar.link 'CV', instance, action: :generate, method: :get, style: :primary, icon: "fa fa-file-pdf", target: "_blank" toolbar.link 'CLS', instance, action: :generate_cls, method: :get, style: :secondary, icon: "fa", target: "_blank" toolbar.link 'JSON', instance, action: :generate_json, method: :get, style: :secondary, icon: "fa", target: "_blank" + # TODO: Add a new action to clone the user and all its associations passing a "cloned" label to the new user. end end diff --git a/app/models/user.rb b/app/models/user.rb index ec7f2c2..b3723fd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,4 +2,25 @@ class User < ApplicationRecord has_many :work_experiences has_many :skills has_many :education_entries + has_many :cover_letters + + def clone!(label:) + self.class.transaction do + cloned_user = dup + cloned_user.label = label + cloned_user.save! + + self.class.reflect_on_all_associations(:has_many).each do |association| + next if association.options[:through] + + public_send(association.name).find_each do |record| + cloned_record = record.dup + cloned_record[association.foreign_key] = cloned_user.id + cloned_record.save! + end + end + + cloned_user + end + end end diff --git a/db/migrate/20260224131848_add_label_to_users.rb b/db/migrate/20260224131848_add_label_to_users.rb new file mode 100644 index 0000000..0700785 --- /dev/null +++ b/db/migrate/20260224131848_add_label_to_users.rb @@ -0,0 +1,5 @@ +class AddLabelToUsers < ActiveRecord::Migration[7.0] + def change + add_column :users, :label, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index e344d4b..39b4227 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.0].define(version: 2024_11_04_003755) do +ActiveRecord::Schema[7.0].define(version: 2026_02_24_131848) do create_table "cover_letters", force: :cascade do |t| t.string "title" t.string "company_name" @@ -62,6 +62,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_11_04_003755) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "title" + t.string "label" end create_table "work_experiences", force: :cascade do |t|