From 6e0ec6cb654f68d500cd9fcea0eb875fe0db8d8f Mon Sep 17 00:00:00 2001 From: Roger Roelofs Date: Sat, 27 Mar 2021 10:47:16 -0400 Subject: [PATCH] #206 rails_best_practice updates --- app/controllers/salaries_controller.rb | 12 ++++++++---- app/models/salary.rb | 1 + app/views/salaries/_form.html.haml | 10 +++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/controllers/salaries_controller.rb b/app/controllers/salaries_controller.rb index 5652b371..c13b8633 100644 --- a/app/controllers/salaries_controller.rb +++ b/app/controllers/salaries_controller.rb @@ -12,8 +12,7 @@ def new end def create - update_params = salary_params - update_params["start_date"] = @employee.tenures.last.start_date unless update_params.dig(:start_date) + update_params = fill_start_date(@employee.tenures.last.start_date) @salary = @employee.tenures.last.salaries.new(update_params) if @salary.save redirect_to employee_path(@employee), notice: 'Successfully recorded raise' @@ -25,8 +24,7 @@ def create def edit; end def update - update_params = salary_params - update_params["start_date"] = @salary.tenure.start_date unless update_params.dig(:start_date) + update_params = fill_start_date(@salary.tenure_start_date) if @salary.update(update_params) redirect_to employee_path(@salary.employee), notice: 'Successfully upddated salary' else @@ -53,6 +51,12 @@ def last_tenure @employee.tenures.last end + def fill_start_date(date) + update_params = salary_params + update_params[:start_date] = date unless update_params.dig(:start_date) + update_params + end + def salary_params params.require(:salary).permit(:start_date, :annual_amount) end diff --git a/app/models/salary.rb b/app/models/salary.rb index 7c310ac9..85edd220 100644 --- a/app/models/salary.rb +++ b/app/models/salary.rb @@ -9,6 +9,7 @@ class Salary < ActiveRecord::Base validate :no_salaries_outside_tenure_dates, if: :tenure delegate :first_name, :last_name, to: :employee, prefix: true + delegate :start_date, to: :tenure, prefix: true before_validation :ensure_start_date diff --git a/app/views/salaries/_form.html.haml b/app/views/salaries/_form.html.haml index 07f0315b..e41d1bae 100644 --- a/app/views/salaries/_form.html.haml +++ b/app/views/salaries/_form.html.haml @@ -1,15 +1,15 @@ -- if @salary.errors.any? +- if salary.errors.any? .alert.alert-danger - %p #{pluralize(@salary.errors.count, "error")} prohibited this raise from being saved: + %p #{pluralize(salary.errors.count, "error")} prohibited this raise from being saved: %ul - - @salary.errors.full_messages.each do |msg| + - salary.errors.full_messages.each do |msg| %li= msg .col-lg-9 .well.bs-component %fieldset .form-group.col-lg-12 = f.label :start_date, class: 'control-label' - = f.date_field :start_date, class: 'form-control', required: true, autofocus: @salary.start_date != @salary.tenure.start_date, disabled: (@salary.start_date == @salary.tenure.start_date) + = f.date_field :start_date, class: 'form-control', required: true, autofocus: salary.start_date != salary.tenure_start_date, disabled: (salary.start_date == salary.tenure_start_date) .form-group.col-lg-12 = f.label :annual_amount, class: 'control-label' .input-group @@ -17,4 +17,4 @@ = f.number_field :annual_amount, class: 'form-control', required: true .form-group.col-lg-12 = f.submit 'Save', class: 'btn btn-primary' - = link_to 'Cancel', employee_path(@salary.employee), class: 'btn btn-default' + = link_to 'Cancel', employee_path(salary.employee), class: 'btn btn-default'