Skip to content

Commit

Permalink
Merge pull request #1901 from tactilenews/1900_fix_undefined_method_t…
Browse files Browse the repository at this point in the history
…rucate_on_nil_class

Fix ActionView::Template::Error for message with nil text
  • Loading branch information
mattwr18 authored Jun 13, 2024
2 parents e6dcc73 + 03bca1f commit 97296b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/components/chat_form/chat_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<h3><%= "Zur Frage: #{reply_to.request.title}" %></h3>
<%= hidden_field_tag 'message[reply_to_id]', reply_to&.id %>
<% end %>
<%= c 'textarea', id: 'message[text]', value: reply_to && t('.reply_to_reference', text: reply_to.text.truncate(50)), required: true, placeholder: t('components.chat_form.placeholder') %>
<%= c 'textarea', id: 'message[text]', value: prefilled_value, required: true, placeholder: t('components.chat_form.placeholder') %>
<%= c 'button', label: t('components.chat_form.submit'), type: :submit %>
<% end %>
8 changes: 8 additions & 0 deletions app/components/chat_form/chat_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ def initialize(contributor:, reply_to: nil)
private

attr_reader :contributor, :reply_to

def prefilled_value
return nil unless reply_to

text = reply_to.text.present? ? reply_to.text.truncate(50) : date_time(reply_to.updated_at)

t('.reply_to_reference', text: text)
end
end
end
18 changes: 18 additions & 0 deletions spec/components/chat_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,23 @@
it 'should have the message prefilled' do
expect(subject).to have_css('textarea', text: I18n.t('components.chat_form.reply_to_reference', text: reply_to.text.truncate(50)))
end

context 'message with nil text' do
before { params[:reply_to] = create(:message, text: '') }

it 'should display the time instead' do
expect(subject).to have_css('textarea',
text: I18n.t('components.chat_form.reply_to_reference', text: date_time(reply_to.updated_at)))
end
end

context 'message with nil text' do
before { params[:reply_to] = create(:message, text: nil) }

it 'should display the time instead' do
expect(subject).to have_css('textarea',
text: I18n.t('components.chat_form.reply_to_reference', text: date_time(reply_to.updated_at)))
end
end
end
end

0 comments on commit 97296b8

Please sign in to comment.