11class JobApplicationsController < ApplicationController
2- before_action :set_job_application , only : [ :create , :update , :destroy ]
2+ include ActionView ::RecordIdentifier
3+ before_action :set_job_application , only : [ :edit , :update , :destroy ]
34
45 def index
56 @job_applications = filter_and_sort_job_applications
@@ -22,53 +23,61 @@ def index
2223
2324 def new
2425 @job_application = JobApplication . new
26+ respond_to do |format |
27+ format . html
28+ format . turbo_stream { render turbo_stream : turbo_stream . replace ( "new_job_application" , partial : "form" , locals : { job_application : @job_application , title : "New" } ) }
29+ end
30+ end
31+
32+ def edit
33+ respond_to do |format |
34+ format . html
35+ format . turbo_stream { render turbo_stream : turbo_stream . replace ( dom_id ( @job_application ) , partial : "form" , locals : { job_application : @job_application , title : "Edit" } ) }
36+ end
2537 end
2638
2739 def create
2840 @job_application = JobApplication . new ( job_application_params )
2941
3042 respond_to do |format |
3143 if @job_application . save
32- format . html { redirect_to job_applications_path , notice : "Job application was successfully created." }
33- format . turbo_stream {
34- flash . now [ :notice ] = "Job application was successfully created."
35- render turbo_stream : [
36- turbo_stream . prepend ( "job_applications" , partial : "job_application" , locals : { job_application : @job_application } ) ,
37- turbo_stream . update ( "flash_messages" , partial : "flash_messages" ) ,
38- turbo_stream . replace ( "new_job_application" , partial : "form" , locals : { job_application : JobApplication . new } )
39- ]
40- }
44+ format . html { redirect_to root_path , success : "Job application was successfully created." }
45+ format . turbo_stream do
46+ render turbo_stream : turbo_stream . redirect_advanced ( root_path )
47+ flash [ :success ] = "Job application was successfully created."
48+ end
4149 else
4250 format . html { render :new , status : :unprocessable_entity }
43- format . turbo_stream { render turbo_stream : turbo_stream . replace ( @job_application , partial : "form" , locals : { job_application : @job_application } ) }
51+ format . turbo_stream do
52+ render turbo_stream : [
53+ turbo_stream . replace ( "new_job_application" , partial : "form" , locals : { job_application : @job_application , title : "New" } ) ,
54+ turbo_stream . update ( "flash_messages" , partial : "flash_messages" )
55+ ]
56+ end
4457 end
4558 end
4659 end
4760
4861 def update
4962 respond_to do |format |
5063 if @job_application . update ( job_application_params )
51- format . html { redirect_to job_applications_path , notice : "Job application was successfully updated." }
52- format . turbo_stream {
53- flash . now [ :notice ] = "Job application was successfully updated."
54- render turbo_stream : [
55- turbo_stream . replace ( @job_application , partial : "job_application" , locals : { job_application : @job_application } ) ,
56- turbo_stream . update ( "flash_messages" , partial : "flash_messages" )
57- ]
58- }
64+ format . html { redirect_to root_path , success : "Job application was successfully updated." }
65+ format . turbo_stream do
66+ render turbo_stream : turbo_stream . redirect_advanced ( root_path )
67+ flash [ :success ] = "Job application was successfully updated."
68+ end
5969 else
6070 format . html { render :edit , status : :unprocessable_entity }
61- format . turbo_stream { render turbo_stream : turbo_stream . replace ( @job_application , partial : "form" , locals : { job_application : @job_application } ) }
6271 end
6372 end
6473 end
6574
6675 def destroy
6776 @job_application . destroy
6877 respond_to do |format |
69- format . html { redirect_to job_applications_url , notice : "Job application was successfully deleted." }
78+ format . html { redirect_to root_path , success : "Job application was successfully deleted." }
7079 format . turbo_stream {
71- flash . now [ :notice ] = "Job application was successfully deleted."
80+ flash . now [ :success ] = "Job application was successfully deleted."
7281 render turbo_stream : [
7382 turbo_stream . remove ( @job_application ) ,
7483 turbo_stream . update ( "job_application_count" , JobApplication . count ) ,
@@ -82,6 +91,8 @@ def destroy
8291
8392 def set_job_application
8493 @job_application = JobApplication . find ( params [ :id ] )
94+ rescue ActiveRecord ::RecordNotFound
95+ redirect_to root_path , alert : "Job application not found."
8596 end
8697
8798 def job_application_params
@@ -97,11 +108,18 @@ def filter_and_sort_job_applications
97108
98109 sort_column = sort_column ( params [ :sort ] )
99110 sort_direction = sort_direction ( params [ :direction ] )
100- job_applications . order ( sort_column => sort_direction )
111+
112+ if sort_column == "created_at" || params [ :sort ] . blank?
113+ # If sorting by created_at or no sorting specified, always use desc order
114+ job_applications . order ( created_at : :desc )
115+ else
116+ # For other columns, use the specified sort direction
117+ job_applications . order ( sort_column => sort_direction )
118+ end
101119 end
102120
103121 def sort_column ( column )
104- %w[ date_applied company_name position_title ] . include? ( column ) ? column : "date_applied "
122+ %w[ date_applied company_name position_title created_at ] . include? ( column ) ? column : "created_at "
105123 end
106124
107125 def sort_direction ( direction )
0 commit comments