- Read Tutorial
- Watch Guide Video
In this video, we are going to update the audit log status when an overtime is rejected.
Let's say we have an employee who entered an overtime of 30 hours, and this caused the confirm button on homepage to disappear, which is good. Next, the manager feels this is too much and rejects the overtime. In that case, our app should put the confirm button back on the employee's homepage because he or she doesn't really have an overtime for that week. They shouldn't be able to edit the rejected entry, rather they should create another one from scratch. This is what we're going to work on.
Open post.rb
, and if you see, the update_audit_log
method only updates the status of the audit log to confirmed. It doesn't do anything when an entry is rejected. So, we're going to change this. We'll rename the method to confirm_audit_log
, and we'll ask the callback to call this method only if the status is submitted.
We'll create another method called un_confirm_audit_log
, and this will be called when an entry is rejected.
In the un_confirm_audit_log
method, we'll find the audit log and change its status to rejected.
# app/models/post.rb after_save :confirm_audit_log, if: :submitted? after_save :un_confirm_audit_log, if: :rejected? private def confirm_audit_log audit_log = AuditLog.where(user_id: self.user_id, start_date: (self.date - 7.days..self.date)).last audit_log.confirmed! if audit_log end def un_confirm_audit_log audit_log = AuditLog.where(user_id: self.user_id, start_date: (self.date - 7.days..self.date)).last audit_log.pending! if audit_log end
We obviously need to refactor this because there's lot of duplicate code. For now though, let's check if it's working. Start the rails server and open your browser. Log in as admin and reject a time entry, Now, log in as an employee and the confirm button for that week should be present on the homepage. That works!
Run rspec
just to make sure everything is working fine. It does!