Class: HrLite::Admin::LeaveRequestsController

Inherits:
BaseController show all
Defined in:
app/controllers/hr_lite/admin/leave_requests_controller.rb

Instance Method Summary collapse

Instance Method Details

#approveObject



15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/hr_lite/admin/leave_requests_controller.rb', line 15

def approve
  request = LeaveRequest.find(params[:id])
  if request.approve!(actor: hr_current_user, note: params[:decision_note].presence)
    redirect_to admin_leave_requests_path, notice: "Leave approved."
  else
    redirect_to admin_leave_request_path(request),
                alert: "Cannot approve — balance no longer covers this request."
  end
rescue ActiveRecord::RecordInvalid
  redirect_to admin_leave_request_path(request), alert: "Only pending requests can be decided."
end

#indexObject



4
5
6
7
8
# File 'app/controllers/hr_lite/admin/leave_requests_controller.rb', line 4

def index
  scope = LeaveRequest.includes(:leave_type, :user).recent_first
  @status = params[:status].presence_in(LeaveRequest::STATUSES) || "pending"
  @requests = paginate(scope.where(status: @status))
end

#rejectObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/hr_lite/admin/leave_requests_controller.rb', line 27

def reject
  request = LeaveRequest.find(params[:id])
  note = params[:decision_note].to_s.strip
  if note.blank?
    return redirect_to admin_leave_request_path(request), alert: "A note is required to reject."
  end

  request.reject!(actor: hr_current_user, note: note)
  redirect_to admin_leave_requests_path, notice: "Leave rejected."
rescue ActiveRecord::RecordInvalid
  redirect_to admin_leave_request_path(request), alert: "Only pending requests can be decided."
end

#showObject



10
11
12
13
# File 'app/controllers/hr_lite/admin/leave_requests_controller.rb', line 10

def show
  @request = LeaveRequest.includes(:leave_type).find(params[:id])
  @balance = @request.balance
end