class DraftSchool
Attributes
Public Class Methods
Source
# File app/models/draft_school.rb, line 21 def initialize( request_session:, current_user:, current_team: nil, **attributes ) @current_user = current_user @current_team = current_team super(request_session:, **attributes) end
Calls superclass method
RequestSessionPersistable::new
Public Instance Methods
Source
# File app/models/draft_school.rb, line 59 def add_school_context? context == "add_school" end
Source
# File app/models/draft_school.rb, line 63 def add_site_context? context == "add_site" end
Source
# File app/models/draft_school.rb, line 36 def address_line_1=(value) super(ApostropheNormaliser.call(value.presence&.normalise_whitespace)) end
Calls superclass method
Source
# File app/models/draft_school.rb, line 40 def address_line_2=(value) super(ApostropheNormaliser.call(value.presence&.normalise_whitespace)) end
Calls superclass method
Source
# File app/models/draft_school.rb, line 130 def address_parts [ address_line_1, address_line_2, address_town, address_postcode ].compact_blank end
Source
# File app/models/draft_school.rb, line 44 def address_town=(value) super(ApostropheNormaliser.call(value.presence&.normalise_whitespace)) end
Calls superclass method
Source
# File app/models/draft_school.rb, line 204 def cannot_remove_year_groups return unless editing? return if selected_year_groups.blank? if (source_location.year_groups - selected_year_groups).present? errors.add(:selected_year_groups, :inclusion) end end
Source
# File app/models/draft_school.rb, line 120 def existing_names return [] if resolved_urn.blank? scope = schools_with_urn scope = scope.where.not(id: editing_id) if editing? scope.pluck(:name) end
Source
# File app/models/draft_school.rb, line 170 def existing_year_groups source_location&.year_groups.presence || source_location&.gias_year_groups || [] end
Source
# File app/models/draft_school.rb, line 192 def human_enum_name(attr) source_location&.human_enum_name(attr) end
Source
# File app/models/draft_school.rb, line 32 def name=(value) super(ApostropheNormaliser.call(value.presence&.normalise_whitespace)) end
Calls superclass method
Source
# File app/models/draft_school.rb, line 161 def next_site_letter return nil if resolved_urn.blank? existing_sites = schools_with_urn.pluck(:site).compact.sort return "B" if existing_sites.empty? existing_sites.max_by { [it.length, it] }.next end
Source
# File app/models/draft_school.rb, line 183 def programmes return source_location.programmes if source_location&.programmes.present? return [] if current_team.nil? current_team.programmes.select do |programme| (programme.default_year_groups & year_groups).any? end end
Source
# File app/models/draft_school.rb, line 139 def readable_attribute_names writable_attribute_names end
Source
# File app/models/draft_school.rb, line 128 def request_session_key = "draft_school" def address_parts [ address_line_1, address_line_2, address_town, address_postcode ].compact_blank end def readable_attribute_names writable_attribute_names end def writable_attribute_names %w[name address_line_1 address_line_2 address_town address_postcode] end def resolved_urn source_location&.urn end def urn_and_site if editing? source_location&.urn_and_site else return nil if resolved_urn.blank? add_school_context? ? resolved_urn : "#{resolved_urn}#{next_site_letter}" end end def next_site_letter return nil if resolved_urn.blank? existing_sites = schools_with_urn.pluck(:site).compact.sort return "B" if existing_sites.empty? existing_sites.max_by { [it.length, it] }.next end def existing_year_groups source_location&.year_groups.presence || source_location&.gias_year_groups || [] end def year_groups selected_year_groups.presence || existing_year_groups end def year_groups=(values) self.selected_year_groups = values&.compact_blank&.map(&:to_i) || [] end def programmes return source_location.programmes if source_location&.programmes.present? return [] if current_team.nil? current_team.programmes.select do |programme| (programme.default_year_groups & year_groups).any? end end def human_enum_name(attr) source_location&.human_enum_name(attr) end def schools_with_urn return [] if resolved_urn.blank? Location.where(urn: resolved_urn) end private def cannot_remove_year_groups return unless editing? return if selected_year_groups.blank? if (source_location.year_groups - selected_year_groups).present? errors.add(:selected_year_groups, :inclusion) end end def school_exists_and_available return if urn.blank? if source_location.nil? errors.add(:urn, "No school found with this URN") return end if source_location.closed? errors.add( :urn, "This school is closed and cannot be added to your team." ) return end sites = Location .where(id: schools_with_urn.map(&:id)) .joins(:team_locations) .where(team_locations: { academic_year: AcademicYear.pending }) if sites.where(team_locations: { team: current_team }).exists? errors.add(:urn, "This school has already been added to your team.") return end if sites.where.not(team_locations: { team: current_team }).exists? errors.add( :urn, "This school is already assigned to another team. Contact the Mavis team if this needs to be changed." ) end end end
Source
# File app/models/draft_school.rb, line 147 def resolved_urn source_location&.urn end
Source
# File app/models/draft_school.rb, line 213 def school_exists_and_available return if urn.blank? if source_location.nil? errors.add(:urn, "No school found with this URN") return end if source_location.closed? errors.add( :urn, "This school is closed and cannot be added to your team." ) return end sites = Location .where(id: schools_with_urn.map(&:id)) .joins(:team_locations) .where(team_locations: { academic_year: AcademicYear.pending }) if sites.where(team_locations: { team: current_team }).exists? errors.add(:urn, "This school has already been added to your team.") return end if sites.where.not(team_locations: { team: current_team }).exists? errors.add( :urn, "This school is already assigned to another team. Contact the Mavis team if this needs to be changed." ) end end
Source
# File app/models/draft_school.rb, line 196 def schools_with_urn return [] if resolved_urn.blank? Location.where(urn: resolved_urn) end
Source
# File app/models/draft_school.rb, line 105 def source_location return Location.find(editing_id) if editing? return nil if add_site_context? && parent_urn_and_site.blank? return nil if add_school_context? && urn.blank? if add_school_context? Location.find_by_urn_and_site(urn.strip) else LocationPolicy::Scope .new(@current_user, Location) .resolve .find_by_urn_and_site(parent_urn_and_site) end end
Returns the source location based on context:
-
When editing: the location being edited
-
When adding school: the school found by URN
-
When adding site: the parent school to add a site to
Source
# File app/models/draft_school.rb, line 151 def urn_and_site if editing? source_location&.urn_and_site else return nil if resolved_urn.blank? add_school_context? ? resolved_urn : "#{resolved_urn}#{next_site_letter}" end end
Source
# File app/models/draft_school.rb, line 48 def wizard_steps [ (:school if add_site_context? && !editing?), (:details if add_site_context? || editing?), (:urn if add_school_context?), (:confirm_urn if add_school_context?), :year_groups, :confirm ].compact end
Source
# File app/models/draft_school.rb, line 143 def writable_attribute_names %w[name address_line_1 address_line_2 address_town address_postcode] end
Source
# File app/models/draft_school.rb, line 175 def year_groups selected_year_groups.presence || existing_year_groups end
Source
# File app/models/draft_school.rb, line 179 def year_groups=(values) self.selected_year_groups = values&.compact_blank&.map(&:to_i) || [] end