diff --git a/app/controllers/admin/application_controller.rb b/app/controllers/admin/application_controller.rb index 921a46d7ae157ee5413edfc7767c95ae8065efa0..084644e1d0aac2dc59bcc6cd28a4eac79cfcb840 100644 --- a/app/controllers/admin/application_controller.rb +++ b/app/controllers/admin/application_controller.rb @@ -1,6 +1,8 @@ class Admin::ApplicationController < ApplicationController layout 'admin/layouts/application' + before_action :load_filters, only: :index + around_action :switch_locale protected @@ -25,4 +27,12 @@ class Admin::ApplicationController < ApplicationController locale = LocaleService.locale(current_user, request.env['HTTP_ACCEPT_LANGUAGE']) I18n.with_locale(locale, &action) end + + def load_filters + @filters = [] + filter_class_name = "::Filters::#{self.class.to_s.gsub('Controller', '')}" + # filter_class will be nil if filter does not exist + filter_class = filter_class_name.safe_constantize + @filters = filter_class.new(current_user).list unless filter_class.nil? + end end diff --git a/app/controllers/admin/university/people_controller.rb b/app/controllers/admin/university/people_controller.rb index 73b048763c7f2422e60ada101928405974cedd30..bdc9ee2d24cb6797568ca27be11c62683b902ccf 100644 --- a/app/controllers/admin/university/people_controller.rb +++ b/app/controllers/admin/university/people_controller.rb @@ -3,8 +3,12 @@ class Admin::University::PeopleController < Admin::University::ApplicationContro through: :current_university, through_association: :people + + has_scope :for_search_term + has_scope :for_role + def index - @people = @people.ordered.page(params[:page]) + @people = apply_scopes(@people).ordered.page(params[:page]) breadcrumb end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 1cdadc2ae8ac07723f3ae7f42c6a330b06c38052..ddb1f9b14892952864005cc7807c0495be6273cc 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -5,7 +5,6 @@ class Admin::UsersController < Admin::ApplicationController has_scope :for_search_term def index - @filters = ::Filters::User.new(current_user).list @users = apply_scopes(@users).ordered.page(params[:page]) breadcrumb end diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index 71ca1ad6ee362b58d6d0744b936b825bbb9c8a3e..14cda1e87c58e99446f1e9268fecb73820bcf977 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -63,6 +63,7 @@ class Communication::Website::Post < ApplicationRecord validates :title, presence: true before_validation :set_published_at, if: :published_changed? + after_save_commit :update_authors_statuses!, if: :saved_change_to_author_id? scope :published, -> { where(published: true) } scope :ordered, -> { order(published_at: :desc, created_at: :desc) } @@ -123,4 +124,12 @@ class Communication::Website::Post < ApplicationRecord def inherited_blob_ids [best_featured_image&.blob_id] end + + def update_authors_statuses! + old_author = University::Person.find_by(id: author_id_before_last_save) + if old_author && old_author.communication_website_posts.none? + old_author.update_and_sync(is_author: false) + end + author.update_and_sync(is_author: true) if author_id + end end diff --git a/app/models/university/person.rb b/app/models/university/person.rb index 8f4533ab0afcce87ea7fddb14c1b341c1d73d8f4..242b23a70ce295607b4075ee8c35815767300701 100644 --- a/app/models/university/person.rb +++ b/app/models/university/person.rb @@ -11,6 +11,7 @@ # habilitation :boolean default(FALSE) # is_administration :boolean # is_alumnus :boolean default(FALSE) +# is_author :boolean # is_researcher :boolean # is_teacher :boolean # last_name :string @@ -45,6 +46,8 @@ class University::Person < ApplicationRecord include WithPicture include WithEducation + LIST_OF_ROLES = [:administration, :teacher, :researcher, :alumnus, :author].freeze + has_summernote :biography belongs_to :user, optional: true @@ -109,22 +112,34 @@ class University::Person < ApplicationRecord scope :teachers, -> { where(is_teacher: true) } scope :researchers, -> { where(is_researcher: true) } scope :alumni, -> { where(is_alumnus: true) } + scope :for_role, -> (role) { where("is_#{role}": true) } + + scope :for_search_term, -> (term) { + where(" + unaccent(concat(university_people.first_name, ' ', university_people.last_name)) ILIKE unaccent(:term) OR + unaccent(concat(university_people.last_name, ' ', university_people.first_name)) ILIKE unaccent(:term) OR + unaccent(university_people.first_name) ILIKE unaccent(:term) OR + unaccent(university_people.last_name) ILIKE unaccent(:term) OR + unaccent(university_people.email) ILIKE unaccent(:term) OR + unaccent(university_people.phone) ILIKE unaccent(:term) OR + unaccent(university_people.biography) ILIKE unaccent(:term) OR + unaccent(university_people.description) ILIKE unaccent(:term) OR + unaccent(university_people.description_short) ILIKE unaccent(:term) OR + unaccent(university_people.twitter) ILIKE unaccent(:term) OR + unaccent(university_people.url) ILIKE unaccent(:term) + ", term: "%#{sanitize_sql_like(term)}%") + } def to_s "#{first_name} #{last_name}" end def roles - [:administration, :teacher, :researcher, :alumnus, :author].reject do |role| + LIST_OF_ROLES.reject do |role| ! send "is_#{role}" end end - # TODO denormalize - def is_author - communication_website_posts.any? - end - def websites university.communication_websites end diff --git a/app/models/university/person/administrator.rb b/app/models/university/person/administrator.rb index da5cbf55348ac85560812cea12230796c7972e9f..387c93a640f1446247ac545493cbc6ab290b2d42 100644 --- a/app/models/university/person/administrator.rb +++ b/app/models/university/person/administrator.rb @@ -11,6 +11,7 @@ # habilitation :boolean default(FALSE) # is_administration :boolean # is_alumnus :boolean default(FALSE) +# is_author :boolean # is_researcher :boolean # is_teacher :boolean # last_name :string diff --git a/app/models/university/person/alumnus.rb b/app/models/university/person/alumnus.rb index f9a0ae78d3dd880e672ec601c8f2cd3dc2d5d89c..2e140c8c5a3c4c64f2172aea733483431d94cc74 100644 --- a/app/models/university/person/alumnus.rb +++ b/app/models/university/person/alumnus.rb @@ -11,6 +11,7 @@ # habilitation :boolean default(FALSE) # is_administration :boolean # is_alumnus :boolean default(FALSE) +# is_author :boolean # is_researcher :boolean # is_teacher :boolean # last_name :string diff --git a/app/models/university/person/author.rb b/app/models/university/person/author.rb index 95faae1d68252020f2db9ae06ae188e7efee5d46..a51eadb10d87ba126e899071f130495f7b315e13 100644 --- a/app/models/university/person/author.rb +++ b/app/models/university/person/author.rb @@ -11,6 +11,7 @@ # habilitation :boolean default(FALSE) # is_administration :boolean # is_alumnus :boolean default(FALSE) +# is_author :boolean # is_researcher :boolean # is_teacher :boolean # last_name :string diff --git a/app/models/university/person/researcher.rb b/app/models/university/person/researcher.rb index 079783999e11704b3ceec18c3a716706980ce35f..44616e8492facc2bcb1908e8519bdcccff2d8bac 100644 --- a/app/models/university/person/researcher.rb +++ b/app/models/university/person/researcher.rb @@ -11,6 +11,7 @@ # habilitation :boolean default(FALSE) # is_administration :boolean # is_alumnus :boolean default(FALSE) +# is_author :boolean # is_researcher :boolean # is_teacher :boolean # last_name :string diff --git a/app/models/university/person/teacher.rb b/app/models/university/person/teacher.rb index 4f424311600efbcb2b217b527852fed33d94eccf..37140ba7b1d670cda457627f9b6feeccffbceb18 100644 --- a/app/models/university/person/teacher.rb +++ b/app/models/university/person/teacher.rb @@ -11,6 +11,7 @@ # habilitation :boolean default(FALSE) # is_administration :boolean # is_alumnus :boolean default(FALSE) +# is_author :boolean # is_researcher :boolean # is_teacher :boolean # last_name :string diff --git a/app/services/filters/admin/university/people.rb b/app/services/filters/admin/university/people.rb new file mode 100644 index 0000000000000000000000000000000000000000..515697eafa3b0dc4ec161cd5d6715c283f5d9e02 --- /dev/null +++ b/app/services/filters/admin/university/people.rb @@ -0,0 +1,9 @@ +module Filters + class Admin::University::People < Filters::Base + def initialize(user) + super + add_search + add :for_role, ::University::Person::LIST_OF_ROLES.map { |r| { to_s: I18n.t("activerecord.attributes.university/person.#{r}"), id: r } }, I18n.t('filters.attributes.role') + end + end +end diff --git a/app/services/filters/admin/users.rb b/app/services/filters/admin/users.rb new file mode 100644 index 0000000000000000000000000000000000000000..bc431fa8eccedc0ecb727251214081bd884eeb65 --- /dev/null +++ b/app/services/filters/admin/users.rb @@ -0,0 +1,9 @@ +module Filters + class Admin::Users < Filters::Base + def initialize(user) + super + add_search + add :for_role, ::User.roles.keys.map { |r| { to_s: I18n.t("activerecord.attributes.user.roles.#{r}"), id: r } }, I18n.t('filters.attributes.role') + end + end +end diff --git a/app/services/filters/user.rb b/app/services/filters/user.rb deleted file mode 100644 index a71e11effad3718dc0f3e890188e92e6ec83c9e1..0000000000000000000000000000000000000000 --- a/app/services/filters/user.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Filters - class User < Base - def initialize(user) - super - add_search - add :for_role, ::User.roles.keys.map { |r| { to_s: r.humanize, id: r } }, I18n.t('filters.attributes.role') - end - end -end diff --git a/app/views/admin/communication/website/posts/index.html.erb b/app/views/admin/communication/website/posts/index.html.erb index 814a431bf51ab1da6c16ae00eb1bec4d3504f962..e13ed71d7909411615da36df6db1dbbb891872cf 100644 --- a/app/views/admin/communication/website/posts/index.html.erb +++ b/app/views/admin/communication/website/posts/index.html.erb @@ -2,7 +2,7 @@ <%= render 'admin/communication/websites/sidebar' do %> <div class="card"> - <%= render 'admin/communication/website/posts/list', posts: @posts, hide_author: true %> + <%= render 'admin/communication/website/posts/list', posts: @posts %> <% if @posts.total_pages > 1 %> <div class="card-footer"> <%= paginate @posts, theme: 'bootstrap-5' %> diff --git a/app/views/admin/university/people/index.html.erb b/app/views/admin/university/people/index.html.erb index b95e8aa322d9c79d5f98203b273e6acaa580aea7..df601c13f7ed7424fe37ef9a2f4e46cd067630ee 100644 --- a/app/views/admin/university/people/index.html.erb +++ b/app/views/admin/university/people/index.html.erb @@ -1,5 +1,7 @@ <% content_for :title, "#{University::Person.model_name.human(count: 2)} (#{@people.total_count})" %> +<%= render 'filters', current_path: admin_university_people_path, filters: @filters if @filters.any? %> + <%= render 'admin/university/people/list', people: @people %> <%= paginate @people, theme: 'bootstrap-5' %> diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index af80ff5b3d6d1520db414931e45d74790689756d..1e1ee9d58e0331d332c45fd08d547ff9c80ddef1 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -21,7 +21,7 @@ <td><%= link_to user.email, [:admin, user] %></td> <td><%= user.first_name %></td> <td><%= user.last_name %></td> - <td><%= user.role.humanize %></td> + <td><%= t("activerecord.attributes.user.roles.#{user.role}") %></td> <td><%= user.language %></td> <td class="text-end"> <div class="btn-group" role="group"> diff --git a/db/migrate/20220425150705_add_is_author_to_university_people.rb b/db/migrate/20220425150705_add_is_author_to_university_people.rb new file mode 100644 index 0000000000000000000000000000000000000000..287c2cb05772e3445ca8518061f49dc0e35df04b --- /dev/null +++ b/db/migrate/20220425150705_add_is_author_to_university_people.rb @@ -0,0 +1,5 @@ +class AddIsAuthorToUniversityPeople < ActiveRecord::Migration[6.1] + def change + add_column :university_people, :is_author, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 604f8b5a6f49203c067224c55c7686ad8f322dfd..bf0583b11350e281eb844e4e6968bde0c0af469d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -689,6 +689,7 @@ ActiveRecord::Schema.define(version: 2022_04_25_152944) do t.boolean "is_alumnus", default: false t.text "description_short" t.string "name" + t.boolean "is_author" t.index ["university_id"], name: "index_university_people_on_university_id" t.index ["user_id"], name: "index_university_people_on_user_id" end diff --git a/lib/tasks/app.rake b/lib/tasks/app.rake index 616dfe0f4cbf34b41bc0261d9369ef8b0d4bf344..c83caba11e229f3bca805868771096e0cbde6865 100644 --- a/lib/tasks/app.rake +++ b/lib/tasks/app.rake @@ -8,17 +8,9 @@ namespace :app do desc 'Fix things' task fix: :environment do - Communication::Block.where(template: 'partners').find_each do |partner| - next if partner.data.nil? - data = partner.data - next unless data.has_key? 'elements' - elements = data['elements'] - next if elements.none? - first = elements.first - next unless first.has_key? 'partners' - partner.title = first['title'] - partner.data['elements'] = first['partners'] - partner.save + University::Person.find_each do |person| + person.is_author = person.communication_website_posts.any? + person.save end end