Skip to content

Commit

Permalink
Handle language being blank (not just nil)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Aug 1, 2024
1 parent d788923 commit ea2081b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
3 changes: 1 addition & 2 deletions app/models/concerns/has_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion app/views/events/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<!-- Field: Language -->
<%= 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') } %>

<!-- Field: Prerequisites -->
<%= f.input :prerequisites, as: :markdown_area, input_html: { rows: '3', title: t('events.hints.prerequisites') } %>
Expand Down
7 changes: 1 addition & 6 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@
</p>
<%= 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) } %>
<!-- Field: description -->
<div class="description">
<%= google_maps_javascript_api_tag(@event) if @event.show_map? %>
Expand Down
24 changes: 24 additions & 0 deletions test/controllers/events_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/models/event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit ea2081b

Please sign in to comment.