From 20e2e989a5f8691723c41ee7ccccee6a768e546c Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 6 Jan 2022 18:41:31 +0100
Subject: [PATCH] wip #108

---
 app/models/communication/website/post.rb |  1 +
 app/models/university/person.rb          | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 36099f9fe..fe3fb8522 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -59,6 +59,7 @@ class Communication::Website::Post < ApplicationRecord
 
   before_validation :set_published_at, if: :published_changed?
 
+  scope :published, -> { where(published: true) }
   scope :ordered, -> { order(published_at: :desc, created_at: :desc) }
   scope :recent, -> { order(published_at: :desc).limit(5) }
 
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index 68299e2fa..8cd7b16e9 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -100,16 +100,28 @@ class University::Person < ApplicationRecord
   end
 
   def identifiers(website: nil)
-    website_id = website&.id
     list = []
     # TODO :administrator
     [:author, :researcher, :teacher].each do |role|
-      list << role if send("#{role.to_s}_websites").pluck(:id).include?(website_id)
+      list << role if public_send("#{role.to_s}_for_website", website)
     end
     list << :static unless list.empty?
     list
   end
 
+  def author_for_website(website)
+    is_author && communication_website_posts.published.where(communication_website_id: website&.id).any?
+  end
+
+  def researcher_for_website(website)
+    is_researcher
+  end
+
+  def teacher_for_website(website)
+    # a des formations publiées pour ce website
+    is_teacher && website.programs.published.joins(:teachers).where(education_program_teachers: { person_id: id }).any?
+  end
+
   def git_path_static
     "content/persons/#{slug}.html"
   end
-- 
GitLab