Skip to content
Snippets Groups Projects
Unverified Commit 750c17ed authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

wip scope for program

parent 23df742a
No related branches found
No related tags found
No related merge requests found
...@@ -91,7 +91,7 @@ class Education::School < ApplicationRecord ...@@ -91,7 +91,7 @@ class Education::School < ApplicationRecord
validates :name, :address, :city, :zipcode, :country, presence: true validates :name, :address, :city, :zipcode, :country, presence: true
scope :ordered, -> { order(:name) } scope :ordered, -> { order(:name) }
scope :for_program, -> (program_id) { } # TODO @Sebou scope :for_program, -> (program_id) { joins(:programs).where(education_programs: { id: program_id }) }
scope :for_search_term, -> (term) { scope :for_search_term, -> (term) {
where(" where("
unaccent(education_schools.address) ILIKE unaccent(:term) OR unaccent(education_schools.address) ILIKE unaccent(:term) OR
......
...@@ -44,6 +44,7 @@ class University::Person < ApplicationRecord ...@@ -44,6 +44,7 @@ class University::Person < ApplicationRecord
include WithBlobs include WithBlobs
include WithSlug include WithSlug
include WithPicture include WithPicture
include WithRoles
include WithEducation include WithEducation
LIST_OF_ROLES = [ LIST_OF_ROLES = [
...@@ -77,11 +78,6 @@ class University::Person < ApplicationRecord ...@@ -77,11 +78,6 @@ class University::Person < ApplicationRecord
class_name: 'University::Person::Involvement', class_name: 'University::Person::Involvement',
dependent: :destroy dependent: :destroy
has_many :involvements_as_administrator,
-> { where(kind: 'administrator') },
class_name: 'University::Person::Involvement',
dependent: :destroy
has_many :author_websites, has_many :author_websites,
-> { distinct }, -> { distinct },
through: :communication_website_posts, through: :communication_website_posts,
...@@ -117,7 +113,16 @@ class University::Person < ApplicationRecord ...@@ -117,7 +113,16 @@ class University::Person < ApplicationRecord
scope :researchers, -> { where(is_researcher: true) } scope :researchers, -> { where(is_researcher: true) }
scope :alumni, -> { where(is_alumnus: true) } scope :alumni, -> { where(is_alumnus: true) }
scope :for_role, -> (role) { where("is_#{role}": true) } scope :for_role, -> (role) { where("is_#{role}": true) }
scope :for_program, -> (program_id) { } # TODO @Sebou scope :for_program, -> (program_id) {
left_joins(:education_programs_as_administrator, :education_programs_as_teacher)
.where(education_programs: { id: program_id })
.or(
left_joins(:education_programs_as_administrator, :education_programs_as_teacher)
.where(education_programs_as_teachers_university_people: { id: program_id })
)
.select("university_people.*")
.distinct
}
scope :for_search_term, -> (term) { scope :for_search_term, -> (term) {
where(" where("
unaccent(concat(university_people.first_name, ' ', university_people.last_name)) ILIKE unaccent(:term) OR unaccent(concat(university_people.first_name, ' ', university_people.last_name)) ILIKE unaccent(:term) OR
......
...@@ -32,6 +32,10 @@ class University::Person::Involvement < ApplicationRecord ...@@ -32,6 +32,10 @@ class University::Person::Involvement < ApplicationRecord
belongs_to :person, class_name: 'University::Person' belongs_to :person, class_name: 'University::Person'
belongs_to :target, polymorphic: true belongs_to :target, polymorphic: true
belongs_to :university_role,
-> { where(university_person_involvements: { target_type: 'University::Role' }) },
class_name: "University::Role",
foreign_key: "target_id"
validates :person_id, uniqueness: { scope: [:target_id, :target_type] } validates :person_id, uniqueness: { scope: [:target_id, :target_type] }
validates :target_id, uniqueness: { scope: [:person_id, :target_type] } validates :target_id, uniqueness: { scope: [:person_id, :target_type] }
......
...@@ -12,6 +12,12 @@ module University::Person::WithEducation ...@@ -12,6 +12,12 @@ module University::Person::WithEducation
source: :target, source: :target,
source_type: "Education::Program" source_type: "Education::Program"
has_many :education_programs_as_administrator,
-> { distinct },
through: :roles_as_administrator,
source: :target,
source_type: "Education::Program"
has_many :experiences has_many :experiences
has_and_belongs_to_many :cohorts, has_and_belongs_to_many :cohorts,
...@@ -30,13 +36,6 @@ module University::Person::WithEducation ...@@ -30,13 +36,6 @@ module University::Person::WithEducation
association_foreign_key: 'education_program_id' association_foreign_key: 'education_program_id'
end end
def education_programs_as_administrator
university.education_programs
.joins(:involvements_through_roles)
.where(university_person_involvements: { person_id: id })
.distinct
end
def add_to_cohort(cohort) def add_to_cohort(cohort)
cohorts << cohort unless cohort.in?(cohorts) cohorts << cohort unless cohort.in?(cohorts)
diploma_years << cohort.academic_year unless cohort.academic_year.in? diploma_years diploma_years << cohort.academic_year unless cohort.academic_year.in? diploma_years
......
module University::Person::WithRoles
extend ActiveSupport::Concern
included do
has_many :involvements_as_administrator,
-> { where(kind: 'administrator') },
class_name: 'University::Person::Involvement',
dependent: :destroy
has_many :roles_as_administrator,
through: :involvements_as_administrator,
source: :target,
source_type: "University::Role"
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment