diff --git a/app/models/education/school.rb b/app/models/education/school.rb index 93f6aac57c9da709065e24d5655f7b2a30804ddc..8902cb9613fb08eca9fd9074b806c3469d32d87c 100644 --- a/app/models/education/school.rb +++ b/app/models/education/school.rb @@ -26,18 +26,9 @@ class Education::School < ApplicationRecord include WithGit include Aboutable - - has_and_belongs_to_many :programs, - class_name: 'Education::Program', - join_table: 'education_programs_schools', - foreign_key: 'education_school_id', - association_foreign_key: 'education_program_id' - has_and_belongs_to_many :published_programs, - -> { published }, - class_name: 'Education::Program', - join_table: 'education_programs_schools', - foreign_key: 'education_school_id', - association_foreign_key: 'education_program_id' + include WithTeam + include WithPrograms # must come before WithAlumni + include WithAlumni belongs_to :university @@ -45,59 +36,11 @@ class Education::School < ApplicationRecord class_name: 'Communication::Website', as: :about, dependent: :nullify - has_many :university_roles, - class_name: 'University::Role', - as: :target, - dependent: :destroy - has_many :involvements_through_roles, - through: :university_roles, - source: :involvements - has_many :university_people_through_role_involvements, - through: :involvements_through_roles, - source: :person - has_many :university_people_through_program_involvements, - through: :programs, - source: :university_people_through_involvements - has_many :university_people_through_program_role_involvements, - through: :programs, - source: :university_people_through_role_involvements - has_many :university_people_through_published_program_involvements, - through: :published_programs, - source: :university_people_through_involvements - has_many :university_people_through_published_program_role_involvements, - through: :published_programs, - source: :university_people_through_role_involvements - - has_many :alumni, -> { distinct }, - through: :programs - alias_attribute :university_person_alumni, :alumni - - has_many :alumni_experiences, -> { distinct }, - class_name: 'University::Person::Experience', - through: :alumni, - source: :experiences - alias_attribute :university_person_experiences, :alumni_experiences - - has_many :alumni_organizations, -> { distinct }, - class_name: 'University::Organization', - through: :alumni_experiences, - source: :organization - alias_attribute :university_person_alumni_organizations, :alumni_organizations - - has_many :academic_years, -> { distinct }, - class_name: 'Education::AcademicYear', - through: :programs - alias_attribute :education_academic_years, :academic_years - has_many :education_cohorts, -> { distinct }, - class_name: 'Education::Cohort', - through: :programs - alias_attribute :cohorts, :education_cohorts validates :name, :address, :city, :zipcode, :country, presence: true scope :ordered, -> { order(:name) } - scope :for_program, -> (program_id) { joins(:programs).where(education_programs: { id: program_id }) } scope :for_search_term, -> (term) { where(" unaccent(education_schools.address) ILIKE unaccent(:term) OR @@ -108,65 +51,37 @@ class Education::School < ApplicationRecord unaccent(education_schools.zipcode) ILIKE unaccent(:term) ", term: "%#{sanitize_sql_like(term)}%") } + scope :for_program, -> (program_id) { + joins(:programs).where(education_programs: { id: program_id }) + } def to_s "#{name}" end - def researchers - people_ids = ( - university_people_through_published_program_involvements + - university_people_through_role_involvements + - university_people_through_published_program_role_involvements - ).pluck(:id) - university.people.where(id: people_ids, is_researcher: true) - end - - def teachers - people_ids = university_people_through_published_program_involvements.pluck(:id) - university.people.where(id: people_ids, is_teacher: true) - end - - def administrators - people_ids = ( - university_people_through_role_involvements + - university_people_through_published_program_role_involvements - ).pluck(:id) - university.people.where(id: people_ids, is_administration: true) - end - def git_path(website) "data/school.yml" end def git_dependencies(website) dependencies = [self] - dependencies += published_programs + published_programs.map(&:active_storage_blobs).flatten if has_education_programs? - dependencies += teachers + teachers.map(&:teacher) + teachers.map(&:active_storage_blobs).flatten if has_teachers? - dependencies += researchers + researchers.map(&:researcher) + researchers.map(&:active_storage_blobs).flatten if has_researchers? - dependencies += administrators + administrators.map(&:administrator) + administrators.map(&:active_storage_blobs).flatten if has_administrators? + dependencies += published_programs + + published_programs.map(&:active_storage_blobs).flatten if has_education_programs? + dependencies += teachers + + teachers.map(&:teacher) + + teachers.map(&:active_storage_blobs).flatten if has_teachers? + dependencies += researchers + + researchers.map(&:researcher) + + researchers.map(&:active_storage_blobs).flatten if has_researchers? + dependencies += administrators + + administrators.map(&:administrator) + + administrators.map(&:active_storage_blobs).flatten if has_administrators? dependencies end ##################### # Aboutable methods # ##################### - def has_administrators? - university_people_through_role_involvements.any? || - university_people_through_program_role_involvements.any? - end - - def has_researchers? - researchers.any? - end - - def has_teachers? - teachers.any? - end - - def has_education_programs? - published_programs.any? - end def has_research_articles? false diff --git a/app/models/education/school/with_alumni.rb b/app/models/education/school/with_alumni.rb new file mode 100644 index 0000000000000000000000000000000000000000..a234267c35ea0eeeca54642dafa75b2bdf73810a --- /dev/null +++ b/app/models/education/school/with_alumni.rb @@ -0,0 +1,32 @@ +module Education::School::WithAlumni + extend ActiveSupport::Concern + + included do + + has_many :alumni, -> { distinct }, + through: :programs + alias_attribute :university_person_alumni, :alumni + + has_many :alumni_experiences, -> { distinct }, + class_name: 'University::Person::Experience', + through: :alumni, + source: :experiences + alias_attribute :university_person_experiences, :alumni_experiences + + has_many :alumni_organizations, -> { distinct }, + class_name: 'University::Organization', + through: :alumni_experiences, + source: :organization + alias_attribute :university_person_alumni_organizations, :alumni_organizations + + has_many :academic_years, -> { distinct }, + class_name: 'Education::AcademicYear', + through: :programs + alias_attribute :education_academic_years, :academic_years + + has_many :cohorts, -> { distinct }, + class_name: 'Education::Cohort', + through: :programs + alias_attribute :education_cohorts, :cohorts + end +end diff --git a/app/models/education/school/with_programs.rb b/app/models/education/school/with_programs.rb new file mode 100644 index 0000000000000000000000000000000000000000..836aab7fec8dc65ee44c1e3245710766400504e4 --- /dev/null +++ b/app/models/education/school/with_programs.rb @@ -0,0 +1,24 @@ +module Education::School::WithPrograms + extend ActiveSupport::Concern + + included do + has_and_belongs_to_many :programs, + class_name: 'Education::Program', + join_table: 'education_programs_schools', + foreign_key: 'education_school_id', + association_foreign_key: 'education_program_id' + + # Why not programs.published ? + has_and_belongs_to_many :published_programs, + -> { published }, + class_name: 'Education::Program', + join_table: 'education_programs_schools', + foreign_key: 'education_school_id', + association_foreign_key: 'education_program_id' + + end + + def has_education_programs? + published_programs.any? + end +end diff --git a/app/models/education/school/with_team.rb b/app/models/education/school/with_team.rb new file mode 100644 index 0000000000000000000000000000000000000000..826b8a6b550762161d5ffb04e532d32cb30140e7 --- /dev/null +++ b/app/models/education/school/with_team.rb @@ -0,0 +1,70 @@ +module Education::School::WithTeam + extend ActiveSupport::Concern + + included do + has_many :university_roles, + class_name: 'University::Role', + as: :target, + dependent: :destroy + + has_many :involvements_through_roles, + through: :university_roles, + source: :involvements + + has_many :university_people_through_role_involvements, + through: :involvements_through_roles, + source: :person + + has_many :university_people_through_program_involvements, + through: :programs, + source: :university_people_through_involvements + + has_many :university_people_through_program_role_involvements, + through: :programs, + source: :university_people_through_role_involvements + + has_many :university_people_through_published_program_involvements, + through: :published_programs, + source: :university_people_through_involvements + + has_many :university_people_through_published_program_role_involvements, + through: :published_programs, + source: :university_people_through_role_involvements + end + + def researchers + people_ids = ( + university_people_through_published_program_involvements + + university_people_through_role_involvements + + university_people_through_published_program_role_involvements + ).pluck(:id) + university.people.where(id: people_ids, is_researcher: true) + end + + def teachers + people_ids = university_people_through_published_program_involvements.pluck(:id) + university.people.where(id: people_ids, is_teacher: true) + end + + def administrators + people_ids = ( + university_people_through_role_involvements + + university_people_through_published_program_role_involvements + ).pluck(:id) + university.people.where(id: people_ids, is_administration: true) + end + + + def has_administrators? + university_people_through_role_involvements.any? || + university_people_through_program_role_involvements.any? + end + + def has_researchers? + researchers.any? + end + + def has_teachers? + teachers.any? + end +end