Skip to content

Commit

Permalink
Ensure presence gets a valid value
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Nov 13, 2023
1 parent 93aa691 commit 1432149
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -495,4 +497,8 @@ def fix_keywords
end
end
end

def presence_default
self.presence = :onsite if presence.blank?
end
end
67 changes: 67 additions & 0 deletions test/models/event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1432149

Please sign in to comment.