From 7b7875991fdae656b5c9b3e26c20ebc3915dce02 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Mon, 14 Mar 2022 19:28:40 -0400 Subject: [PATCH] created get_estimate to remove redundant code --- app/controllers/estimate_controller.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/controllers/estimate_controller.rb b/app/controllers/estimate_controller.rb index cfab872..07b9816 100644 --- a/app/controllers/estimate_controller.rb +++ b/app/controllers/estimate_controller.rb @@ -14,16 +14,21 @@ class EstimateController < ApplicationController before_action :require_user, :unless => proc {|c| session[:token].nil? } skip_before_action :verify_authenticity_token, :check_if_login_required, :unless => proc {|c| session[:token].nil? } + + def get_estimate + estimate = Estimate.find_by_id(params[:id]) if params[:id] + estimate = Estimate.find_by_doc_number(params[:search]) if params[:search] + return estimate + end # # Downloads and forwards the estimate pdf # def show - e = Estimate.find_by_id(params[:id]) if params[:id] - e = Estimate.find_by_doc_number(params[:search]) if params[:search] + estimate = get_estimate begin - send_data e.pdf, filename: "estimate #{e.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf" + send_data estimate.pdf, filename: "estimate #{e.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf" rescue redirect_to :back, :flash => { :error => "Estimate not found" } end @@ -33,11 +38,10 @@ class EstimateController < ApplicationController # Downloads estimate by document number # def doc - e = Estimate.find_by_doc_number(params[:id]) if params[:id] - e = Estimate.find_by_doc_number(params[:search]) if params[:search] + estimate = get_estimate begin - send_data e.pdf, filename: "estimate #{e.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf" + send_data estimate.pdf, filename: "estimate #{e.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf" rescue redirect_to :back, :flash => { :error => "Estimate not found" } end