From ea2081b36f475a69a93fcf80a5f7702995cd018a Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Thu, 1 Aug 2024 14:04:11 +0100 Subject: [PATCH] Handle `language` being blank (not just nil) --- app/models/concerns/has_language.rb | 3 +-- app/views/events/_form.html.erb | 2 +- app/views/events/show.html.erb | 7 +------ test/controllers/events_controller_test.rb | 24 ++++++++++++++++++++++ test/models/event_test.rb | 4 ++++ 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/models/concerns/has_language.rb b/app/models/concerns/has_language.rb index 00a94a322..3885b26c0 100644 --- a/app/models/concerns/has_language.rb +++ b/app/models/concerns/has_language.rb @@ -3,8 +3,7 @@ module HasLanguage extend ActiveSupport::Concern included do - validates :language, controlled_vocabulary: { dictionary: 'LanguageDictionary', - allow_nil: true } + validates :language, controlled_vocabulary: { dictionary: 'LanguageDictionary', allow_blank: true } if TeSS::Config.solr_enabled # :nocov: diff --git a/app/views/events/_form.html.erb b/app/views/events/_form.html.erb index a0a6d0ff8..726cf424f 100644 --- a/app/views/events/_form.html.erb +++ b/app/views/events/_form.html.erb @@ -52,7 +52,7 @@ <%= f.input :language, collection: LanguageDictionary.instance.options_for_select, - prompt: t('events.prompts.language'), input_html: { title: t('events.hints.language') } %> + prompt: t('events.prompts.language'), include_blank: true, input_html: { title: t('events.hints.language') } %> <%= f.input :prerequisites, as: :markdown_area, input_html: { rows: '3', title: t('events.hints.prerequisites') } %> diff --git a/app/views/events/show.html.erb b/app/views/events/show.html.erb index f54b19c00..5ad4ae5a4 100644 --- a/app/views/events/show.html.erb +++ b/app/views/events/show.html.erb @@ -63,12 +63,7 @@

<%= display_attribute(@event, :timezone) %> <%= display_attribute(@event, :duration) %> - <% if @event.language %> - <%= display_attribute(@event, :language) do |value| - render_language_name(value) - end %> - <% end %> - + <%= display_attribute(@event, :language) { |value| render_language_name(value) } %>
<%= google_maps_javascript_api_tag(@event) if @event.show_map? %> diff --git a/test/controllers/events_controller_test.rb b/test/controllers/events_controller_test.rb index b29b42f2b..66ac7305d 100644 --- a/test/controllers/events_controller_test.rb +++ b/test/controllers/events_controller_test.rb @@ -1545,4 +1545,28 @@ class EventsControllerTest < ActionController::TestCase get :show, params: { id: event } assert_response :forbidden end + + test 'should create event without language specified' do + sign_in users(:regular_user) + assert_difference('Event.count', 1) do + post :create, params: { event: { description: @event.description, title: @event.title, url: @event.url, + language: '' } } + end + assert_redirected_to event_path(assigns(:event)) + refute assigns(:event).language.present? + end + + test 'should display language of instruction' do + get :show, params: { id: events(:one) } + assert_response :success + assert_select 'strong', text: 'Language of instruction:' + end + + test 'should not display language of instruction if not specified' do + event = users(:regular_user).events.create!(title: 'No language', url: 'https://example.com/nolang', language: '') + + get :show, params: { id: event } + assert_response :success + assert_select 'strong', text: 'Language of instruction:', count: 0 + end end diff --git a/test/models/event_test.rb b/test/models/event_test.rb index 06d861609..1761061c0 100644 --- a/test/models/event_test.rb +++ b/test/models/event_test.rb @@ -411,6 +411,10 @@ class EventTest < ActiveSupport::TestCase event.language = nil assert event.valid? + # Okay if blank + event.language = '' + assert event.valid? + # Not okay if not a known ISO-639-2 code event.language = 'yo' refute event.valid?