From 1432149037291e3f4a57aa3a8e36dd46a5a0ceb0 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Mon, 13 Nov 2023 15:36:31 +0000 Subject: [PATCH] Ensure `presence` gets a valid value --- app/models/event.rb | 6 ++++ test/models/event_test.rb | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/app/models/event.rb b/app/models/event.rb index 0df322578..6e3de6018 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -19,6 +19,7 @@ class Event < ApplicationRecord include WithTimezone before_validation :fix_keywords, on: :create, if: :scraper_record + before_validation :presence_default before_save :check_country_name # :set_default_times before_save :geocoding_cache_lookup, if: :address_will_change? after_save :enqueue_geocoding_worker, if: :address_changed? @@ -124,6 +125,7 @@ class Event < ApplicationRecord validates :latitude, numericality: { greater_than_or_equal_to: -90, less_than_or_equal_to: 90, allow_nil: true } validates :longitude, numericality: { greater_than_or_equal_to: -180, less_than_or_equal_to: 180, allow_nil: true } # validates :duration, format: { with: /\A[0-9][0-9]:[0-5][0-9]\z/, message: "must be in format HH:MM" }, allow_blank: true + validates :presence, inclusion: { in: presences.keys, allow_blank: true } validate :allowed_url clean_array_fields(:keywords, :fields, :event_types, :target_audience, :eligibility, :host_institutions, :sponsors) @@ -495,4 +497,8 @@ def fix_keywords end end end + + def presence_default + self.presence = :onsite if presence.blank? + end end diff --git a/test/models/event_test.rb b/test/models/event_test.rb index 5d6ae708f..e8a382393 100644 --- a/test/models/event_test.rb +++ b/test/models/event_test.rb @@ -571,20 +571,87 @@ class EventTest < ActiveSupport::TestCase end test 'can still set presence through online setter' do + assert @event.valid? assert @event.onsite? refute @event.online? refute @event.hybrid? @event.online = true + assert @event.valid? refute @event.onsite? assert @event.online? refute @event.hybrid? @event.online = false + assert @event.valid? assert @event.onsite? refute @event.online? refute @event.hybrid? + + @event.online = '' + + assert @event.valid? + assert @event.onsite? + refute @event.online? + refute @event.hybrid? + end + + test 'validates presence' do + @event.presence = 'onsite' + + assert @event.valid? + assert @event.onsite? + refute @event.online? + refute @event.hybrid? + + @event.presence = 0 + + assert @event.valid? + assert @event.onsite? + refute @event.online? + refute @event.hybrid? + + @event.presence = :online + + assert @event.valid? + refute @event.onsite? + assert @event.online? + refute @event.hybrid? + + @event.presence = 1 + + assert @event.valid? + refute @event.onsite? + assert @event.online? + refute @event.hybrid? + + @event.presence = 'hybrid' + + assert @event.valid? + refute @event.onsite? + refute @event.online? + assert @event.hybrid? + + @event.presence = nil + + assert @event.valid? + assert @event.onsite? + refute @event.online? + refute @event.hybrid? + + @event.presence = '' + + assert @event.valid? + assert @event.onsite? + refute @event.online? + refute @event.hybrid? + + # TODO: Use enum validation in Rails 7.1 https://github.com/rails/rails/pull/49100 + assert_raises(ArgumentError) do + @event.presence = 'xyz' + refute @event.valid? + end end end