diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index 9e7c9fa1..c72c66da 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -11,9 +11,16 @@ class SchoolsController < ApplicationController authenticate(district.name.downcase, "#{district.name.downcase}!") @school_categories = @school.school_categories.for_parent_category(@school, nil).valid.sort + @years = @school_categories.map(&:year).map(&:to_i).sort.uniq - @year = (params[:year] || @years.last).to_i + @year = (params[:year] || @years.last || "2018").to_i @years.delete(@year) + + missing_categories = Category.root - @school_categories.map(&:category) + missing_categories.each do |category| + @school_categories << category.school_categories.new(school: @school, year: @year) + end + @school_categories = @school_categories.select { |sc| sc.year.to_i == @year } end diff --git a/app/models/category.rb b/app/models/category.rb index b4e0d9fa..5b2338f5 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -25,6 +25,10 @@ class Category < ApplicationRecord end def root_index + Category.root_identifiers.index(root_identifier) || 0 + end + + def self.root_identifiers [ "teachers-and-the-teaching-environment", "school-culture", @@ -32,7 +36,11 @@ class Category < ApplicationRecord "academic-learning", "community-and-wellbeing", "pilot-family-questions" - ].index(root_identifier) || 0 + ] + end + + def self.root + Category.where(parent_category: nil).select { |c| self.root_identifiers.index(c.slug) } end def custom_zones