Skip to content

Commit

Permalink
#206 rails_best_practice updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerroelofs committed Mar 27, 2021
1 parent beea651 commit 6e0ec6c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
12 changes: 8 additions & 4 deletions app/controllers/salaries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/salary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions app/views/salaries/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
- 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
%span.input-group-addon $
= 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'

0 comments on commit 6e0ec6c

Please sign in to comment.