diff --git a/app/controllers/api/schools_controller.rb b/app/controllers/api/schools_controller.rb index 3874e17b4..4e36b654c 100644 --- a/app/controllers/api/schools_controller.rb +++ b/app/controllers/api/schools_controller.rb @@ -65,7 +65,8 @@ def school_params :creator_agree_authority, :creator_agree_terms_and_conditions, :creator_agree_to_ux_contact, - :creator_agree_responsible_safeguarding + :creator_agree_responsible_safeguarding, + :user_origin ) end end diff --git a/app/models/school.rb b/app/models/school.rb index caffbe356..6176bfb78 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -9,6 +9,8 @@ class School < ApplicationRecord VALID_URL_REGEX = %r{\A(?:https?://)?(?:www.)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,63}(\.[a-z]{2,63})*(/.*)?\z}ix + enum :user_origin, %i[for_education experience_cs], default: :for_education, validate: true + validates :name, presence: true validates :website, presence: true, format: { with: VALID_URL_REGEX, message: I18n.t('validations.school.website') } validates :address_line_1, presence: true diff --git a/app/views/api/my_school/show.json.jbuilder b/app/views/api/my_school/show.json.jbuilder index 51a26d598..40f10f263 100644 --- a/app/views/api/my_school/show.json.jbuilder +++ b/app/views/api/my_school/show.json.jbuilder @@ -1,20 +1,3 @@ # frozen_string_literal: true -json.call( - @school, - :id, - :name, - :website, - :reference, - :address_line_1, - :address_line_2, - :municipality, - :administrative_area, - :postal_code, - :country_code, - :code, - :verified_at, - :created_at, - :updated_at -) -json.roles @user.school_roles(@school) +json.partial! '/api/schools/school', school: @school, roles: @user.school_roles(@school), code: true diff --git a/app/views/api/school_owners/_school_owner.json.jbuilder b/app/views/api/school_owners/_school_owner.json.jbuilder index abd71e200..d002c3852 100644 --- a/app/views/api/school_owners/_school_owner.json.jbuilder +++ b/app/views/api/school_owners/_school_owner.json.jbuilder @@ -4,5 +4,4 @@ json.call(owner, :id, :name) json.type('owner') include_email = local_assigns.fetch(:include_email, true) - json.email(owner.email) if include_email diff --git a/app/views/api/schools/_school.json.jbuilder b/app/views/api/schools/_school.json.jbuilder new file mode 100644 index 000000000..7dea9024d --- /dev/null +++ b/app/views/api/schools/_school.json.jbuilder @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +json.call( + school, + :id, + :name, + :website, + :reference, + :address_line_1, + :address_line_2, + :municipality, + :administrative_area, + :postal_code, + :country_code, + :verified_at, + :created_at, + :updated_at +) + +include_roles = local_assigns.fetch(:roles, false) +json.roles(roles) if include_roles + +include_code = local_assigns.fetch(:code, false) +json.code(school.code) if include_code + +include_user_origin = local_assigns.fetch(:user_origin, false) +json.user_origin(school.user_origin) if include_user_origin diff --git a/app/views/api/schools/index.json.jbuilder b/app/views/api/schools/index.json.jbuilder index 564faeab0..688131641 100644 --- a/app/views/api/schools/index.json.jbuilder +++ b/app/views/api/schools/index.json.jbuilder @@ -1,20 +1,5 @@ # frozen_string_literal: true json.array!(@schools) do |school| - json.call( - school, - :id, - :name, - :website, - :reference, - :address_line_1, - :address_line_2, - :municipality, - :administrative_area, - :postal_code, - :country_code, - :verified_at, - :created_at, - :updated_at - ) + json.partial! 'school', school: end diff --git a/app/views/api/schools/show.json.jbuilder b/app/views/api/schools/show.json.jbuilder index 0ee4bfc86..fb913b0cc 100644 --- a/app/views/api/schools/show.json.jbuilder +++ b/app/views/api/schools/show.json.jbuilder @@ -1,18 +1,3 @@ # frozen_string_literal: true -json.call( - @school, - :id, - :name, - :website, - :reference, - :address_line_1, - :address_line_2, - :municipality, - :administrative_area, - :postal_code, - :country_code, - :verified_at, - :created_at, - :updated_at -) +json.partial! 'school', school: @school, user_origin: true diff --git a/db/migrate/20250515081023_add_user_origin_field_to_schools.rb b/db/migrate/20250515081023_add_user_origin_field_to_schools.rb new file mode 100644 index 000000000..7d839b676 --- /dev/null +++ b/db/migrate/20250515081023_add_user_origin_field_to_schools.rb @@ -0,0 +1,5 @@ +class AddUserOriginToSchools < ActiveRecord::Migration[7.1] + def change + add_column :schools, :user_origin, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 9aefee6f7..b9237c649 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2025_04_15_133546) do +ActiveRecord::Schema[7.1].define(version: 2025_05_15_081023) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -270,6 +270,7 @@ t.string "code" t.boolean "creator_agree_to_ux_contact", default: false t.boolean "creator_agree_responsible_safeguarding", default: true + t.integer "user_origin", default: 0 t.index ["code"], name: "index_schools_on_code", unique: true t.index ["creator_id"], name: "index_schools_on_creator_id", unique: true t.index ["reference"], name: "index_schools_on_reference", unique: true diff --git a/spec/features/school/creating_a_school_spec.rb b/spec/features/school/creating_a_school_spec.rb index 7689b039d..fd51863ed 100644 --- a/spec/features/school/creating_a_school_spec.rb +++ b/spec/features/school/creating_a_school_spec.rb @@ -22,7 +22,8 @@ creator_agree_authority: true, creator_agree_terms_and_conditions: true, creator_agree_to_ux_contact: true, - creator_agree_responsible_safeguarding: true + creator_agree_responsible_safeguarding: true, + user_origin: 'for_education' } } end diff --git a/spec/models/school_spec.rb b/spec/models/school_spec.rb index 4f928726b..76ac67470 100644 --- a/spec/models/school_spec.rb +++ b/spec/models/school_spec.rb @@ -242,6 +242,15 @@ school.update(code: '00-00-00') expect(school.errors[:code]).to include('cannot be changed after verification') end + + it 'requires a user_origin' do + school.user_origin = nil + expect(school).to be_invalid + end + + it 'sets the user_origin to for_education by default' do + expect(school.user_origin).to eq('for_education') + end end describe '#creator' do