Skip to content

Commit

Permalink
Merge pull request #1764 from tactilenews/1754_conditionally_alLow_re…
Browse files Browse the repository at this point in the history
…quest_without_text

Conditionally allow requests without text
  • Loading branch information
mattwr18 authored Dec 15, 2023
2 parents 3f34b10 + cfa20b4 commit a1e8ec2
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
21 changes: 16 additions & 5 deletions app/components/request_form/request_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ export default class extends Controller {
}

updatePreview(event) {
const message = this.setMessage();
const imagePreviewCaption = document.getElementById('caption');
if (imagePreviewCaption) {
imagePreviewCaption.innerHTML = message;
imagePreviewCaption.innerHTML = this.setCaption();
} else {
this.previewTarget.innerHTML = message;
this.previewTarget.innerHTML = this.setMessage();
}

if (event?.target?.files?.length) {
this.previewTarget.innerHTML = '';
this.addImagePreview(event.target.files, message);
this.messageTarget.removeAttribute('required');
this.addImagePreview(event.target.files, this.setCaption());
}
}

Expand Down Expand Up @@ -168,8 +168,9 @@ export default class extends Controller {
'RequestForm-filenamesWrapper--hidden'
);
this.previewTarget.innerHTML = this.setMessage();
this.messageTarget.setAttribute('required', true);
} else {
this.addImagePreview(this.imageInputTarget.files, this.setMessage());
this.addImagePreview(this.imageInputTarget.files, this.setCaption());
}
}

Expand All @@ -180,6 +181,16 @@ export default class extends Controller {
return replacePlaceholder(message, placeholder, 'Max');
}

setCaption() {
const placeholder = 'VORNAME';
const message = sanitize(this.messageTarget.value);
if (message && message.length > 0) {
return replacePlaceholder(message, placeholder, 'Max');
} else {
return message;
}
}

updateFilesname(index, file) {
const listItem = document.createElement('li');
listItem.setAttribute('id', `image-filename-${file.name}`);
Expand Down
2 changes: 1 addition & 1 deletion app/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Request < ApplicationRecord

validates :files, blob: { content_type: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'] }
validates :title, presence: true
validates :text, length: { maximum: 1500 }, presence: true
validates :text, length: { maximum: 1500 }, presence: true, unless: -> { files.attached? }

acts_as_taggable_on :tags

Expand Down
37 changes: 32 additions & 5 deletions spec/models/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
title: 'Hitchhiker’s Guide',
text: 'What is the answer to life, the universe, and everything?',
user: user,
schedule_send_for: Time.current,
files: [fixture_file_upload('example-image.png')]
schedule_send_for: Time.current
)
end

Expand All @@ -31,6 +30,17 @@
before { request.text = '' }

it { is_expected.not_to be_valid }

describe 'with file attached' do
before do
request.files.attach(
io: Rails.root.join('example-image.png').open,
filename: 'example-image.png'
)
end

it { is_expected.to be_valid }
end
end

context 'text shorter than or equal to 1500 chars' do
Expand Down Expand Up @@ -74,8 +84,17 @@
expect(subject.attributes.keys).to include('title', 'text', 'user_id')
end

it 'has files attached' do
expect(subject.files).to be_attached
context 'files' do
before do
request.files.attach(
io: Rails.root.join('example-image.png').open,
filename: 'example-image.png'
)
end

it 'attached' do
expect(subject.files).to be_attached
end
end

it 'is by default sorted in reverse chronological order' do
Expand Down Expand Up @@ -242,8 +261,16 @@
end

describe '::after_create' do
before(:each) { allow(Request).to receive(:broadcast!).and_call_original } # is stubbed for every other test
subject { -> { request.save! } }

before do
request.files.attach(
io: Rails.root.join('example-image.png').open,
filename: 'example-image.png'
)
allow(Request).to receive(:broadcast!).and_call_original # is stubbed for every other test
end

describe 'given some existing contributors in the moment of creation' do
before(:each) do
create(:contributor, id: 1, email: 'somebody@example.org')
Expand Down
15 changes: 15 additions & 0 deletions spec/requests/requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@
expect(flash[:success]).not_to be_empty
end
end

describe 'with no text' do
before { params[:request][:text] = '' }

it 'redirects to requests#show' do
response = subject.call
request = Request.last
expect(response).to redirect_to request
end

it 'shows success notification' do
subject.call
expect(flash[:success]).not_to be_empty
end
end
end

context 'scheduled for future datetime' do
Expand Down
26 changes: 26 additions & 0 deletions spec/system/requests/sending_images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,34 @@
it 'sending a request with image files' do
visit new_request_path(as: user)

# With no text, no file
fill_in 'Titel', with: 'No text, no files'

click_button 'Frage an die Community senden'
message = page.find('textarea[name="request[text]"]').evaluate_script('this.validationMessage')
expect(message).to eq 'Please fill out this field.'
expect(page).to have_current_path(new_request_path(as: user))

# with no text, with file
visit new_request_path(as: user)

fill_in 'Titel', with: 'Message with files, no text'

find_button('Bilder anhängen').trigger('click')
image_file = File.expand_path('../../fixtures/files/example-image.png', __dir__)
find_field('request_files', visible: :all).attach_file(image_file)

click_button 'Frage an die Community senden'
expect(page).to have_content('Message with files, no text')
expect(page).to have_current_path(request_path(Request.first))

# With text
visit new_request_path(as: user)

fill_in 'Titel', with: 'Message with files'
fill_in 'Was möchtest du wissen?', with: 'Did you get my image?'

# Non-image file
click_button 'Bilder anhängen'
image_file = File.expand_path('../../fixtures/files/invalid_profile_picture.pdf', __dir__)
find_field('request_files', visible: :all).attach_file(image_file)
Expand All @@ -33,6 +58,7 @@
expect(page).to have_current_path(new_request_path(as: user))
expect(page).to have_content('Kein gültiges Bildformat. Bitte senden Sie Bilder als jpg, png oder gif.')

# Image file
click_button 'Bilder anhängen'
image_file = File.expand_path('../../fixtures/files/example-image.png', __dir__)
find_field('request_files', visible: :all).attach_file(image_file)
Expand Down

0 comments on commit a1e8ec2

Please sign in to comment.