-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Handle both .csv & .xlsx file uploads * Fixes bs-custom-file-input UI filename issue as per: bootstrap-ruby/bootstrap_form#528 * Add validation to ensure file contains at least one facility
- Loading branch information
Showing
22 changed files
with
641 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$(document).ready(function () { | ||
bsCustomFileInput.init(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module FileUploadable | ||
extend ActiveSupport::Concern | ||
|
||
VALID_MIME_TYPES = %w[text/csv application/vnd.openxmlformats-officedocument.spreadsheetml.sheet].freeze | ||
|
||
def read_xlsx_or_csv_file(file) | ||
file_contents = '' | ||
if file.content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | ||
xlsx = Roo::Spreadsheet.open(file.path) | ||
file_contents = xlsx.to_csv | ||
elsif file.content_type == 'text/csv' | ||
file_contents = file.read | ||
end | ||
end | ||
|
||
def initialize_upload | ||
@errors = [] | ||
@file = params.require(:file) | ||
end | ||
|
||
def validate_file_type | ||
@errors << 'File type not supported, please upload a csv or xlsx file instead' if | ||
VALID_MIME_TYPES.exclude?(@file.content_type) | ||
end | ||
|
||
def validate_file_size | ||
@errors << 'File is too big, must be smaller than 5MB' if @file.size > 5.megabytes | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class ImportFacilitiesJob < ApplicationJob | ||
queue_as :default | ||
self.queue_adapter = :sidekiq | ||
|
||
def perform(facilities) | ||
|
||
import_facilities = [] | ||
facilities.each do |facility| | ||
organization = Organization.find_by(name: facility[:organization_name]) | ||
facility_group = FacilityGroup.find_by(name: facility[:facility_group_name], | ||
organization_id: organization.id) | ||
import_facility = Facility.new(facility.merge!(facility_group_id: facility_group.id)) | ||
import_facilities << import_facility | ||
end | ||
Facility.import!(import_facilities, validate: true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,10 @@ def graphics? | |
show? | ||
end | ||
|
||
def upload? | ||
user.owner? | ||
end | ||
|
||
private | ||
|
||
def destroyable? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<h1>Upload Facilities</h1> | ||
<% if @errors.present? %> | ||
<div class="alert alert-warning"> | ||
<p>Please fix the errors below and try again:</p> | ||
<ul> | ||
<% @errors.each do |error| %> | ||
<li> <%= error %> </li> | ||
<% end %> | ||
</ul> | ||
<p>You can also contact [email protected] for assistance</p> | ||
</div> | ||
<% end %> | ||
<h3>Instructions:</h3> | ||
<ol> | ||
<li>Download and fill in <%= link_to 'this template', '/documents/upload_facilities.csv'%> to create multiple facilities via file upload.</li> | ||
<li>Organizations for the facilities must already exist. | ||
<% if policy(Organization).new? %> | ||
Click here to create an organization: | ||
<%= link_to '+ Organization', new_admin_organization_path, class: "btn btn-sm btn-outline-primary" %> | ||
<% end %></li> | ||
<li>Facility Groups for the facilities must already exist. | ||
<% if policy(FacilityGroup).new? %> | ||
Click here to create a facility group: | ||
<%= link_to '+ Facility group', new_admin_facility_group_path, class: "btn btn-sm btn-outline-primary" %> | ||
<% end %></li> | ||
<li>Ensure that the Organization (Column A) and Facility Group (Column B) are entered correctly.</li> | ||
</ol> | ||
<%= bootstrap_form_tag(url: upload_admin_facilities_url, multipart: true) do |f| %> | ||
<%= f.file_field :upload_facilities_file, required: true, accept: '.csv, .xlsx', label:'Upload your filled in facilities file'%> | ||
<%= f.primary 'Upload' %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
organization,facility_group,facility_name,facility_type,street_address (optional),village_or_colony (optional),district,state,country,pin (optional),longitude (optional),latitude (optional) |
Oops, something went wrong.