Skip to content

Commit

Permalink
Feature/617 daterange picker and advances trainings (#626)
Browse files Browse the repository at this point in the history
* User is able to see all stations and to modify them

* Add bootstrap classes and improve styling

* Use stimulus to hide to fields on butotn clikc

* enable visiblity change by using rails only

* clean up

* allow datepicker to send empty values

* add vogu

* set attr to nil if not in params

* selects has the ability to set value null in db and gets displayed properly

* rework nilify

* display all advanced trainings and suse edit

* records can be updated and errors are shown

* use global error partial

* style advanced tranings

* Render new form on button click

* use haml instead of erb for turbostream

* replace erb files with haml

* commit

* Try to implement create

* fix error with create

* add create funtionnality

* show all input field per default and display correct lablel for button

* clean up daterange picker

* fix sort

* add translations

* rename visibility controller to date-range_controller

* Ahhhhhh finally working \n add button to set same date

* refactor date_picker_controller

* add cancel button

* merge

* refactor

* add styling

* redirect to person for specific advanced_training_features

* optimize tubostream requests

* clean up

* styling

* change erb to haml

* clean up

* add controller tests for AdvancedTrainings

* finally chli mental sanity

* error tests

* fixx tests

* fixx all failing tests

* merge

* try to do magic

* fix tets

* Clean up styles.scss

* Clean up mess

* Clean up gemfile

* clean up

* Resolve errors in tests

* Clean up

* add collection rendering

* use collection renderubg

* clean up translations

* use correct translations

* Resolve erros in advanced_trainings_spec

* Clean up

* clean up

* refactor

* Refactor

* add save and new functionality

* Refactor and add tests

* Don't use legacy hash syntax

* Implement feedback and update tests

* rename new_after_save var

* beautify till_today

---------

Co-authored-by: Yanick Minder <[email protected]>
  • Loading branch information
kcinay055679 and kcinay055679 authored Apr 4, 2024
1 parent 4e14977 commit be93be7
Show file tree
Hide file tree
Showing 45 changed files with 514 additions and 70 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@ pzsh-topbar {
height: 8px;
border-radius: 50%;
background-color: #69b978
}

.bg-hover-gray:hover{
background-color: #e5e5e5;
}
5 changes: 5 additions & 0 deletions app/controllers/advanced_trainings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class AdvancedTrainingsController < PersonRelationsController
self.permitted_attrs = %i[description year_to month_to year_from month_from person_id]
end
2 changes: 1 addition & 1 deletion app/controllers/crud_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ def render_on_success(format, **options)
end

def render_on_unsaved(format, **options)
format.turbo_stream { render options[:render_on_unsaved], status: options[:status] }
format.html { render_or_redirect_on_unsaved(**options) }
format.turbo_stream { render options[:render_on_unsaved], status: options[:status] }
format.json { render_unsaved_json }
end

Expand Down
4 changes: 1 addition & 3 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def show
end

def update
if params.include?('has_nationality2') && false?(params['has_nationality2']['checked'])
params['person']['nationality2'] = nil
end
params[:person][:nationality2] = nil if false?(params[:has_nationality2]&.[](:checked))
super
end

Expand Down
38 changes: 38 additions & 0 deletions app/controllers/person_relations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

class PersonRelationsController < CrudController
def index
redirect_to person_path(person_id)
end

def show
redirect_to person_path(person_id)
end

def new
params[model_identifier] = { person_id: person_id }
super
end

def create
super(location: person_path(person_id)) do |format, success|
if success && params.key?(:render_new_after_save)
format.turbo_stream { render 'new_after_save' }
end
end
end

def update
super(location: person_path(person_id))
end

def destroy
super(location: person_path(person_id)) do |format, success|
format.turbo_stream { render turbo_stream: turbo_stream.remove(entry) } if success
end
end

def person_id
params[:person_id] || model_params[:person_id]
end
end
6 changes: 0 additions & 6 deletions app/helpers/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ def admin?
current_auth_user&.is_admin
end

def generate_select_options_with_default(person)
selected = person ? person.id : ''
prompt = person ? false : true
{ :selected => selected, :prompt => prompt, :disabled => '' }
end

def development?
ENV['DEVELOPMENT'] == 'true' && ENV['RAILS_ENV'] == 'development'
end
Expand Down
30 changes: 30 additions & 0 deletions app/helpers/date_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module DateHelper
def date_range_label(model_with_dates)
now = "#{months[model_with_dates.month_from || 0]} #{model_with_dates.year_from} "
now + date_range_end_label(model_with_dates)
end

def months
t('date.month_names')
end

def date_range_end_label(model_with_dates)
if model_with_dates&.till_today?
return "- #{t('date_range_picker.today')}"
elsif !(model_with_dates.same_year? && model_with_dates.same_month?)
return "- #{months[model_with_dates&.month_to || 0]} #{model_with_dates&.year_to}"
end

''
end

def months_with_nil
months.compact.each_with_index.map { |month, index| [month, index + 1] }
end

def last_100_years
(100.years.ago.year..Time.zone.today.year).to_a.reverse
end
end
13 changes: 13 additions & 0 deletions app/helpers/select_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module SelectHelper
def select_when_availabale(obj)
selected = obj ? obj.id : ''
prompt = obj ? false : true
{ selected: selected, prompt: prompt, disabled: '' }
end

def add_default_option(collection, option = {})
collection.unshift([option[:text], option[:value], option])
end
end
16 changes: 16 additions & 0 deletions app/javascript/controllers/date_picker_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="date-picker"
export default class extends Controller {

static targets = [ "hideable" ]

toggleTargets() {
this.hideableTargets.forEach((el) => {
el.hidden = !el.hidden;
el.querySelectorAll('select').forEach((select) => {
select.value = "";
});
});
}
}
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ application.register("remote-modal", RemoteModalController)
import SkillsFilterController from "./skills_filter_controller"
application.register("skills-filter", SkillsFilterController)

import DatePickerController from "./date_picker_controller"
application.register("date-picker", DatePickerController)

import DropdownLinksController from "./dropdown_controller"
application.register("dropdown", DropdownLinksController)

2 changes: 1 addition & 1 deletion app/javascript/controllers/person_roles_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ export default class extends Controller {
fieldParent.style.display = "none";
}
}
}
}
9 changes: 9 additions & 0 deletions app/models/advanced_training.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class AdvancedTraining < ApplicationRecord
include DaterangeModel

attr_readonly :person_id
after_create :update_associations_updatet_at
after_update :update_associations_updatet_at
after_destroy :update_associations_updatet_at
Expand All @@ -28,6 +29,14 @@ class AdvancedTraining < ApplicationRecord
validates :description, presence: true
validates :description, length: { maximum: 5000 }

scope :list, lambda {
order('
year_to DESC NULLS FIRST,
year_from DESC,
month_to DESC NULLS FIRST,
month_from DESC NULLS FIRST')
}

private

def update_associations_updatet_at
Expand Down
12 changes: 12 additions & 0 deletions app/models/concerns/daterange_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ module DaterangeModel
}
end

def till_today?
year_to.nil? && month_to.nil?
end

def same_year?
year_to == year_from
end

def same_month?
month_from? == month_to
end

private

def start_at_before_finish_at
Expand Down
2 changes: 2 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Person < ApplicationRecord
has_many :skills, through: :people_skills
has_many :roles, through: :person_roles

accepts_nested_attributes_for :advanced_trainings, allow_destroy: true

validates :birthdate, :location, :name, :nationality,
:title, :marital_status, :email, presence: true
validates :location, :name, :title,
Expand Down
8 changes: 8 additions & 0 deletions app/views/advanced_trainings/_advanced_training.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%div.border-top
%turbo-frame{id: "#{dom_id advanced_training}"}
= link_to edit_person_advanced_training_path(advanced_training.person, advanced_training), data:{turbo_prefetch: :false}, class: "text-decoration-none text-dark bg-hover-gray d-block" do
%div.d-flex.row.pt-3.pb-5
%span.col-3.ps-5
= date_range_label advanced_training
%span.col
= advanced_training.description
15 changes: 15 additions & 0 deletions app/views/advanced_trainings/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
= form_with model: ([advanced_training.person, advanced_training]) do |f|
= f.hidden_field :person_id
%div.d-flex.row.py-3.ps-5
%span.col-3
= render partial: 'application/daterange_picker', locals: { form: f, end_date: f.object.till_today? }
%span.col-5.d-flex.flex-column
= t "activerecord.attributes.advanced_training.description"
= f.text_area :description, placeholder: "Description", class: "form-control w-100"
%div.col-3.d-flex.flex-column
= f.button(name: :"save", class:"btn btn-primary", data: { turbo_frame: "advanced_trainings_all"})
- if advanced_training.persisted?
= link_to t("helpers.submit.delete"), person_advanced_training_path(advanced_training.person, advanced_training), data: { turbo_method: :delete, turbo_frame: ["#{dom_id advanced_training}"]}, class: "btn btn-link"
- else
= f.button(t("helpers.submit.save-and-new"), name: :"render_new_after_save", class:"btn btn-link", data: { turbo_frame: "advanced_trainings_all"})
= link_to t("helpers.submit.cancel"), person_path(advanced_training.person), data: { turbo_frame: "#{dom_id advanced_training}"}, method: :get, class: "btn btn-link"
5 changes: 5 additions & 0 deletions app/views/advanced_trainings/_index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%turbo-frame{id: "advanced_trainings_all"}
%div.border.rounded.border
%turbo-frame{id: "#{dom_id AdvancedTraining.new}"}
= render person.advanced_trainings.list

4 changes: 4 additions & 0 deletions app/views/advanced_trainings/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%div
%h1.font-bold.text-2xl.mb-3 Edit Advanced Training
%turbo-frame{id: "#{dom_id @advanced_training}"}
= render partial: "form", locals: {advanced_training: @advanced_training}
3 changes: 3 additions & 0 deletions app/views/advanced_trainings/edit.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= turbo_stream.update "#{dom_id @advanced_training}" do
= render "application/error_banners", errors: @advanced_training.errors
= render "form", advanced_training: @advanced_training
2 changes: 2 additions & 0 deletions app/views/advanced_trainings/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%turbo-frame{id: "#{dom_id AdvancedTraining.new}"}
= render partial: "form", locals: {advanced_training: @advanced_training}
3 changes: 3 additions & 0 deletions app/views/advanced_trainings/new.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= turbo_stream.update "#{dom_id AdvancedTraining.new}" do
= render "application/error_banners", errors: @advanced_training.errors
= render partial: "form", locals: {advanced_training: @advanced_training}
6 changes: 6 additions & 0 deletions app/views/advanced_trainings/new_after_save.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= turbo_stream.replace "advanced_trainings_all" do
= render partial: "index", locals: {person: @advanced_training.person}

= turbo_stream.replace "#{dom_id AdvancedTraining.new}" do
= render partial: "form", locals: {advanced_training: @advanced_training.person.advanced_trainings.new}

37 changes: 37 additions & 0 deletions app/views/application/_daterange_picker.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
%div.d-flex.flex-column{"data-controller": "date-picker"}
%div.d-flex.row.justify-content-end
%span.col-5
%span.ps-3
= t("datetime.prompts.month")
%span.col-5
%span.ps-3
= t("datetime.prompts.year")
%span.col-10
%hr.my-1
%div.d-flex.row.align-items-center
%span.fw-bold.col-2
= "#{t('date_range_picker.from')}:"
%span.col-5
= form.select :month_from, add_default_option(months_with_nil, { text: '-' }), {}, class: "form-select"
%span.col-5
= form.select :year_from, add_default_option(last_100_years, { hidden: true }),{}, class: "form-select"
%div{"data-date-picker-target": "hideable", hidden: end_date, id: "end_date_picker"}
%div.d-flex.row.align-items-center
%span.fw-bold.col-2
= "#{t('date_range_picker.to')}:"
%span.col-5
= form.select :month_to, add_default_option(months_with_nil, { text: '-' }), {}, class: "form-select"
%span.col-5
= form.select :year_to, add_default_option(last_100_years, { hidden: true }), {}, class: "form-select"
%div{"data-date-picker-target": "hideable", hidden: !end_date}
%div.d-flex.justify-content-end
%span.d-flex.justify-content-center.fw-bold.py-2.col-10
= t("date_range_picker.till_today")
%div.d-flex.row.justify-content-end
%span.col-10
%button{"data-action": "date-picker#toggleTargets", class: "btn btn-primary w-100", type: "button"}
%span{"data-date-picker-target": "hideable", hidden: end_date}
= t("date_range_picker.till_today")
%span{"data-date-picker-target": "hideable", hidden: !end_date}
= t("date_range_picker.with_enddate")
5 changes: 5 additions & 0 deletions app/views/application/_error_banners.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- if errors.any?
.alert.alert-danger
%ul.mb-0
- errors.full_messages.each do |error|
%li= error
7 changes: 7 additions & 0 deletions app/views/people/_advanced_trainings.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%div.border.border-secondary-subtle.border-1.d-flex.flex-column
%div.profile-header.mw-100.border-bottom
= "#{t "activerecord.models.advanced_training"} (#{@person.advanced_trainings.count})"
%div.d-flex.flex-column.ms-5.mt-3.mb-3
= link_to "#{t "activerecord.models.advanced_training"} hinzufügen", new_person_advanced_training_path(@person), data: { turbo_frame: "#{dom_id AdvancedTraining.new}"}, method: :get
= render partial: 'advanced_trainings/index', locals: { person: @person }

6 changes: 2 additions & 4 deletions app/views/people/_profile.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,5 @@
%div.border.border-dark-subtle.mt-1.p-2.rounded
- @person.language_skills.each do |language|
%div.mb-1= "#{language.language}: #{language.level} - #{language.certificate}"
= link_to "Show all", people_path, {"data-turbo"=>false}

%div.d-flex.justify-content-end
=link_to image_tag("plus-lg.svg", class: "text-primary")+ "Export", export_cv_person_path(@person), class: "btn text-primary", data: { turbo_frame: "remote_modal" }
= link_to "Show all", people_path, {"data-turbo": false}
= link_to image_tag("plus-lg.svg", class: "text-primary")+ "Export", export_cv_person_path(@person), class: "btn text-primary", data: { turbo_frame: "remote_modal" }
2 changes: 1 addition & 1 deletion app/views/people/_search.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
= form_with do |f|
%section{"data-controller"=>"dropdown"}
= f.collection_select :person_id, Person.all.sort_by(&:name), :id, :name, generate_select_options_with_default(person) , {class: "form-select w-100", "data-action": "change->dropdown#handleChange", "data-value": "/people/"}
= f.collection_select :person_id, Person.all.sort_by(&:name), :id, :name, select_when_availabale(person) , {class: "form-select w-100", "data-action": "change->dropdown#handleChange", "data-value": "/people/"}
12 changes: 0 additions & 12 deletions app/views/people/edit.turbo_stream.erb

This file was deleted.

3 changes: 3 additions & 0 deletions app/views/people/edit.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= turbo_stream.update "#{dom_id @person}" do
= render "form", person: @person
= render "application/error_banners", errors: @person.errors
1 change: 1 addition & 0 deletions app/views/people/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
= render('profile')
= render('core_competences')
= render('advanced_trainings')

5 changes: 0 additions & 5 deletions app/views/skills/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
- if @skill.errors.any?
.alert.alert-danger
%ul.mb-0
- @skill.errors.full_messages.each do |error|
%li= error
= form_with(model: @skill) do |f|
.mb-4.w-100
= f.label :title, class: "form-label"
Expand Down
12 changes: 0 additions & 12 deletions app/views/skills/edit.turbo_stream.erb

This file was deleted.

Loading

0 comments on commit be93be7

Please sign in to comment.