Skip to content

Commit

Permalink
Generalize date range entities (#647)
Browse files Browse the repository at this point in the history
* squash all commits

* merge and bugfix

* clean up

* readd save and new functionality

* refeactor

* refactor new and index and fix failing tests

* refactor

* refactor routes.rb

* Use convention over configuration

* implement feedback

---------

Co-authored-by: Yanick Minder <[email protected]>
  • Loading branch information
kcinay055679 and kcinay055679 authored Apr 15, 2024
1 parent 8a01cff commit 40b4ea3
Show file tree
Hide file tree
Showing 32 changed files with 1,807 additions and 110 deletions.
3 changes: 2 additions & 1 deletion app/controllers/advanced_trainings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

class AdvancedTrainingsController < PersonRelationsController
class AdvancedTrainingsController < People::PersonRelationsController
self.permitted_attrs = %i[description year_to month_to year_from month_from person_id]
self.nesting = Person
end
9 changes: 3 additions & 6 deletions app/controllers/crud_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def create(**options, &block)
#
# Specify a :location option if you wish to do a custom redirect.

# rubocop:disable Metrics/MethodLength
def update(**options, &block)
def update(**options, &block) # rubocop:disable Metrics/MethodLength
model_class.transaction do
if assign_attributes
updated = false
Expand All @@ -94,15 +93,13 @@ def update(**options, &block)
else
updated = with_callbacks(:update, :save) { entry.save }
end

respond(updated,
**options.merge(status: :ok, render_on_unsaved: :edit),
**options.reverse_merge(status: :ok, render_on_unsaved: :edit),
&block)
raise ActiveRecord::Rollback unless updated
end
end
end
# rubocop:enable Metrics/MethodLength

# DELETE /entries/1
# DELETE /entries/1.json
Expand All @@ -120,7 +117,7 @@ def destroy(**options, &block)
model_class.transaction do
destroyed = run_callbacks(:destroy) { entry.destroy }
respond(destroyed,
**options.merge(status: :no_content),
**options.reverse_merge(status: :no_content),
&block)
raise ActiveRecord::Rollback unless destroyed
end
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/dry_crud/generic_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ def path_args(last)
# Get the instance variable named after the +model_class+.
# If the collection variable is required, pass true as the second argument.
def model_ivar_get(plural: false)
name = model_ivar_name(plural: plural)
instance_variable_get(name) if instance_variable_defined?(name)
end

def model_ivar_name(plural: false)
name = ivar_name(model_class)
name = name.pluralize if plural
name = :"@#{name}"
instance_variable_get(name) if instance_variable_defined?(name)
:"@#{name}"
end

# Sets an instance variable with the underscored class name if the given
Expand Down
31 changes: 31 additions & 0 deletions app/controllers/people/person_relations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class People::PersonRelationsController < CrudController

def index
redirect_to person_path(entry.person)
end

def show
redirect_to person_path(entry.person)
end

def create
super do |format, success|
if success && params.key?(:render_new_after_save)
remove_instance_variable(model_ivar_name)
format.turbo_stream { render('save_and_new') }
end
end
end

def update
super(location: person_path(entry.person))
end

def destroy
super do |format, success|
format.turbo_stream { render turbo_stream: turbo_stream.remove(entry) } if success
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class PeopleController < CrudController
self.permitted_attrs = [:birthdate, :location, :marital_status, :updated_by, :name, :nationality,
:nationality2, :title, :competence_notes, :company_id, :email,
:department_id, :shortname, :picture, :picture_cache,
{ person_roles_attributes: [:role_id, :person_role_level_id,
:percent, :id, :_destroy] },
{ person_roles_attributes:
[:role_id, :person_role_level_id, :percent, :id, :_destroy] },
{ language_skills_attributes:
Expand Down
38 changes: 0 additions & 38 deletions app/controllers/person_relations_controller.rb

This file was deleted.

1 change: 1 addition & 0 deletions app/helpers/date_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ def months_with_nil
def last_100_years
(100.years.ago.year..Time.zone.today.year).to_a.reverse
end

end
7 changes: 7 additions & 0 deletions app/helpers/generify_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module GenerifyHelper
def name_of_obj(obj)
obj.model_name.element
end
end
4 changes: 2 additions & 2 deletions app/views/advanced_trainings/_advanced_training.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%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
%turbo-frame{id: dom_id(advanced_training)}
= link_to edit_person_advanced_training_path(advanced_training.person, advanced_training), data:{turbo_prefetch: :false, turbo_frame: dom_id(advanced_training)}, 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
Expand Down
16 changes: 3 additions & 13 deletions app/views/advanced_trainings/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
= 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
= form_with model: ([entry.person, entry]) do |f|
= render('people/person_relations/form', form: f) do
- content_for :input do
= 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: 0 additions & 5 deletions app/views/advanced_trainings/_index.html.haml

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/advanced_trainings/edit.html.haml

This file was deleted.

3 changes: 0 additions & 3 deletions app/views/advanced_trainings/edit.turbo_stream.haml

This file was deleted.

2 changes: 0 additions & 2 deletions app/views/advanced_trainings/new.html.haml

This file was deleted.

3 changes: 0 additions & 3 deletions app/views/advanced_trainings/new.turbo_stream.haml

This file was deleted.

6 changes: 0 additions & 6 deletions app/views/advanced_trainings/new_after_save.turbo_stream.haml

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/application/_daterange_picker.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
%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")
= t("date_range_picker.with_enddate")
7 changes: 0 additions & 7 deletions app/views/people/_advanced_trainings.html.haml

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/people/_cv.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
= render('profile')
= render('core_competences')
= render('advanced_trainings')
= render('people/person_relations/index', list: @person.advanced_trainings)
14 changes: 14 additions & 0 deletions app/views/people/person_relations/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
= yield
= form.hidden_field :person_id
%div.d-flex.row.py-3.ps-5
%span.col-3
= render partial: 'application/daterange_picker', locals: { form: form, end_date: form.object.till_today? }
%span.col-5.d-flex.flex-column
= yield :input
%div.col-3.d-flex.flex-column
= form.button(name: :"save", class:"btn btn-primary")
- if entry.persisted?
= link_to t("helpers.submit.delete"),polymorphic_path([entry.person, entry]), data: { turbo_method: :delete, turbo_frame: dom_id(entry)}, class: "btn btn-link"
- else
= form.button(t("helpers.submit.save-and-new"), name: :"render_new_after_save", class:"btn btn-link", data: { turbo_frame: dom_id(entry.class.new)})
= link_to t("helpers.submit.cancel"), polymorphic_path(entry.person), data: { turbo_frame: dom_id(entry.persisted? ? entry : entry.class.new)}, method: :get, class: "btn btn-link"
9 changes: 9 additions & 0 deletions app/views/people/person_relations/_index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%div.border.border-secondary-subtle.border-1.d-flex.flex-column
%div.profile-header.mw-100.border-bottom
= "#{t "activerecord.models.#{name_of_obj(list)}"} (#{list.count})"
%div.d-flex.flex-column.ms-5.mt-3.mb-3
= link_to "#{t "activerecord.models.#{name_of_obj(list)}"} hinzufügen", new_polymorphic_path([entry, name_of_obj(list).to_sym]), data: { turbo_frame: dom_id(list.model.new)}, method: :get
%div.border.rounded.border
%turbo-frame{id: dom_id(list.model.new)}
%turbo-frame{id: name_of_obj(list)}
= render list
2 changes: 2 additions & 0 deletions app/views/people/person_relations/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%turbo-frame{id: dom_id(entry) }
= render partial: "form"
3 changes: 3 additions & 0 deletions app/views/people/person_relations/edit.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= turbo_stream.update dom_id(entry) do
= render "application/error_banners", errors: entry.errors
= render "form", attribute: entry
2 changes: 2 additions & 0 deletions app/views/people/person_relations/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%turbo-frame{id: dom_id(entry.class.new)}
= render partial: "form"
3 changes: 3 additions & 0 deletions app/views/people/person_relations/new.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= turbo_stream.update dom_id(entry.class.new) do
= render "application/error_banners", errors: entry.errors
= render partial: "form", locals: {attribute: entry}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= turbo_stream.update name_of_obj(entry) do
= render entries

= turbo_stream.update dom_id(entry.class.new) do
= render partial: "form"

17 changes: 17 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,20 @@ de:
till_today: Bis Heute
with_enddate: Mit Enddatum

people-skills:
level:
trainee: Trainee
junior: Junior
professional: Professional
senior: Senior
expert: Expert
interest: Interesse
certificate: Zertifikat
core-competence: Kernkompetenz
date_range_picker:
from: Von
to: Bis
today: Heute
till_today: Bis Heute
with_enddate: Mit Enddatum

15 changes: 15 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ en:
from: From
to: To
today: Today

people-skills:
level:
trainee: Trainee
junior: Junior
professional: Professional
senior: Senior
expert: Expert
interest: Interest
certificate: Certificate
core-competence: Core Competence
date_range_picker:
from: From
to: To
today: Today
12 changes: 3 additions & 9 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@
delete 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_auth_user_session
end

resources :people do
resources :advanced_trainings
end

resources :skills
resources :people_skills


# Status
scope 'status' do
get 'health', to: 'status#health'
get 'readiness', to: 'status#readiness'
end

resources :people_skills

resources :people do
resources :advanced_trainings
member do
get 'export-cv', to: 'people/export_cv#show'
put 'picture', to: 'people/picture#update'
Expand All @@ -38,7 +33,6 @@
get 'people-skills-edit', to: 'people/people_skills#edit'
patch 'people-skills', to: 'people/people_skills#update'
end

end

resources :skills do
Expand Down
Loading

0 comments on commit 40b4ea3

Please sign in to comment.