clone_user #2

Merged
rodo merged 4 commits from clone_user into main 2 weeks ago
  1. 4
      app/admin/cover_letters_admin.rb
  2. 4
      app/admin/education_entries_admin.rb
  3. 4
      app/admin/skills_admin.rb
  4. 119
      app/admin/users_admin.rb
  5. 3
      app/admin/work_experiences_admin.rb
  6. 21
      app/models/user.rb
  7. 5
      db/migrate/20260224131848_add_label_to_users.rb
  8. 3
      db/schema.rb

4
app/admin/cover_letters_admin.rb

@ -1,8 +1,4 @@
Trestle.resource(:cover_letters) do Trestle.resource(:cover_letters) do
menu do
item :cover_letters, icon: "fa fa-envelope", priority: 2
end
form do |item| form do |item|
concat Trestle::Form::Automatic.new(admin).render(self, item) concat Trestle::Form::Automatic.new(admin).render(self, item)
# text_area :objective # text_area :objective

4
app/admin/education_entries_admin.rb

@ -1,8 +1,4 @@
Trestle.resource(:education_entries) do Trestle.resource(:education_entries) do
menu do
item :education_entries, icon: "fa fa-graduation-cap", priority: 4
end
form do |item| form do |item|
concat Trestle::Form::Automatic.new(admin).render(self, item) concat Trestle::Form::Automatic.new(admin).render(self, item)
end end

4
app/admin/skills_admin.rb

@ -1,8 +1,4 @@
Trestle.resource(:skills) do Trestle.resource(:skills) do
menu do
item :skills, icon: "fa fa-toolbox", priority: 5
end
form do |item| form do |item|
concat Trestle::Form::Automatic.new(admin).render(self, item) concat Trestle::Form::Automatic.new(admin).render(self, item)
end end

119
app/admin/users_admin.rb

@ -1,24 +1,120 @@
Trestle.resource(:users) do Trestle.resource(:users) do
menu do menu do
item :users, icon: "fa fa-user", priority: 1 item :resumes, icon: "fa fa-file-alt", priority: 1
end end
form do |item| form do |item|
tab :details, label: "Details" do
concat Trestle::Form::Automatic.new(admin).render(self, item) concat Trestle::Form::Automatic.new(admin).render(self, item)
text_area :objective text_area :objective
end end
tab :export, label: "Export Resume" do
safe_join([
link_to("Export as PDF", generate_users_admin_path(item), class: "btn btn-primary has-icon", target: "_blank"),
link_to("Export as TeX", generate_cls_users_admin_path(item), class: "btn btn-secondary has-icon", target: "_blank"),
link_to("Export as JSON", generate_json_users_admin_path(item), class: "btn btn-secondary has-icon", target: "_blank")
], " ")
end
tab :work_experiences, badge: (item.persisted? ? item.work_experiences.size : 0) do
concat content_tag(:p, "User: #{item.to_s}")
if item.persisted?
concat link_to("Add Work Experience", WorkExperiencesAdmin.path(:new, user_id: item.id))
table item.work_experiences do
column :employer
column :title
column :period
column :reference_date, align: :center
column :actions, align: :right do |experience|
# TODO: The redirect after succesful delete goes back to the index page.. it should go back to the user page
safe_join([
link_to("Edit", WorkExperiencesAdmin.path(:edit, id: experience.id), class: "btn btn-secondary has-icon"),
link_to("Delete", work_experiences_admin_path(experience), class: "btn btn-danger has-icon", method: :delete, data: { confirm: "Are you sure?" })
], " | ")
end
end
else
concat content_tag(:p, "Save the user before adding work experiences.")
end
end
tab :education_entries, badge: (item.persisted? ? item.education_entries.size : 0) do
concat content_tag(:p, "User: #{item.to_s}")
if item.persisted?
concat link_to("Add Education Entry", EducationEntriesAdmin.path(:new, user_id: item.id))
table item.education_entries do
column :institution
column :title
column :period
column :location
column :actions, align: :right do |entry|
safe_join([
link_to("Edit", EducationEntriesAdmin.path(:edit, id: entry.id), class: "btn btn-secondary has-icon"),
link_to("Delete", EducationEntriesAdmin.path(:destroy, id: entry.id), class: "btn btn-danger has-icon", method: :delete, data: { confirm: "Are you sure?" })
], " | ")
end
end
else
concat content_tag(:p, "Save the user before adding education entries.")
end
end
tab :skills, badge: (item.persisted? ? item.skills.size : 0) do
concat content_tag(:p, "User: #{item.to_s}")
if item.persisted?
concat link_to("Add Skill", SkillsAdmin.path(:new, user_id: item.id))
table item.skills do
column :title
column :detail
column :order, align: :center
column :actions, align: :right do |skill|
safe_join([
link_to("Edit", SkillsAdmin.path(:edit, id: skill.id), class: "btn btn-secondary has-icon"),
link_to("Delete", SkillsAdmin.path(:destroy, id: skill.id), class: "btn btn-danger has-icon", method: :delete, data: { confirm: "Are you sure?" })
], " | ")
end
end
else
concat content_tag(:p, "Save the user before adding skills.")
end
end
tab :cover_letters, badge: (item.persisted? ? item.cover_letters.size : 0) do
concat content_tag(:p, "User: #{item.to_s}")
if item.persisted?
concat link_to("Add Cover Letter", CoverLettersAdmin.path(:new, user_id: item.id))
table item.cover_letters do
column :title
column :company_name
column :job_application
column :order, align: :center
column :actions, align: :right do |cover_letter|
safe_join([
link_to("Edit", CoverLettersAdmin.path(:edit, id: cover_letter.id), class: "btn btn-secondary has-icon"),
link_to("Delete", CoverLettersAdmin.path(:destroy, id: cover_letter.id), class: "btn btn-danger has-icon", method: :delete, data: { confirm: "Are you sure?" })
], " | ")
end
end
else
concat content_tag(:p, "Save the user before adding cover letters.")
end
end
end
# Customize the table columns shown on the index view. # Customize the table columns shown on the index view.
table do table do
column :name column :name
column :label
column :created_at, align: :center column :created_at, align: :center
actions do |toolbar, instance, admin| actions do |toolbar, instance, admin|
toolbar.edit if admin && admin.actions.include?(:edit) toolbar.edit if admin && admin.actions.include?(:edit)
toolbar.delete if admin && admin.actions.include?(:destroy) toolbar.delete if admin && admin.actions.include?(:destroy)
toolbar.link 'CV', instance, action: :generate, method: :get, style: :primary, icon: "fa fa-file-pdf", target: "_blank" toolbar.link 'Clone', instance, action: :clone, method: :post, style: :secondary, icon: "fa fa-copy"
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"
end end
end end
@ -44,11 +140,8 @@ Trestle.resource(:users) do
end end
def generate_json def generate_json
generate_json_resume
end
def generate_json_resume
@user = admin.find_instance(params) @user = admin.find_instance(params)
response.headers["Content-Disposition"] = "attachment; filename=resume.json"
render template: "users/show", formats: [:json], layout: false render template: "users/show", formats: [:json], layout: false
end end
@ -57,11 +150,19 @@ Trestle.resource(:users) do
ac = ActionController::Base.new() ac = ActionController::Base.new()
ac.render_to_string(layout: false, template: "templates/awesome/#{template}", locals: { "@user": user } ) ac.render_to_string(layout: false, template: "templates/awesome/#{template}", locals: { "@user": user } )
end end
def clone
user = admin.find_instance(params)
cloned_user = user.clone!(label: "cloned")
flash[:message] = "User cloned as ##{cloned_user.id}."
redirect_to users_admin_path(cloned_user)
end
end end
routes do routes do
get :generate, on: :member get :generate, on: :member
get :generate_cls, on: :member get :generate_cls, on: :member
get :generate_json, on: :member get :generate_json, on: :member
post :clone, on: :member
end end
end end

3
app/admin/work_experiences_admin.rb

@ -1,5 +1,2 @@
Trestle.resource(:work_experiences) do Trestle.resource(:work_experiences) do
menu do
item :work_experiences, icon: "fa fa-user-nurse", priority: 3
end
end end

21
app/models/user.rb

@ -2,4 +2,25 @@ class User < ApplicationRecord
has_many :work_experiences has_many :work_experiences
has_many :skills has_many :skills
has_many :education_entries 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 end

5
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

3
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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| create_table "cover_letters", force: :cascade do |t|
t.string "title" t.string "title"
t.string "company_name" 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 "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "title" t.string "title"
t.string "label"
end end
create_table "work_experiences", force: :cascade do |t| create_table "work_experiences", force: :cascade do |t|

Loading…
Cancel
Save