diff --git a/app/models/education/program/with_websites.rb b/app/models/education/program/with_websites.rb index a9b81dbe992889487e4afd02768f994c5a8ccb6f..6fe2a46406b1c3f1fbc793ee7d1dd4d220f0d9de 100644 --- a/app/models/education/program/with_websites.rb +++ b/app/models/education/program/with_websites.rb @@ -4,18 +4,29 @@ module Education::Program::WithWebsites included do attr_accessor :skip_websites_categories_callback - after_save_commit :set_websites_categories, unless: :skip_websites_categories_callback - - # FIXME incorrect, forgets websites about programs - has_many :websites, -> { distinct }, - through: :schools + after_save_commit :set_websites_categories, + unless: :skip_websites_categories_callback has_many :website_categories, class_name: 'Communication::Website::Category', dependent: :destroy end + def websites + @websites ||= university.websites.reject do |website| + website_concerned = false + # Site de formation + website_concerned = true if website.about == self + # Site d'école + if website.about&.is_a? Education::School + # Formation dispensée dans l'école + website_concerned = true if self.in? website.about.programs + end + !website_concerned + end + end + def set_websites_categories - websites.find_each(&:set_programs_categories!) + websites.each { |website| website.set_programs_categories! } end end