diff --git a/app/controllers/admin/university/people_controller.rb b/app/controllers/admin/university/people_controller.rb index b3ef36fb44e323bf962a657eae6a45c01d0ab0a2..bdc9ee2d24cb6797568ca27be11c62683b902ccf 100644 --- a/app/controllers/admin/university/people_controller.rb +++ b/app/controllers/admin/university/people_controller.rb @@ -5,6 +5,7 @@ class Admin::University::PeopleController < Admin::University::ApplicationContro has_scope :for_search_term + has_scope :for_role def index @people = apply_scopes(@people).ordered.page(params[:page]) 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 04132d7c266d85f5312370ff3febaf020fb0860f..b92e02457df910c7acd4d17a12837e30b847e62b 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 @@ -44,6 +45,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 @@ -108,6 +111,8 @@ 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 @@ -129,16 +134,11 @@ class University::Person < ApplicationRecord 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 f5fda080ad57e31d179660161ba2567f1bf4b526..8205a3fb2438b3345d11bee7a6af8da7242923e8 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 06c70f24cd3fd7a02f2ecd63a47a35dd48ed660a..cf5bf3a3ddcd14f85321c2c107dbf494b2480b77 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 67dda6a6e289d65603bd5184ad345b06e76283db..e18ceeef71fd03f9ad536fd00f4f81d1d538e543 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 8fffe668f473cf48fa7237a2d68500b78c507ec1..307043e55627513eeb31c20d0a17c2fbc6a681a3 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 df1e88803e668e76624ea44562d02825c1e7ccad..8a6ef74e97f93bef70c2261f3bda3f1f1a7296df 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 index 6ad19dca1b5bfe91342e4ac6f1fc7f386ba9e9bf..515697eafa3b0dc4ec161cd5d6715c283f5d9e02 100644 --- a/app/services/filters/admin/university/people.rb +++ b/app/services/filters/admin/university/people.rb @@ -3,6 +3,7 @@ module Filters 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/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/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 9344a9d833e29ce473f3c4cb2a46179b8a9ef262..7df6f03f9a24a3e50cbf35e754db7aa6306a4e9d 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_04_21_093107) do +ActiveRecord::Schema.define(version: 2022_04_25_150705) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -671,6 +671,7 @@ ActiveRecord::Schema.define(version: 2022_04_21_093107) do t.string "linkedin" t.boolean "is_alumnus", default: false t.text "description_short" + 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