From b3226090bafdc15b7cb23bd34cc613a17c4456f2 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Mon, 7 Mar 2022 18:05:55 +0100
Subject: [PATCH] with university

---
 app/models/communication/block.rb                     | 2 +-
 app/models/communication/extranet.rb                  | 2 +-
 app/models/communication/website.rb                   | 3 +--
 app/models/communication/website/imported/author.rb   | 2 +-
 app/models/communication/website/imported/category.rb | 2 +-
 app/models/communication/website/imported/medium.rb   | 3 ++-
 app/models/communication/website/imported/page.rb     | 2 +-
 app/models/communication/website/imported/post.rb     | 2 +-
 app/models/communication/website/imported/website.rb  | 3 ++-
 app/models/communication/website/index_page.rb        | 2 +-
 app/models/communication/website/menu.rb              | 2 +-
 app/models/communication/website/menu/item.rb         | 2 +-
 app/models/communication/website/page.rb              | 2 +-
 app/models/communication/website/post.rb              | 2 +-
 app/models/concerns/with_university.rb                | 8 ++++++++
 app/models/education/program.rb                       | 2 +-
 app/models/research/journal.rb                        | 2 +-
 app/models/research/journal/article.rb                | 2 +-
 app/models/research/journal/volume.rb                 | 2 +-
 app/models/research/laboratory/axis.rb                | 2 +-
 app/models/research/thesis.rb                         | 2 +-
 app/models/university/organization.rb                 | 3 +--
 app/models/university/organization/import.rb          | 5 +++--
 app/models/university/person.rb                       | 2 +-
 app/models/university/person/involvement.rb           | 2 +-
 app/models/university/role.rb                         | 2 +-
 app/models/user.rb                                    | 2 +-
 27 files changed, 38 insertions(+), 29 deletions(-)
 create mode 100644 app/models/concerns/with_university.rb

diff --git a/app/models/communication/block.rb b/app/models/communication/block.rb
index d9a1936eb..a29994da5 100644
--- a/app/models/communication/block.rb
+++ b/app/models/communication/block.rb
@@ -23,9 +23,9 @@
 #  fk_rails_18291ef65f  (university_id => universities.id)
 #
 class Communication::Block < ApplicationRecord
+  include WithUniversity
   include WithPosition
 
-  belongs_to :university
   belongs_to :about, polymorphic: true
 
   enum template: {
diff --git a/app/models/communication/extranet.rb b/app/models/communication/extranet.rb
index 3ab3df331..4d22aba55 100644
--- a/app/models/communication/extranet.rb
+++ b/app/models/communication/extranet.rb
@@ -18,7 +18,7 @@
 #  fk_rails_c2268c7ebd  (university_id => universities.id)
 #
 class Communication::Extranet < ApplicationRecord
-  belongs_to :university
+  include WithUniversity
 
   validates_presence_of :name, :domain
 
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 34d1eb1cf..f23530348 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -25,6 +25,7 @@
 #  fk_rails_bb6a496c08  (university_id => universities.id)
 #
 class Communication::Website < ApplicationRecord
+  include WithUniversity
   include WithAbouts
   include WithConfigs
   include WithGit
@@ -33,8 +34,6 @@ class Communication::Website < ApplicationRecord
   include WithIndexPages
   include WithMenuItems
 
-  belongs_to :university
-
   scope :ordered, -> { order(:name) }
 
   enum git_provider: {
diff --git a/app/models/communication/website/imported/author.rb b/app/models/communication/website/imported/author.rb
index ad3f3b708..98469ecdf 100644
--- a/app/models/communication/website/imported/author.rb
+++ b/app/models/communication/website/imported/author.rb
@@ -27,8 +27,8 @@
 #  fk_rails_e37b63f880  (author_id => university_people.id)
 #
 class Communication::Website::Imported::Author < ApplicationRecord
+  include WithUniversity
 
-  belongs_to :university
   belongs_to :website,
              class_name: 'Communication::Website::Imported::Website'
   belongs_to :author,
diff --git a/app/models/communication/website/imported/category.rb b/app/models/communication/website/imported/category.rb
index 4eb668cf6..b3f5841f8 100644
--- a/app/models/communication/website/imported/category.rb
+++ b/app/models/communication/website/imported/category.rb
@@ -29,8 +29,8 @@
 #  fk_rails_f6070ca53d  (university_id => universities.id)
 #
 class Communication::Website::Imported::Category < ApplicationRecord
+  include WithUniversity
 
-  belongs_to :university
   belongs_to :website,
              class_name: 'Communication::Website::Imported::Website'
   belongs_to :category,
diff --git a/app/models/communication/website/imported/medium.rb b/app/models/communication/website/imported/medium.rb
index f4388e679..0f3ab3b07 100644
--- a/app/models/communication/website/imported/medium.rb
+++ b/app/models/communication/website/imported/medium.rb
@@ -25,7 +25,8 @@
 #  fk_rails_c54ac5a28b  (website_id => communication_website_imported_websites.id)
 #
 class Communication::Website::Imported::Medium < ApplicationRecord
-  belongs_to :university
+  include WithUniversity
+
   belongs_to :website,
              class_name: 'Communication::Website::Imported::Website'
   has_many   :pages,
diff --git a/app/models/communication/website/imported/page.rb b/app/models/communication/website/imported/page.rb
index b98885582..f4dbdce8b 100644
--- a/app/models/communication/website/imported/page.rb
+++ b/app/models/communication/website/imported/page.rb
@@ -36,9 +36,9 @@
 #  fk_rails_e582fbdc5c  (featured_medium_id => communication_website_imported_media.id)
 #
 class Communication::Website::Imported::Page < ApplicationRecord
+  include WithUniversity
   include Communication::Website::Imported::WithRichText
 
-  belongs_to :university
   belongs_to :website,
              class_name: 'Communication::Website::Imported::Website'
   belongs_to :page,
diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb
index 44f5f36de..8e2bcaec9 100644
--- a/app/models/communication/website/imported/post.rb
+++ b/app/models/communication/website/imported/post.rb
@@ -37,9 +37,9 @@
 #  fk_rails_f9a08c7c77  (post_id => communication_website_posts.id)
 #
 class Communication::Website::Imported::Post < ApplicationRecord
+  include WithUniversity
   include Communication::Website::Imported::WithRichText
 
-  belongs_to :university
   belongs_to :website,
              class_name: 'Communication::Website::Imported::Website'
   belongs_to :post,
diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb
index 024462b80..0e6426109 100644
--- a/app/models/communication/website/imported/website.rb
+++ b/app/models/communication/website/imported/website.rb
@@ -20,7 +20,8 @@
 #  fk_rails_3dbdb9cb2d  (website_id => communication_websites.id)
 #
 class Communication::Website::Imported::Website < ApplicationRecord
-  belongs_to :university
+  include WithUniversity
+
   belongs_to :website,
              class_name: 'Communication::Website'
   has_many   :authors,
diff --git a/app/models/communication/website/index_page.rb b/app/models/communication/website/index_page.rb
index aa62f6213..fc8f8e428 100644
--- a/app/models/communication/website/index_page.rb
+++ b/app/models/communication/website/index_page.rb
@@ -27,6 +27,7 @@
 #  fk_rails_7eb45227ae  (university_id => universities.id)
 #
 class Communication::Website::IndexPage < ApplicationRecord
+  include WithUniversity
   include Sanitizable
   include WithGit
   include WithFeaturedImage
@@ -45,7 +46,6 @@ class Communication::Website::IndexPage < ApplicationRecord
       teachers: 140
   }
 
-  belongs_to :university
   belongs_to :website, foreign_key: :communication_website_id
 
   has_summernote :header_text
diff --git a/app/models/communication/website/menu.rb b/app/models/communication/website/menu.rb
index 2e21098b7..24f8df696 100644
--- a/app/models/communication/website/menu.rb
+++ b/app/models/communication/website/menu.rb
@@ -22,10 +22,10 @@
 #  fk_rails_dcc7198fc5  (communication_website_id => communication_websites.id)
 #
 class Communication::Website::Menu < ApplicationRecord
+  include WithUniversity
   include Sanitizable
   include WithGit
 
-  belongs_to :university
   belongs_to :website, foreign_key: :communication_website_id
   has_many :items, class_name: 'Communication::Website::Menu::Item', dependent: :destroy
 
diff --git a/app/models/communication/website/menu/item.rb b/app/models/communication/website/menu/item.rb
index f4980a465..4f84f3ebd 100644
--- a/app/models/communication/website/menu/item.rb
+++ b/app/models/communication/website/menu/item.rb
@@ -32,6 +32,7 @@
 #  fk_rails_fa4f4585e4  (website_id => communication_websites.id)
 #
 class Communication::Website::Menu::Item < ApplicationRecord
+  include WithUniversity
   include Sanitizable
   include WithTree
   include WithPosition
@@ -39,7 +40,6 @@ class Communication::Website::Menu::Item < ApplicationRecord
 
   attr_accessor :skip_publication_callback
 
-  belongs_to :university
   belongs_to :website, class_name: 'Communication::Website'
   belongs_to :menu, class_name: 'Communication::Website::Menu'
   belongs_to :parent, class_name: 'Communication::Website::Menu::Item', optional: true
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 74521470c..207d51207 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -40,6 +40,7 @@
 
 class Communication::Website::Page < ApplicationRecord
   include Sanitizable
+  include WithUniversity
   include WithGit
   include WithFeaturedImage
   include WithBlobs
@@ -51,7 +52,6 @@ class Communication::Website::Page < ApplicationRecord
 
   has_summernote :text
 
-  belongs_to :university
   belongs_to :website,
              foreign_key: :communication_website_id
   belongs_to :related_category,
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 9acbeb444..0a2250b06 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -33,6 +33,7 @@
 #
 class Communication::Website::Post < ApplicationRecord
   include Sanitizable
+  include WithUniversity
   include WithGit
   include WithFeaturedImage
   include WithBlobs
@@ -44,7 +45,6 @@ class Communication::Website::Post < ApplicationRecord
   has_one :imported_post,
           class_name: 'Communication::Website::Imported::Post',
           dependent: :destroy
-  belongs_to :university
   belongs_to :website,
              class_name: 'Communication::Website',
              foreign_key: :communication_website_id
diff --git a/app/models/concerns/with_university.rb b/app/models/concerns/with_university.rb
new file mode 100644
index 000000000..d0433b792
--- /dev/null
+++ b/app/models/concerns/with_university.rb
@@ -0,0 +1,8 @@
+module WithUniversity
+  extend ActiveSupport::Concern
+
+  included do
+    belongs_to :university
+    validates_presence_of :university
+  end
+end
diff --git a/app/models/education/program.rb b/app/models/education/program.rb
index 449c1d170..360d5ba5f 100644
--- a/app/models/education/program.rb
+++ b/app/models/education/program.rb
@@ -44,6 +44,7 @@
 #
 class Education::Program < ApplicationRecord
   include Sanitizable
+  include WithUniversity
   include WithGit
   include WithFeaturedImage
   include WithBlobs
@@ -70,7 +71,6 @@ class Education::Program < ApplicationRecord
 
   attr_accessor :skip_websites_categories_callback
 
-  belongs_to :university
   belongs_to :parent,
              class_name: 'Education::Program',
              optional: true
diff --git a/app/models/research/journal.rb b/app/models/research/journal.rb
index e34fdbcd3..62c75abdd 100644
--- a/app/models/research/journal.rb
+++ b/app/models/research/journal.rb
@@ -21,9 +21,9 @@
 #  fk_rails_96097d5f10  (university_id => universities.id)
 #
 class Research::Journal < ApplicationRecord
+  include WithUniversity
   include WithGit
 
-  belongs_to :university
   has_many :websites, class_name: 'Communication::Website', as: :about, dependent: :nullify
   has_many :volumes, foreign_key: :research_journal_id, dependent: :destroy
   has_many :articles, foreign_key: :research_journal_id, dependent: :destroy
diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb
index 984835af0..4b2dbf5d1 100644
--- a/app/models/research/journal/article.rb
+++ b/app/models/research/journal/article.rb
@@ -35,6 +35,7 @@
 #
 class Research::Journal::Article < ApplicationRecord
   include Sanitizable
+  include WithUniversity
   include WithGit
   include WithBlobs
   include WithPosition
@@ -43,7 +44,6 @@ class Research::Journal::Article < ApplicationRecord
   has_summernote :text
   has_one_attached :pdf
 
-  belongs_to :university
   belongs_to :journal, foreign_key: :research_journal_id
   belongs_to :volume, foreign_key: :research_journal_volume_id, optional: true
   belongs_to :updated_by, class_name: 'User'
diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb
index 844cb9b93..669024489 100644
--- a/app/models/research/journal/volume.rb
+++ b/app/models/research/journal/volume.rb
@@ -28,12 +28,12 @@
 #
 class Research::Journal::Volume < ApplicationRecord
   include Sanitizable
+  include WithUniversity
   include WithGit
   include WithBlobs
   include WithFeaturedImage
   include WithSlug
 
-  belongs_to :university
   belongs_to :journal, foreign_key: :research_journal_id
   has_many :articles, foreign_key: :research_journal_volume_id, dependent: :nullify
   has_many :websites, -> { distinct }, through: :journal
diff --git a/app/models/research/laboratory/axis.rb b/app/models/research/laboratory/axis.rb
index 40cae3951..9601058ec 100644
--- a/app/models/research/laboratory/axis.rb
+++ b/app/models/research/laboratory/axis.rb
@@ -25,11 +25,11 @@
 #
 class Research::Laboratory::Axis < ApplicationRecord
   include Sanitizable
+  include WithUniversity
   include WithPosition
 
   has_summernote :text
 
-  belongs_to :university
   belongs_to :laboratory, foreign_key: :research_laboratory_id
 
   def to_s
diff --git a/app/models/research/thesis.rb b/app/models/research/thesis.rb
index 48f1f4aed..e8fea9e2f 100644
--- a/app/models/research/thesis.rb
+++ b/app/models/research/thesis.rb
@@ -31,8 +31,8 @@
 #
 class Research::Thesis < ApplicationRecord
   include Sanitizable
+  include WithUniversity
 
-  belongs_to :university
   belongs_to :laboratory, foreign_key: :research_laboratory_id
   belongs_to :author, class_name: 'University::Person'
   belongs_to :director, class_name: 'University::Person'
diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb
index 1df61f1f0..bf78d9717 100644
--- a/app/models/university/organization.rb
+++ b/app/models/university/organization.rb
@@ -31,11 +31,10 @@
 class University::Organization < ApplicationRecord
   include WithGit
   include WithBlobs
+  include WithUniversity
 
   has_one_attached_deletable :logo
 
-  belongs_to :university
-
   scope :ordered, -> { order(:name) }
 
   validates_presence_of :name
diff --git a/app/models/university/organization/import.rb b/app/models/university/organization/import.rb
index 421ad605b..34e9ba553 100644
--- a/app/models/university/organization/import.rb
+++ b/app/models/university/organization/import.rb
@@ -19,7 +19,8 @@
 #  fk_rails_da057ff44d  (user_id => users.id)
 #
 class University::Organization::Import < ApplicationRecord
-  belongs_to :university
+  include WithUniversity
+
   belongs_to :user
 
   has_one_attached :file
@@ -40,7 +41,7 @@ class University::Organization::Import < ApplicationRecord
 
   def parse
     csv.each do |row|
-      university.organizations.where(name: row['name']).first_or_create do |o|
+      university.organizations.where(university: university, name: row['name']).first_or_create do |o|
         [
           :long_name,
           :kind,
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index dd1d4ca90..c98f4d96f 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -32,6 +32,7 @@
 #
 class University::Person < ApplicationRecord
   include Sanitizable
+  include WithUniversity
   include WithGit
   include WithBlobs
   include WithSlug
@@ -40,7 +41,6 @@ class University::Person < ApplicationRecord
 
   has_summernote :biography
 
-  belongs_to :university
   belongs_to :user, optional: true
 
   has_and_belongs_to_many :research_journal_articles,
diff --git a/app/models/university/person/involvement.rb b/app/models/university/person/involvement.rb
index 648bcb594..988be24c9 100644
--- a/app/models/university/person/involvement.rb
+++ b/app/models/university/person/involvement.rb
@@ -25,11 +25,11 @@
 #  fk_rails_5c704f6338  (university_id => universities.id)
 #
 class University::Person::Involvement < ApplicationRecord
+  include WithUniversity
   include WithPosition
 
   enum kind: { administrator: 10, researcher: 20, teacher: 30 }
 
-  belongs_to :university
   belongs_to :person, class_name: 'University::Person'
   belongs_to :target, polymorphic: true
 
diff --git a/app/models/university/role.rb b/app/models/university/role.rb
index 7868717ae..3e740ac52 100644
--- a/app/models/university/role.rb
+++ b/app/models/university/role.rb
@@ -21,9 +21,9 @@
 #  fk_rails_8e52293a38  (university_id => universities.id)
 #
 class University::Role < ApplicationRecord
+  include WithUniversity
   include WithPosition
 
-  belongs_to :university
   belongs_to :target, polymorphic: true, optional: true
   has_many :involvements, class_name: 'University::Person::Involvement', as: :target, dependent: :destroy, inverse_of: :target
   has_many :people, through: :involvements
diff --git a/app/models/user.rb b/app/models/user.rb
index 0905f39af..7b81885d3 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -54,12 +54,12 @@
 #
 class User < ApplicationRecord
   has_one_attached_deletable :picture  # In this order, "resize avatar" callback will be fired after the others.
+  include WithUniversity
   include WithAuthentication
   include WithPerson
   include WithRoles
   include WithSyncBetweenUniversities
 
-  belongs_to :university
   belongs_to :language
 
   scope :ordered, -> { order(:last_name, :first_name) }
-- 
GitLab