From 91a58d3c7aaa5a0a2a462cb5716322a766cad7ac Mon Sep 17 00:00:00 2001 From: pabois <pierreandre.boissinot@noesya.coop> Date: Mon, 3 Oct 2022 17:31:38 +0200 Subject: [PATCH] almuni connected to schools --- .../admin/university/alumni/cohorts_controller.rb | 2 +- app/models/education/cohort.rb | 11 +++++++++-- app/models/university/person/with_education.rb | 2 +- app/views/admin/education/cohorts/_list.html.erb | 4 +++- app/views/admin/education/cohorts/show.html.erb | 2 ++ .../admin/education/programs/show/_cohorts.html.erb | 2 ++ .../alumni/cohorts/_cohort_fields.html.erb | 12 ++++++++++-- app/views/admin/university/alumni/show.html.erb | 12 +++--------- config/locales/education/en.yml | 1 + config/locales/education/fr.yml | 1 + config/locales/university/en.yml | 3 +++ config/locales/university/fr.yml | 1 + db/migrate/20221003144016_add_school_to_cohorts.rb | 10 ++++++++++ db/schema.rb | 5 ++++- test/fixtures/education/cohorts.yml | 3 +++ test/models/education/cohort_test.rb | 3 +++ 16 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 db/migrate/20221003144016_add_school_to_cohorts.rb diff --git a/app/controllers/admin/university/alumni/cohorts_controller.rb b/app/controllers/admin/university/alumni/cohorts_controller.rb index c3a285dd9..9af2f787c 100644 --- a/app/controllers/admin/university/alumni/cohorts_controller.rb +++ b/app/controllers/admin/university/alumni/cohorts_controller.rb @@ -30,7 +30,7 @@ class Admin::University::Alumni::CohortsController < Admin::University::Applicat def cohorts_params params.require(:university_person) - .permit(cohorts_attributes: [:id, :program_id, :year, :_destroy]) + .permit(cohorts_attributes: [:id, :school_id, :program_id, :year, :_destroy]) .merge(university_id: current_university.id) .tap { |permitted_params| permitted_params[:cohorts_attributes].transform_values! do |hash| diff --git a/app/models/education/cohort.rb b/app/models/education/cohort.rb index fff2ec382..c086cf0d3 100644 --- a/app/models/education/cohort.rb +++ b/app/models/education/cohort.rb @@ -8,23 +8,30 @@ # updated_at :datetime not null # academic_year_id :uuid not null, indexed # program_id :uuid not null, indexed +# school_id :uuid not null, indexed # university_id :uuid not null, indexed # # Indexes # # index_education_cohorts_on_academic_year_id (academic_year_id) # index_education_cohorts_on_program_id (program_id) +# index_education_cohorts_on_school_id (school_id) # index_education_cohorts_on_university_id (university_id) # # Foreign Keys # # fk_rails_0f4a4f43d9 (university_id => universities.id) # fk_rails_72528c3d76 (program_id => education_programs.id) +# fk_rails_8545767e2d (school_id => education_schools.id) # fk_rails_c2d725cabd (academic_year_id => education_academic_years.id) # class Education::Cohort < ApplicationRecord include WithUniversity + belongs_to :school, + class_name: 'Education::School' + alias_attribute :education_school, :school + belongs_to :program, class_name: 'Education::Program' alias_attribute :education_program, :program @@ -38,7 +45,7 @@ class Education::Cohort < ApplicationRecord foreign_key: 'education_cohort_id', association_foreign_key: 'university_person_id' - validates_associated :academic_year, :program + validates_associated :school, :academic_year, :program validates :year, presence: true scope :ordered, -> { @@ -46,7 +53,7 @@ class Education::Cohort < ApplicationRecord } def to_s - "#{program} #{academic_year} #{name}" + "#{school} #{program} #{academic_year}" end def year diff --git a/app/models/university/person/with_education.rb b/app/models/university/person/with_education.rb index 15a7e3682..55e94ad13 100644 --- a/app/models/university/person/with_education.rb +++ b/app/models/university/person/with_education.rb @@ -67,7 +67,7 @@ module University::Person::WithEducation cohorts_ids = [] self.cohorts.map do |object| academic_year = Education::AcademicYear.where(university_id: university_id, year: object.year).first_or_create - cohort = Education::Cohort.where(university_id: university_id, program_id: object.program_id, academic_year_id: academic_year.id).first_or_initialize + cohort = Education::Cohort.where(university_id: university_id, school_id: object.school_id, program_id: object.program_id, academic_year_id: academic_year.id).first_or_initialize return unless cohort.valid? cohort.save if cohort.new_record? unless cohorts_ids.include?(cohort.reload.id) || object._destroy diff --git a/app/views/admin/education/cohorts/_list.html.erb b/app/views/admin/education/cohorts/_list.html.erb index 5d5ed64b2..9f593bd74 100644 --- a/app/views/admin/education/cohorts/_list.html.erb +++ b/app/views/admin/education/cohorts/_list.html.erb @@ -2,6 +2,7 @@ <thead> <tr> <th><%= Education::Cohort.human_attribute_name('year') %></th> + <th><%= Education::Cohort.human_attribute_name('school') %></th> <th><%= Education::Cohort.human_attribute_name('program') %></th> <th><%= Education::Cohort.human_attribute_name('alumni') %></th> </tr> @@ -15,7 +16,8 @@ <%= cohort.name %> <% end %> </td> - <td><%= link_to cohort.program, [:admin, cohort.program] %></td> + <td><%= link_to_if can?(:read, cohort.school), cohort.school, [:admin, cohort.school] %></td> + <td><%= link_to_if can?(:read, cohort.program), cohort.program, [:admin, cohort.program] %></td> <td><%= cohort.people.count %></td> </tr> <% end %> diff --git a/app/views/admin/education/cohorts/show.html.erb b/app/views/admin/education/cohorts/show.html.erb index 9453d4e8e..e6597d907 100644 --- a/app/views/admin/education/cohorts/show.html.erb +++ b/app/views/admin/education/cohorts/show.html.erb @@ -4,6 +4,8 @@ <div class="col-md-8"> <div class="card flex-fill w-100"> <div class="card-body"> + <h3 class="h5"><%= Education::Cohort.human_attribute_name('school') %></h3> + <p><%= link_to_if can?(:read, @cohort.school), @cohort.school, [:admin, @cohort.school] %></p> <h3 class="h5"><%= Education::Cohort.human_attribute_name('program') %></h3> <p><%= link_to_if can?(:read, @cohort.program), @cohort.program, [:admin, @cohort.program] %></p> <h3 class="h5"><%= Education::Cohort.human_attribute_name('year') %></h3> diff --git a/app/views/admin/education/programs/show/_cohorts.html.erb b/app/views/admin/education/programs/show/_cohorts.html.erb index 4e37a2cbb..e954c2e8a 100644 --- a/app/views/admin/education/programs/show/_cohorts.html.erb +++ b/app/views/admin/education/programs/show/_cohorts.html.erb @@ -11,6 +11,7 @@ <thead> <tr> <th><%= Education::Cohort.human_attribute_name('year') %></th> + <th><%= Education::Cohort.human_attribute_name('school') %></th> <th><%= Education::Cohort.human_attribute_name('alumni') %></th> </tr> </thead> @@ -23,6 +24,7 @@ <%= cohort.name %> <% end %> </td> + <td><%= link_to_if can?(:read, cohort.school), cohort.school, [:admin, cohort.school] %></td> <td><%= cohort.people.count %></td> </tr> <% end %> diff --git a/app/views/admin/university/alumni/cohorts/_cohort_fields.html.erb b/app/views/admin/university/alumni/cohorts/_cohort_fields.html.erb index 5ebeac215..b66b73ca7 100644 --- a/app/views/admin/university/alumni/cohorts/_cohort_fields.html.erb +++ b/app/views/admin/university/alumni/cohorts/_cohort_fields.html.erb @@ -3,7 +3,15 @@ <div class="card mb-3"> <div class="card-body"> <div class="row"> - <div class="col-md-5"> + <div class="col-md-3"> + <%= f.association :school, + collection: current_university.education_schools.ordered, + label: false, + include_blank: t('simple_form.include_blanks.defaults.school'), + required: true, + wrapper: false %> + </div> + <div class="col-md-4"> <%= f.association :program, collection: collection_tree(current_university.education_programs), label_method: ->(p) { sanitize p[:label] }, @@ -13,7 +21,7 @@ required: true, wrapper: false %> </div> - <div class="col-md-6"> + <div class="col-md-2"> <%= f.input :year, as: :select, collection: ((Time.now.year + 5).downto(1950)), diff --git a/app/views/admin/university/alumni/show.html.erb b/app/views/admin/university/alumni/show.html.erb index b87024b41..7bce04550 100644 --- a/app/views/admin/university/alumni/show.html.erb +++ b/app/views/admin/university/alumni/show.html.erb @@ -7,13 +7,7 @@ <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> + <%= render 'admin/education/cohorts/list', cohorts: @alumnus.cohorts.ordered %> </div> <% end %> @@ -49,9 +43,9 @@ <% content_for :action_bar_right do %> <%= edit_link @alumnus %> - <%= link_to t('university.manage_cohorts'), + <%= link_to t('university.manage_cohorts'), cohorts_admin_university_alumnus_path(@alumnus), class: button_classes if can?(:update, @alumnus) %> - <%= link_to t('university.manage_experiences'), experiences_admin_university_alumnus_path(@alumnus), + <%= link_to t('university.manage_experiences'), experiences_admin_university_alumnus_path(@alumnus), class: button_classes if can?(:update, @alumnus) %> <% end %> diff --git a/config/locales/education/en.yml b/config/locales/education/en.yml index c3fc9d2a5..a9652bec1 100644 --- a/config/locales/education/en.yml +++ b/config/locales/education/en.yml @@ -25,6 +25,7 @@ en: education/cohort: alumni: Alumni program: Program + school: School year: Year education/diploma: name: Name diff --git a/config/locales/education/fr.yml b/config/locales/education/fr.yml index 88f83eede..777c44f83 100644 --- a/config/locales/education/fr.yml +++ b/config/locales/education/fr.yml @@ -25,6 +25,7 @@ fr: education/cohort: alumni: Alumni program: Formation + school: École year: Année education/diploma: name: Nom diff --git a/config/locales/university/en.yml b/config/locales/university/en.yml index cd6cb5d0f..9b3476225 100644 --- a/config/locales/university/en.yml +++ b/config/locales/university/en.yml @@ -70,6 +70,7 @@ en: zipcode: Zipcode university/person/experience: description: Description + edit: Edit experience organization: Organization from_year: Start year to_year: End year @@ -150,6 +151,7 @@ en: include_blanks: defaults: organization: Select organization + school: Select school year: Select year enums: university: @@ -174,6 +176,7 @@ en: manage_cohorts: Manage cohorts manage_experiences: Manage experiences organisation: + employees: Employees import_hint_html: "Possible values for <i>kind</i> are: company, non_profit, government.<br><i>Siren</i>, <i>nic</i>, <i>zipcode</i> and <i>phone</i> fields must have a text format, not numbers.<br><i>Country</i> field must contain the ISO 3166 code of the country, so 2 upcase characters (<a href=\"https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes\" target=\_blank\">list</a>)." person: administrator_roles: Administrator roles diff --git a/config/locales/university/fr.yml b/config/locales/university/fr.yml index aba7fa598..ddb61535f 100644 --- a/config/locales/university/fr.yml +++ b/config/locales/university/fr.yml @@ -151,6 +151,7 @@ fr: include_blanks: defaults: organization: Sélectionnez une organisation + school: Sélectionnez une école year: Sélectionnez une année enums: university: diff --git a/db/migrate/20221003144016_add_school_to_cohorts.rb b/db/migrate/20221003144016_add_school_to_cohorts.rb new file mode 100644 index 000000000..251b13f69 --- /dev/null +++ b/db/migrate/20221003144016_add_school_to_cohorts.rb @@ -0,0 +1,10 @@ +class AddSchoolToCohorts < ActiveRecord::Migration[6.1] + def change + add_reference :education_cohorts, :school, foreign_key: {to_table: :education_schools}, type: :uuid + Education::Cohort.all.each do |cohort| + cohort.school_id = cohort.program.schools.first.id + cohort.save + end + change_column_null :education_cohorts, :school_id, false + end +end diff --git a/db/schema.rb b/db/schema.rb index c2271a7ad..cfbd6e2f3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_09_01_160808) do +ActiveRecord::Schema.define(version: 2022_10_03_144016) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -410,8 +410,10 @@ ActiveRecord::Schema.define(version: 2022_09_01_160808) do t.string "name" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.uuid "school_id", null: false t.index ["academic_year_id"], name: "index_education_cohorts_on_academic_year_id" t.index ["program_id"], name: "index_education_cohorts_on_program_id" + t.index ["school_id"], name: "index_education_cohorts_on_school_id" t.index ["university_id"], name: "index_education_cohorts_on_university_id" end @@ -848,6 +850,7 @@ ActiveRecord::Schema.define(version: 2022_09_01_160808) do add_foreign_key "education_academic_years", "universities" add_foreign_key "education_cohorts", "education_academic_years", column: "academic_year_id" add_foreign_key "education_cohorts", "education_programs", column: "program_id" + add_foreign_key "education_cohorts", "education_schools", column: "school_id" add_foreign_key "education_cohorts", "universities" add_foreign_key "education_diplomas", "universities" add_foreign_key "education_programs", "education_programs", column: "parent_id" diff --git a/test/fixtures/education/cohorts.yml b/test/fixtures/education/cohorts.yml index c8f91dc32..13f18884f 100644 --- a/test/fixtures/education/cohorts.yml +++ b/test/fixtures/education/cohorts.yml @@ -8,18 +8,21 @@ # updated_at :datetime not null # academic_year_id :uuid not null, indexed # program_id :uuid not null, indexed +# school_id :uuid not null, indexed # university_id :uuid not null, indexed # # Indexes # # index_education_cohorts_on_academic_year_id (academic_year_id) # index_education_cohorts_on_program_id (program_id) +# index_education_cohorts_on_school_id (school_id) # index_education_cohorts_on_university_id (university_id) # # Foreign Keys # # fk_rails_0f4a4f43d9 (university_id => universities.id) # fk_rails_72528c3d76 (program_id => education_programs.id) +# fk_rails_8545767e2d (school_id => education_schools.id) # fk_rails_c2d725cabd (academic_year_id => education_academic_years.id) # diff --git a/test/models/education/cohort_test.rb b/test/models/education/cohort_test.rb index 2bb945a42..deb8e05eb 100644 --- a/test/models/education/cohort_test.rb +++ b/test/models/education/cohort_test.rb @@ -8,18 +8,21 @@ # updated_at :datetime not null # academic_year_id :uuid not null, indexed # program_id :uuid not null, indexed +# school_id :uuid not null, indexed # university_id :uuid not null, indexed # # Indexes # # index_education_cohorts_on_academic_year_id (academic_year_id) # index_education_cohorts_on_program_id (program_id) +# index_education_cohorts_on_school_id (school_id) # index_education_cohorts_on_university_id (university_id) # # Foreign Keys # # fk_rails_0f4a4f43d9 (university_id => universities.id) # fk_rails_72528c3d76 (program_id => education_programs.id) +# fk_rails_8545767e2d (school_id => education_schools.id) # fk_rails_c2d725cabd (academic_year_id => education_academic_years.id) # require "test_helper" -- GitLab