Building Instant Approval Functionality for Employees
In this guide we'll walk through how to wire up the confirmation buttons so that employees can instantly confirm their payroll hours.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

To continue from where we left off in the last video, we are now going to change the link of the pending confirmation button from root_path. This is going to be similar to what we did for our approvals, so let's start with the test cases.

Open homepage_spec.rb, and duplicate the previous test case. Change the test name , and create an audit log instead of a post. Then, create a regular user and change the ID of the button. Before that, add an ID to the button. Here, we want the page to have a status of "confirmed." So, the final code is:

# spec/features/homepage_spec.rb

it 'allows the employee to change the audit log status from the homepage' do
  audit_log = FactoryGirl.create(:audit_log)
  user = FactoryGirl.create(:user)
  login_as(user, :scope => :user)

  audit_log.update(user_id: user.id)

  visit root_path

  click_on("confirm_#{audit_log.id}")

  expect(audit_log.reload.status).to eq('confirmed')
end

If you run rspec, it'll fail as expected.

As with the previous implementation, open routes.rb, and add a new member route.

# config/routes.rb

Rails.application.routes.draw do
  resources :audit_logs, except: [:new, :edit, :destory] do
    member do
      get :confirm
    end
  end

Next, open audit_logs_controller.rb and create a new method called confirm. In this method, we have to find the right audit log, authorize it, confirm it, and redirect the user to homepage with a notice that says that the confirmation has been recorded.

# app/controllers/audit_logs_controller.rb

def confirm
  audit_log = AuditLog.find(params[:id])
  authorize audit_log
  audit_log.confirmed!
  redirect_to root_path, notice: "Thank you, your confirmation has been successfully made."
end

Next go to audit_log_policy.rb, and create a method called confirm?.

# app/policies/audit_log_policy.rb

def confirm?
  record.user_id == user.id
end

If you rspec it throws an error. I'm not sure what I missed, so let's look at it in the browser. I found the error. I forgot to change the path of the button. So, go to pending_audit_confirmations.html.erb and change the path from root_path to confirm_audit_log_path(pending_audit_confirmation).

<!-- app/views/static/_pending_audit_confirmations.html.erb -->

<% pending_audit_confirmations.each do |pending_audit_confirmation| %>
  <%= link_to "I confirm that I did not perform any overtime for the week of: #{pending_audit_confirmation.start_date}", confirm_audit_log_path(pending_audit_confirmation), class: 'btn btn-primary btn-block btn-lg', id: "confirm_#{pending_audit_confirmation.id}", data: { confirm: 'Are you sure you want to confirm that you did not perform any overtime?' } %>
<% end %>

Run rspec, and everything is fine.

Now, let's make a small change. If you notice, the confirm button didn't disappear from the homepage even after the use confirmed it. For that, open static_controller.rb and include only those logs that are pending.

# app/controllers/static_controller.rb

class StaticController < ApplicationController
  def homepage
    if admin_types.include?(current_user.type)
      @pending_approvals = Post.submitted
      @recent_audit_items = AuditLog.last(10)
    else
      @pending_audit_confirmations = current_user.audit_logs.pending
    end
  end
end

Resources