Skip to content

Commit

Permalink
Adding Javascript to require README (#1044)
Browse files Browse the repository at this point in the history
Also shows existing README on page if one has laready been selected
fixes #966
  • Loading branch information
carolyncole committed Apr 5, 2023
1 parent 4b89664 commit 94d8546
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ def datacite_validate

def readme_select
@work = Work.find(params[:id])
readme = Readme.new(@work)
@readme = readme.file_name
@wizard = true
end

Expand Down
2 changes: 2 additions & 0 deletions app/javascript/entrypoints/pdc/pdc_ui_loader.es6
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import CopytoClipboard from './copy_to_clipboard.es6';
import MaximumFileUpload from './maximum_file_upload.es6';
import EditRequiredFields from './edit_required_fields.es6';
import ReadmeFileUpload from './readme_file_upload.es6';

/* eslint class-methods-use-this: ["error", { "exceptMethods": ["setup_fileupload_validation"] }] */

Expand All @@ -14,5 +15,6 @@ export default class PdcUiLoader {
(new MaximumFileUpload('pre_curation_uploads_added', 'btn-submit')).attach_validation();
(new CopytoClipboard()).attach_copy();
(new EditRequiredFields()).attach_validations();
(new ReadmeFileUpload('patch_readme_file', 'readme-upload')).attach_validation();
}
}
21 changes: 21 additions & 0 deletions app/javascript/entrypoints/pdc/readme_file_upload.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default class ReadmeFileUpload {
constructor(uploadId, saveId, errorId = 'file-error') {
this.upload_element = $(`#${uploadId}`);
this.save_element = document.getElementById(saveId);
this.error_element = document.getElementById(errorId);
}

attach_validation() {
this.upload_element.on('change', this.validate.bind(this));
}

validate() {
if (this.upload_element[0].files.length < 1) {
this.save_element.disabled = true;
this.error_element.innerText = 'You must select a README file';
} else {
this.save_element.disabled = false;
this.error_element.innerText = '';
}
}
}
19 changes: 17 additions & 2 deletions app/models/readme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(work)
def attach(readme_file_param)
return "A README file is required!" if readme_file_param.blank? && blank?
return nil if readme_file_param.blank?
remove_old_readme

extension = File.extname(readme_file_param.original_filename)
if work.s3_query_service.upload_file(io: readme_file_param.to_io, filename: "README#{extension}")
Expand All @@ -22,10 +23,24 @@ def blank?
s3_readme_idx.nil?
end

def file_name
return nil if blank?
file_names[s3_readme_idx]
end

private

def s3_readme_idx
file_names = work.pre_curation_uploads_fast.map(&:filename_display)
file_names.find_index { |file_name| file_name.start_with?("README") }
@s3_readme_idx ||= file_names.find_index { |file_name| file_name.start_with?("README") }
end

def file_names
@file_names ||= work.pre_curation_uploads_fast.map(&:filename_display)
end

def remove_old_readme
return if blank?

work.s3_query_service.delete_s3_object(work.pre_curation_uploads_fast[s3_readme_idx].key)
end
end
5 changes: 4 additions & 1 deletion app/views/works/readme_select.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
<p><%= t('works.form.readme_upload.actions') %></p>
<div id="file-error" class="error_box"></div>
<div class="file-upload">
<% if @readme.present? %>
<p><%= @readme %> was previously uploaded. You will replace it if you select a different file. </p>
<% end %>
<%= f.file_field(:readme_file, multiple: false) %>
</div>

<div class="actions">
<%= link_to t('works.form.readme_upload.go_back'), edit_work_path(@work, wizard: true), class: "btn btn-secondary" %>
<%= f.submit(t('works.form.readme_upload.continue'), class: "btn btn-primary wizard-next-button", id: 'readme-upload') %>
<%= f.submit(t('works.form.readme_upload.continue'), class: "btn btn-primary wizard-next-button", id: 'readme-upload', disabled: @readme.blank?) %>
</div>
<% end %>
</div>
Expand Down
7 changes: 7 additions & 0 deletions spec/controllers/works_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,17 @@
end

describe "#readme_select" do
let(:fake_readme) { instance_double Readme, file_name: "README.txt" }

before do
allow(Readme).to receive(:new).and_return(fake_readme)
end

it "renders view to upload the readme" do
sign_in user
get :readme_select, params: { id: work.id }
expect(response).to render_template(:readme_select)
expect(assigns[:readme]).to eq("README.txt")
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/models/readme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,15 @@
expect(fake_s3_service).to have_received(:upload_file).with(io: uploaded_file.to_io, filename: "README.csv")
end
end

context "there is an existing readme that should be replaced" do
let(:s3_files) { [FactoryBot.build(:s3_file, work: work), FactoryBot.build(:s3_readme, work: work)] }

it "returns no error message" do
expect(readme.attach(uploaded_file)).to be_nil
expect(fake_s3_service).to have_received(:upload_file).with(io: uploaded_file.to_io, filename: "README.csv")
expect(fake_s3_service).to have_received(:delete_s3_object).with(s3_files.last.key)
end
end
end
end
5 changes: 2 additions & 3 deletions spec/system/work_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@
expect(page).to have_content(description)
click_on "Save Work"
expect(page).to have_content("Please upload the README")
click_on "Continue"
expect(page).to have_content("A README file is required")
click_on "Continue"
expect(page).to have_button("Continue", disabled: true)
path = Rails.root.join("spec", "fixtures", "files", "orcid.csv")
attach_file(path) do
page.find("#patch_readme_file").click
Expand All @@ -98,6 +96,7 @@

click_on "Back"
expect(page).to have_content("Please upload the README")
expect(page).to have_content("README.txt was previously uploaded. You will replace it if you select a different file.")
click_on "Continue"
page.find(:xpath, "//input[@value='file_other']").choose
click_on "Continue"
Expand Down

0 comments on commit 94d8546

Please sign in to comment.