From 50e73672ceede0dcf2efb8e64205aa8d201e7197 Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Fri, 9 Aug 2024 16:25:58 -0700 Subject: [PATCH] [#117] Limit locker application department field length to 70 --- app/models/locker_application.rb | 2 ++ app/views/locker_applications/_form.html.erb | 3 ++- .../20240809225155_department_character_limit.rb | 12 ++++++++++++ db/schema.rb | 6 +++--- spec/views/locker_applications/edit.html.erb_spec.rb | 5 +++++ 5 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20240809225155_department_character_limit.rb diff --git a/app/models/locker_application.rb b/app/models/locker_application.rb index 4a3f508..3bfd405 100644 --- a/app/models/locker_application.rb +++ b/app/models/locker_application.rb @@ -14,6 +14,8 @@ class LockerApplication < ApplicationRecord .where('locker_assignments.locker_application_id = locker_applications.id')) } + validates :department_at_application, length: { maximum: 70 } + def self.awaiting_assignment where(complete: true).where.missing(:locker_assignment).order('locker_applications.created_at') end diff --git a/app/views/locker_applications/_form.html.erb b/app/views/locker_applications/_form.html.erb index 08bc803..fcbd137 100644 --- a/app/views/locker_applications/_form.html.erb +++ b/app/views/locker_applications/_form.html.erb @@ -70,7 +70,8 @@ diff --git a/db/migrate/20240809225155_department_character_limit.rb b/db/migrate/20240809225155_department_character_limit.rb new file mode 100644 index 0000000..b2449c2 --- /dev/null +++ b/db/migrate/20240809225155_department_character_limit.rb @@ -0,0 +1,12 @@ +class DepartmentCharacterLimit < ActiveRecord::Migration[7.0] + def up + LockerApplication.where('LENGTH(department_at_application) > 70').each do |application| + application.update(department_at_application: application.department_at_application.truncate(70, separator: /\s/)) + end + change_column :locker_applications, :department_at_application, :string, limit: 70 + end + + def down + change_column :locker_applications, :department_at_application, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 6d33589..8edcddb 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.0].define(version: 2023_03_07_153224) do +ActiveRecord::Schema[7.0].define(version: 2024_08_09_225155) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -35,13 +35,13 @@ t.boolean "accessible" t.string "semester" t.string "status_at_application" - t.string "department_at_application" + t.string "department_at_application", limit: 70 t.bigint "user_id" t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false + t.boolean "archived", default: false t.bigint "building_id", default: 1 t.boolean "complete", default: false - t.boolean "archived", default: false t.text "accessibility_needs", default: [], array: true t.index ["building_id"], name: "index_locker_applications_on_building_id" t.index ["user_id"], name: "index_locker_applications_on_user_id" diff --git a/spec/views/locker_applications/edit.html.erb_spec.rb b/spec/views/locker_applications/edit.html.erb_spec.rb index 973cf7c..591be2c 100644 --- a/spec/views/locker_applications/edit.html.erb_spec.rb +++ b/spec/views/locker_applications/edit.html.erb_spec.rb @@ -42,6 +42,11 @@ end end + it 'limits the length of the department field to 70' do + render + assert_select '#locker_application_department_at_application[maxlength="70"]' + end + context 'with an administrative user' do let(:user) { FactoryBot.create(:user, :admin) }