diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js index 7d66de99bb90ed200fecdd71024248290187ca17..8325a2d7fb557ccbcf2813bec4b23fe348317df0 100644 --- a/app/assets/javascripts/admin.js +++ b/app/assets/javascripts/admin.js @@ -12,6 +12,7 @@ //= require trix //= require sortablejs/Sortable //= require slug/slug +//= require cocoon //= require_self //= require_tree ./admin/commons //= require_tree ./admin/plugins diff --git a/app/controllers/admin/education/school/role/people_controller.rb b/app/controllers/admin/education/school/role/people_controller.rb index ad55e0a984ba7973098fc3e128cfbda109216509..a1bd4624bdd02b8390da2cfbed647be8cdaa9ffb 100644 --- a/app/controllers/admin/education/school/role/people_controller.rb +++ b/app/controllers/admin/education/school/role/people_controller.rb @@ -57,9 +57,7 @@ class Admin::Education::School::Role::PeopleController < Admin::Education::Schoo end def involvement_params - params.require(:university_person_involvement) - .permit(:description, :position, :person_id) - .merge(university_id: @school.university_id, kind: :administrator) + params.require(:university_person_involvement).permit(:position, :person_id) end def model diff --git a/app/controllers/admin/education/school/roles_controller.rb b/app/controllers/admin/education/school/roles_controller.rb index afbdcd921bc989de362009d6ae70adeabb44b213..7ab46b743b6a18ddeb7ae3c95e381842896a8173 100644 --- a/app/controllers/admin/education/school/roles_controller.rb +++ b/app/controllers/admin/education/school/roles_controller.rb @@ -3,6 +3,8 @@ class Admin::Education::School::RolesController < Admin::Education::School::Appl include Admin::Reorderable + before_action :load_administration_people, only: [:new, :edit, :create, :update] + def index @roles = @roles.ordered breadcrumb @@ -59,11 +61,15 @@ class Admin::Education::School::RolesController < Admin::Education::School::Appl def role_params params.require(:university_role) - .permit(:description, :position) + .permit(:description, involvements_attributes: [:id, :person_id, :position, :_destroy]) .merge(target: @school, university_id: @school.university_id) end def model University::Role end + + def load_administration_people + @administration_people = current_university.people.administration.accessible_by(current_ability).ordered + end end diff --git a/app/models/university/person/involvement.rb b/app/models/university/person/involvement.rb index 9da1b3c05276d4f79e2288ca667ae7c649da26b8..ed52d21ce159ea58fbe887f9b547454117d484cf 100644 --- a/app/models/university/person/involvement.rb +++ b/app/models/university/person/involvement.rb @@ -33,6 +33,10 @@ class University::Person::Involvement < ApplicationRecord belongs_to :person, class_name: 'University::Person' belongs_to :target, polymorphic: true + validates :person_id, uniqueness: { scope: [:target_id, :target_type] } + + before_validation :set_kind, on: :create + before_validation :set_university_id, on: :create after_commit :sync_with_git def to_s @@ -48,4 +52,19 @@ class University::Person::Involvement < ApplicationRecord def last_ordered_element self.class.unscoped.where(university_id: university_id, target: target).ordered.last end + + def set_kind + case target_type + when "Education::Program" + self.kind = :teacher + when "Research::Laboratory" + self.kind = :researcher + else + self.kind = :administrator + end + end + + def set_university_id + self.university_id = self.person.university_id + end end diff --git a/app/models/university/role.rb b/app/models/university/role.rb index befa1d6c7ef5460c4ff0988a934f32499169ff3b..7d6c29d6fa46ae38b9e2b5da3ecc6a13cb3b9371 100644 --- a/app/models/university/role.rb +++ b/app/models/university/role.rb @@ -25,9 +25,11 @@ class University::Role < ApplicationRecord belongs_to :university belongs_to :target, polymorphic: true, optional: true - has_many :involvements, class_name: 'University::Person::Involvement', as: :target, dependent: :destroy + has_many :involvements, class_name: 'University::Person::Involvement', as: :target, dependent: :destroy, inverse_of: :target has_many :people, through: :involvements + accepts_nested_attributes_for :involvements, reject_if: :all_blank, allow_destroy: true + def to_s "#{description}" end diff --git a/app/views/admin/education/school/role/people/_form.html.erb b/app/views/admin/education/school/role/people/_form.html.erb index 3b50a16f7f6524e37f0b0a27fe0ec25eed08fa9c..b4cbef88477622751f0f89df1ef50dc209bf92a7 100644 --- a/app/views/admin/education/school/role/people/_form.html.erb +++ b/app/views/admin/education/school/role/people/_form.html.erb @@ -6,14 +6,7 @@ <h5 class="card-title mb-0"><%= t('admin.infos') %></h5> </div> <div class="card-body"> - <div class="row"> - <div class="col-md-6"> - <%= f.association :person, collection: @available_people %> - </div> - <div class="col-md-6"> - <%= f.input :description %> - </div> - </div> + <%= f.association :person, collection: @available_people %> </div> </div> <% content_for :action_bar_right do %> diff --git a/app/views/admin/education/school/role/people/_list.html.erb b/app/views/admin/education/school/role/people/_list.html.erb index 12a6ee1bbcbbea62f12102acaf7f4088fa334b65..c98d5818bc939c5310b73d5f35221f9a809d6ed3 100644 --- a/app/views/admin/education/school/role/people/_list.html.erb +++ b/app/views/admin/education/school/role/people/_list.html.erb @@ -6,7 +6,6 @@ <th width="20" class="ps-0"> </th> <% end %> <th class="ps-0"><%= University::Person.model_name.human %></th> - <th><%= University::Person::Involvement.human_attribute_name("description") %></th> <th></th> </tr> </thead> @@ -21,7 +20,6 @@ involvement.person, [:admin, involvement.person] %> </td> - <td><%= involvement.description %></td> <td class="text-end pe-0"> <div class="btn-group" role="group"> <%= link_to t('edit'), diff --git a/app/views/admin/education/school/roles/_form.html.erb b/app/views/admin/education/school/roles/_form.html.erb index 24b66befe14b5cf4f64877cf9ce1d0bca0c74c78..af6fb4ed217400fa93ec69881c42ba228ad3dcbf 100644 --- a/app/views/admin/education/school/roles/_form.html.erb +++ b/app/views/admin/education/school/roles/_form.html.erb @@ -1,14 +1,39 @@ <%= simple_form_for [:admin, role], url: role.new_record? ? admin_education_school_roles_path(@school) : admin_education_school_role_path(role, { school_id: @school.id }) do |f| %> - <div class="card flex-fill w-100"> - <div class="card-header"> - <h5 class="card-title mb-0"><%= t('admin.infos') %></h5> + + <div class="row"> + <div class="col-md-8"> + <div class="card flex-fill w-100"> + <div class="card-header"> + <h5 class="card-title mb-0"><%= t('admin.infos') %></h5> + </div> + <div class="card-body"> + <%= f.input :description %> + </div> + </div> </div> - <div class="card-body"> - <%= f.input :description %> + <div class="col-md-4"> + <div class="card flex-fill w-100"> + <div class="card-header"> + <h5 class="card-title mb-0"><%= University::Person.model_name.human(count: 2) %></h5> + </div> + <div class="card-body"> + <%= link_to_add_association t('add'), f, :involvements, class: "btn btn-primary mb-3", data: { + 'association-insertion-method': 'append', + 'association-insertion-node': '#involvements' + } %> + + <div class="mb-3" id="involvements" data-sortable="inputs"> + <%= f.simple_fields_for :involvements, include_id: false do |involvement_f| %> + <%= render 'admin/education/school/roles/involvement_fields', f: involvement_f, include_id: true %> + <% end %> + </div> + </div> + </div> </div> </div> + <% content_for :action_bar_right do %> <%= submit f %> <% end %> diff --git a/app/views/admin/education/school/roles/_involvement_fields.html.erb b/app/views/admin/education/school/roles/_involvement_fields.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..009f66f10e01be2803737f8b1692347dc1583fb8 --- /dev/null +++ b/app/views/admin/education/school/roles/_involvement_fields.html.erb @@ -0,0 +1,18 @@ +<% include_id ||= false %> +<div class="card nested-fields mb-2"> + <div class="card-body"> + <div class="row align-items-center"> + <div class="col-1"> + <i class="fa fa-bars handle"></i> + </div> + <div class="col-9"> + <%= f.association :person, collection: @administration_people, label: false, include_blank: "Sélectionnez une personne", wrapper: false, required: true %> + </div> + <div class="col-2"> + <%= link_to_remove_association '<i class="fas fa-trash"></i>'.html_safe, f, class: 'btn btn-sm btn-danger' %> + </div> + </div> + </div> + <%= f.hidden_field :position, data: { 'sortable-input': '' } %> + <%= f.hidden_field :id if include_id %> +</div> diff --git a/app/views/admin/education/school/roles/show.html.erb b/app/views/admin/education/school/roles/show.html.erb index f4c18babfa4490bf24910f2b1bae4923db9ca539..68b39b6b7223ff66775ca32fc78b9fd99fc13cfc 100644 --- a/app/views/admin/education/school/roles/show.html.erb +++ b/app/views/admin/education/school/roles/show.html.erb @@ -2,3 +2,9 @@ <%= link_to t('create'), new_admin_education_school_role_person_path(school_id: @school.id, role_id: @role.id), class: button_classes %> <%= render 'admin/education/school/role/people/list', involvements: @involvements %> + +<% content_for :action_bar_right do %> + <%= link_to t('edit'), + edit_admin_education_school_role_path(@role, { school_id: @school.id }), + class: button_classes if can?(:update, @role) %> +<% end %>