diff --git a/app/controllers/admin/university/people/alumni_controller.rb b/app/controllers/admin/university/people/alumni_controller.rb index ba34e709c7373c86adaec9ac7aad5e6090420405..5a06332f2ab3fa85af886510d8f49a8e73e4b861 100644 --- a/app/controllers/admin/university/people/alumni_controller.rb +++ b/app/controllers/admin/university/people/alumni_controller.rb @@ -24,15 +24,14 @@ class Admin::University::People::AlumniController < Admin::University::Applicati end def update_cohorts - # TODO - # if @alumnus.update(alumnus_params) - # redirect_to [:admin, @alumnus], - # notice: t('admin.successfully_updated_html', model: @alumnus.to_s) - # else - # render :edit - # breadcrumb - # add_breadcrumb t('edit') - # end + if @alumnus.update(alumnus_params) + redirect_to admin_university_people_alumnus_path(@alumnus), + notice: t('admin.successfully_updated_html', model: @alumnus.to_s) + else + render :edit_cohorts + breadcrumb + add_breadcrumb t('edit') + end end protected @@ -45,8 +44,7 @@ class Admin::University::People::AlumniController < Admin::University::Applicati end def alumnus_params - # TODO: Permit nested attrs - params.require(:university_person_alumnus) - .permit() + params.require(:university_person) + .permit(cohorts_attributes: [:id, :program_id, :university_id, :year, :_destroy]) end end diff --git a/app/models/education/academic_year.rb b/app/models/education/academic_year.rb index 1002b03ca63e9d986888824cad6626c982ea0971..47588caa3e3ad7fcdaf627bb3f8b669ec637de8e 100644 --- a/app/models/education/academic_year.rb +++ b/app/models/education/academic_year.rb @@ -33,6 +33,8 @@ class Education::AcademicYear < ApplicationRecord class_name: 'University::Person', through: :cohorts + validates :year, numericality: { only_integer: true, greater_than: 0 } + scope :ordered, -> { order(year: :desc) } def cohorts_in_context(context) diff --git a/app/models/education/cohort.rb b/app/models/education/cohort.rb index b513e77a815caf8378d5b7c9ddd59dd5331441d8..a60997c58b75764a579e99c655c531d6cb0817a7 100644 --- a/app/models/education/cohort.rb +++ b/app/models/education/cohort.rb @@ -27,17 +27,21 @@ class Education::Cohort < ApplicationRecord belongs_to :program, class_name: 'Education::Program' - alias_attribute :education_program, :program + alias_attribute :education_program, :program belongs_to :academic_year, class_name: 'Education::AcademicYear' - alias_attribute :education_academic_year, :academic_year + alias_attribute :education_academic_year, :academic_year has_and_belongs_to_many :people, class_name: 'University::Person', foreign_key: 'education_cohort_id', association_foreign_key: 'university_person_id' + validates_associated :academic_year, :program + validates :year, presence: true + before_validation :set_university_id, on: :create + scope :ordered, -> { includes(:academic_year).order('education_academic_years.year DESC') } @@ -45,4 +49,18 @@ class Education::Cohort < ApplicationRecord def to_s "#{program} #{academic_year} #{name}" end + + def year + academic_year&.year + end + + def year=(val) + self.academic_year = Education::AcademicYear.where(university_id: university_id, year: val).first_or_create + end + + private + + def set_university_id + self.university_id = self.program.university_id + end end diff --git a/app/models/university/person/with_education.rb b/app/models/university/person/with_education.rb index a42f499dbab3d54be47ae8b2894201cf5c90977a..3c36e95e988f1114e4ac641a6da2d52d09ae4fe8 100644 --- a/app/models/university/person/with_education.rb +++ b/app/models/university/person/with_education.rb @@ -21,7 +21,7 @@ module University::Person::WithEducation has_many :experiences has_and_belongs_to_many :cohorts, - class_name: 'Education::Cohort', + class_name: '::Education::Cohort', foreign_key: 'university_person_id', association_foreign_key: 'education_cohort_id' diff --git a/app/views/admin/university/people/alumni/_cohort_fields.html.erb b/app/views/admin/university/people/alumni/_cohort_fields.html.erb index bf6e5f83490f73d5864f5f755f2ff14ad497483d..632f02976117be7297d2e905c93f94dd629e4981 100644 --- a/app/views/admin/university/people/alumni/_cohort_fields.html.erb +++ b/app/views/admin/university/people/alumni/_cohort_fields.html.erb @@ -4,6 +4,10 @@ <div class="card-body"> <div class="row"> <div class="col-md-5"> + <%= f.input :university_id, + as: :hidden, + input_html: { value: current_university.id }, + wrapper: false %> <%= f.association :program, collection: collection_tree(current_university.education_programs), label_method: ->(p) { sanitize p[:label] }, @@ -14,10 +18,12 @@ wrapper: false %> </div> <div class="col-md-6"> - <%#= f.input :description, - as: :string, + <%= f.input :year, + as: :select, + collection: ((Time.now.year + 5).downto(1950)), label: false, - placeholder: University::Person::Involvement.human_attribute_name('description'), + include_blank: t('simple_form.include_blanks.defaults.year'), + required: true, wrapper: false %> </div> <div class="col-md-1 text-end"> diff --git a/app/views/admin/university/people/alumni/edit_cohorts.html.erb b/app/views/admin/university/people/alumni/edit_cohorts.html.erb index c129197dc159ad92734910be48e8083a867a2a26..2bc79d96d2f87afb3619c6506ab3e328bbe74777 100644 --- a/app/views/admin/university/people/alumni/edit_cohorts.html.erb +++ b/app/views/admin/university/people/alumni/edit_cohorts.html.erb @@ -2,7 +2,7 @@ <h2 class="h3"><%= Education::Program.model_name.human(count: 2) %></h2> -<%= simple_form_for [:admin, @alumnus], url: edit_cohorts_admin_university_people_alumni_path(@alumnus) do |f| %> +<%= simple_form_for [:admin, @alumnus], url: edit_cohorts_admin_university_people_alumnus_path(@alumnus) do |f| %> <%= f.error_notification %> <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> @@ -20,7 +20,7 @@ <div class="col-md-10"> <div id="cohorts"> - <%= f.simple_fields_for :cohorts, @alumnus.cohorts.ordered, include_id: false do |cohort_f| %> + <%= f.simple_fields_for :cohorts, @alumnus.cohorts.sort_by { |c| [c.year ? 0 : 1, c.year] }.reverse, include_id: false do |cohort_f| %> <%= render 'admin/university/people/alumni/cohort_fields', f: cohort_f, include_id: true %> <% end %> </div> diff --git a/app/views/admin/university/people/alumni/show.html.erb b/app/views/admin/university/people/alumni/show.html.erb index 45ee95df477410c48821ce448021b70486c327fd..ca7335644c78d9925ae4c87e2adfd727f7ad2141 100644 --- a/app/views/admin/university/people/alumni/show.html.erb +++ b/app/views/admin/university/people/alumni/show.html.erb @@ -2,16 +2,20 @@ <%= render 'admin/university/people/main_infos', person: @alumnus %> -<%= render 'admin/university/people/alumni/show/cohorts', cohorts: @cohorts, param_name: 'cohorts_page' %> - -<%# TODO: remove %> <% if @alumnus.cohorts.any? %> -<h2><%= Education::Cohort.model_name.human(count: @alumnus.cohorts.count) %></h2> -<% @alumnus.cohorts.each do |cohort| %> - <%= link_to cohort, [:admin, cohort] %> -<% end %> + <div class="card flex-fill w-100"> + <div class="card-header"> + <h2 class="card-title mb-0 h5"><%= Education::Cohort.model_name.human(count: @alumnus.cohorts.count) %></h2> + </div> + <div class="card-body"> + <ul class="list-unstyled"> + <% @alumnus.cohorts.ordered.each do |cohort| %> + <li><%= link_to cohort, [:admin, cohort] %></li> + <% end %> + </ul> + </div> + </div> <% end %> -<%# end TODO %> <% content_for :action_bar_right do %> diff --git a/app/views/admin/university/people/alumni/show/_cohorts.html.erb b/app/views/admin/university/people/alumni/show/_cohorts.html.erb deleted file mode 100644 index a85f73d8abd631a2d1f123591a450c97508de44c..0000000000000000000000000000000000000000 --- a/app/views/admin/university/people/alumni/show/_cohorts.html.erb +++ /dev/null @@ -1,48 +0,0 @@ -<% param_name ||= :page %> - -<% if cohorts.total_count > 0 %> - <div class="card"> - <div class="card-header"> - <h2 class="card-title mb-0 h5"><%= "#{Education::Cohort.model_name.human(count: 2)} (#{cohorts.total_count})" %></h2> - </div> - <table class="table"> - <thead> - <tr> - <th><%= Education::Cohort.human_attribute_name('program') %></th> - <th><%= Education::Cohort.human_attribute_name('academic_year') %></th> - <th></th> - </tr> - </thead> - <tbody> - <% cohorts.each do |cohort| %> - <% program = cohort.program %> - <tr> - <td><%= link_to_if can?(:read, program), program, [:admin, program] %></td> - <td><%= program.level_i18n %></td> - <td><%= cohort.academic_year %></td> - <td class="text-end"> - <div class="btn-group" role="group"> - <%= link_to t('edit'), - '#', - class: button_classes if can?(:update, cohort) %> - <%= link_to t('delete'), - '#', - method: :delete, - data: { confirm: t('please_confirm') }, - class: button_classes_danger if can?(:destroy, cohort) %> - </div> - </td> - </tr> - <% end %> - </tbody> - </table> - - <% if cohorts.total_pages > 1 %> - <div class="card-footer"> - <%= paginate cohorts, theme: 'bootstrap-5', param_name: param_name %> - </div> - <% end %> - - </div> - -<% end %> diff --git a/config/locales/university/en.yml b/config/locales/university/en.yml index c453abf00418bc7e77e019f9085367b8a6489e92..6de579a94b3757e9ee037e4de09bc694fe85c1ad 100644 --- a/config/locales/university/en.yml +++ b/config/locales/university/en.yml @@ -129,6 +129,9 @@ en: linkedin: "Example: https://www.linkedin.com/in/osuny" tenure: "To differ from temporary lecturer." twitter: "Example: osuny" + include_blanks: + defaults: + year: Select year enums: university: organization: diff --git a/config/locales/university/fr.yml b/config/locales/university/fr.yml index 00d4d9d976dc08d43ea41a3fc9b10f81d79827a8..af72d52d27263c1fea9d70bd08509b8f68cb6ebc 100644 --- a/config/locales/university/fr.yml +++ b/config/locales/university/fr.yml @@ -129,6 +129,9 @@ fr: linkedin: "Exemple : https://www.linkedin.com/in/osuny" tenure: "À différencier d'une personne vacataire." twitter: "Exemple : osuny" + include_blanks: + defaults: + year: Sélectionnez une année enums: university: organization: