From f49b045a068a2deade9e4fe90f503c567bca7440 Mon Sep 17 00:00:00 2001 From: Dan Halson Date: Mon, 19 May 2025 18:00:31 +0100 Subject: [PATCH 1/6] 543: Add enum to capture user_origin --- app/controllers/api/schools_controller.rb | 3 ++- app/models/school.rb | 2 ++ db/migrate/20250515081023_add_connected_apps_table.rb | 5 +++++ db/schema.rb | 3 ++- spec/models/school_spec.rb | 9 +++++++++ 5 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20250515081023_add_connected_apps_table.rb diff --git a/app/controllers/api/schools_controller.rb b/app/controllers/api/schools_controller.rb index 3874e17b4..6fdf6033c 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: %i[for_education experience_cs] ) 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/db/migrate/20250515081023_add_connected_apps_table.rb b/db/migrate/20250515081023_add_connected_apps_table.rb new file mode 100644 index 000000000..e83fa478a --- /dev/null +++ b/db/migrate/20250515081023_add_connected_apps_table.rb @@ -0,0 +1,5 @@ +class AddConnectedAppsTable < 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/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 From f9fe95040f69c7245e866b79ed1d282d6069ba34 Mon Sep 17 00:00:00 2001 From: Dan Halson Date: Mon, 19 May 2025 18:01:10 +0100 Subject: [PATCH 2/6] 543: Add school template in views --- app/views/api/my_school/show.json.jbuilder | 19 +-------------- .../school_owners/_school_owner.json.jbuilder | 1 - app/views/api/schools/_school.json.jbuilder | 24 +++++++++++++++++++ app/views/api/schools/index.json.jbuilder | 17 +------------ app/views/api/schools/show.json.jbuilder | 17 +------------ 5 files changed, 27 insertions(+), 51 deletions(-) create mode 100644 app/views/api/schools/_school.json.jbuilder 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..c38f64c7f --- /dev/null +++ b/app/views/api/schools/_school.json.jbuilder @@ -0,0 +1,24 @@ +# 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 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..881e0dd9f 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 From 2a2c2556f199c335230bd82cb43efd47b30243f4 Mon Sep 17 00:00:00 2001 From: Dan Halson Date: Mon, 19 May 2025 18:07:09 +0100 Subject: [PATCH 3/6] 543: Derive list from enum values --- app/controllers/api/schools_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/schools_controller.rb b/app/controllers/api/schools_controller.rb index 6fdf6033c..50656d2bb 100644 --- a/app/controllers/api/schools_controller.rb +++ b/app/controllers/api/schools_controller.rb @@ -66,7 +66,7 @@ def school_params :creator_agree_terms_and_conditions, :creator_agree_to_ux_contact, :creator_agree_responsible_safeguarding, - user_origin: %i[for_education experience_cs] + user_origin: School.user_origins.keys ) end end From 9d6c6979d0738c24bf1852bc66055feeb3ba7a84 Mon Sep 17 00:00:00 2001 From: Dan Halson Date: Mon, 19 May 2025 18:15:11 +0100 Subject: [PATCH 4/6] 543: operations that 'show' a school should return the user_origin --- app/views/api/schools/_school.json.jbuilder | 3 +++ app/views/api/schools/show.json.jbuilder | 2 +- spec/features/school/creating_a_school_spec.rb | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/views/api/schools/_school.json.jbuilder b/app/views/api/schools/_school.json.jbuilder index c38f64c7f..7dea9024d 100644 --- a/app/views/api/schools/_school.json.jbuilder +++ b/app/views/api/schools/_school.json.jbuilder @@ -22,3 +22,6 @@ 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/show.json.jbuilder b/app/views/api/schools/show.json.jbuilder index 881e0dd9f..fb913b0cc 100644 --- a/app/views/api/schools/show.json.jbuilder +++ b/app/views/api/schools/show.json.jbuilder @@ -1,3 +1,3 @@ # frozen_string_literal: true -json.partial! 'school', school: @school +json.partial! 'school', school: @school, user_origin: 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 From 9044c24bea338cb55eb5776d8e48cf488684bef6 Mon Sep 17 00:00:00 2001 From: Dan Halson Date: Wed, 21 May 2025 07:41:51 +0100 Subject: [PATCH 5/6] 543: Don't restrict values at the param level --- app/controllers/api/schools_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/schools_controller.rb b/app/controllers/api/schools_controller.rb index 50656d2bb..4e36b654c 100644 --- a/app/controllers/api/schools_controller.rb +++ b/app/controllers/api/schools_controller.rb @@ -66,7 +66,7 @@ def school_params :creator_agree_terms_and_conditions, :creator_agree_to_ux_contact, :creator_agree_responsible_safeguarding, - user_origin: School.user_origins.keys + :user_origin ) end end From ab74f4dd4595b9a0d4f71a8e26dd3f7ed9319d0b Mon Sep 17 00:00:00 2001 From: Dan Halson Date: Fri, 23 May 2025 15:17:02 +0100 Subject: [PATCH 6/6] 543: Rename migration --- ...le.rb => 20250515081023_add_user_origin_field_to_schools.rb} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename db/migrate/{20250515081023_add_connected_apps_table.rb => 20250515081023_add_user_origin_field_to_schools.rb} (58%) diff --git a/db/migrate/20250515081023_add_connected_apps_table.rb b/db/migrate/20250515081023_add_user_origin_field_to_schools.rb similarity index 58% rename from db/migrate/20250515081023_add_connected_apps_table.rb rename to db/migrate/20250515081023_add_user_origin_field_to_schools.rb index e83fa478a..7d839b676 100644 --- a/db/migrate/20250515081023_add_connected_apps_table.rb +++ b/db/migrate/20250515081023_add_user_origin_field_to_schools.rb @@ -1,4 +1,4 @@ -class AddConnectedAppsTable < ActiveRecord::Migration[7.1] +class AddUserOriginToSchools < ActiveRecord::Migration[7.1] def change add_column :schools, :user_origin, :integer, default: 0 end