Skip to content

Commit

Permalink
Merge branch 'release-1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiomarrocoli committed Oct 26, 2022
2 parents 76e6d32 + cda89fe commit f7e5d22
Show file tree
Hide file tree
Showing 29 changed files with 2,241 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ruby 2.3.1
bundler 1.17.3
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 1.0.0

Site has been released for a long time, but starting at 1.0.0 for the purposes of adding a change log

- use date_discovery for date filtering instead of created_at
- rake task to fix non-breaking spaces and  character in database
- process strings during import to prevent the above issue
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ gem 'slack-notifier', '~> 1.5.1'
# Authentication and Authorization
gem 'cancancan', '~> 1.10'
gem 'devise', '~> 4.2.0'
gem 'bcrypt', '~> 3.1.17' # Required on Ubuntu 20+

# Background jobs
gem 'redis', '~> 3.3.1'
Expand Down Expand Up @@ -76,3 +77,5 @@ end
gem 'web-console', '~> 2.0', group: :development
gem 'byebug', group: [:development, :test]
gem 'annotate', '~> 2.7.1'

gem "activerecord-session_store", "~> 1.1"
13 changes: 11 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ GEM
activemodel (= 4.2.6)
activesupport (= 4.2.6)
arel (~> 6.0)
activerecord-session_store (1.1.3)
actionpack (>= 4.0)
activerecord (>= 4.0)
multi_json (~> 1.11, >= 1.11.2)
rack (>= 1.5.2, < 3)
railties (>= 4.0)
activesupport (4.2.6)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
Expand All @@ -53,7 +59,7 @@ GEM
rack
thread_safe
arel (6.0.3)
bcrypt (3.1.11)
bcrypt (3.1.18)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
browserify-rails (3.1.0)
Expand Down Expand Up @@ -150,6 +156,7 @@ GEM
minitest (5.9.0)
mocha (1.1.0)
metaclass (~> 0.0.1)
multi_json (1.15.0)
net-scp (1.2.1)
net-ssh (>= 2.6.5)
net-ssh (3.2.0)
Expand Down Expand Up @@ -255,8 +262,10 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord-session_store (~> 1.1)
annotate (~> 2.7.1)
appsignal (~> 1.1.9)
bcrypt (~> 3.1.17)
browserify-rails (~> 3.1.0)
byebug
cancancan (~> 1.10)
Expand Down Expand Up @@ -297,4 +306,4 @@ DEPENDENCIES
yard (~> 0.8.7.6)

BUNDLED WITH
1.12.5
1.17.3
71 changes: 54 additions & 17 deletions app/controllers/bulk_uploads_controller.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,87 @@
# frozen_string_literal: true

class BulkUploadsController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource

def show
end
def show; end

def index
@bulk_uploads = BulkUpload.includes(:reports).order(:created_at).page(params[:page])
end

def new
end
def new; end

def edit
@bulk_upload = BulkUpload.find(params[:id]) or raise_404
(@bulk_upload = BulkUpload.find(params[:id])) || raise_404
end

def update
redirect_to(:back) and return if params[:file].nil?
redirect_to(:back) && return if params[:file].nil?

result = CsvImporter.import(params[:file].path)
@bulk_upload = BulkUpload.find(params[:id]) or raise_404
(@bulk_upload = BulkUpload.find(params[:id])) || raise_404
@bulk_upload.update(result)

if result[:successful]
redirect_to @bulk_upload, flash: {success: t("bulk_uploads.upload_successful")}
redirect_to @bulk_upload, flash: { success: t('bulk_uploads.upload_successful') }
else
redirect_to @bulk_upload, flash: {error: t("bulk_uploads.upload_error")}
redirect_to @bulk_upload, flash: { error: t('bulk_uploads.upload_error') }
end
end

def create
redirect_to(:back) and return if params[:file].nil?
redirect_to(:back) && return unless params[:files].present?

result = CsvImporter.import(params[:file].path)
if result[:successful]
redirect_to BulkUpload.create(result), flash: {success: t("bulk_uploads.upload_successful")}
else
redirect_to BulkUpload.create(result), flash: {error: t("bulk_uploads.upload_error")}
results = [] # store of all this BulkUpload's reports (successful and not)
blocking_files = [] # store of this BulkUpload's invalid files ( { filename:, error_messages: } )

ActiveRecord::Base.transaction do
params[:files].each do |file|
import = CsvImporter.import file.path
results << import

next if import[:successful]

blocking_files.push({
filename: file.original_filename,
error_messages: import[:happy_accidents].map { |hash| hash[:message] }.uniq
})
end

raise ActiveRecord::Rollback unless blocking_files.empty?

# Only reached if no files are 'blocking'
results.each { |_result| BulkUpload.create results }
return redirect_to bulk_uploads_path, flash: { success: t('bulk_uploads.upload_successful') }
end

error_message = generate_bulk_upload_error_message(blocking_files)

redirect_to bulk_uploads_path, flash: { error: error_message }
end

def destroy
bulk_upload = BulkUpload.find(params[:id]) or raise_404
(bulk_upload = BulkUpload.find(params[:id])) || raise_404
bulk_upload.destroy

redirect_to action: :index, notice: "That bulk upload has been successfully deleted from the system. Thank you!"
redirect_to action: :index, notice: 'That bulk upload has been successfully deleted from the system. Thank you!'
end

private

def generate_bulk_upload_error_message(blocking_files)
error_message = t('bulk_uploads.upload_error')
tab = "&nbsp;" * 4


blocking_files.each do |file|
error_message += [
file[:filename],
file[:error_messages].map { |message| tab + message }.flatten
].join("<br>")
end

error_message
end
end
12 changes: 12 additions & 0 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def lock

def search
@countries = CountryUtilities.all_countries

# Find date of earliest record, to limit the date range filters.
report_dates = Report.pluck("data -> 'answers' -> 'date_of_discovery' -> 'selected'").compact
report_years = report_dates.map { |report_date| report_date.split('/').last }

@earliest_report_year = report_years.present? ? report_years.uniq.min.to_i : 1950
end

def export
Expand All @@ -117,6 +123,12 @@ def validate
def thank_you
end

def destroy
Report.find(params['id']).destroy
flash[:success] = "The report with ID #{params['id']} has been successfully deleted from the system."
redirect_to action: :index
end

private
def report_params
{data: params.require(:report)[:data]}
Expand Down
12 changes: 12 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ def current_controller?(url)
def display_or_default(data, default="N/A")
data.present? ? data : default
end

# Bulk Upload index errors should persist so users can see the errors in their bulk upload
def flash_should_persist?
return false unless controller_name == 'bulk_uploads' && action_name == 'index'
return false unless flash.present?

begin
flash.any? { |key, val| key == 'error' }
rescue
false
end
end
end
2 changes: 1 addition & 1 deletion app/models/bulk_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
#

class BulkUpload < ActiveRecord::Base
has_many :reports
has_many :reports, dependent: :destroy
end
7 changes: 5 additions & 2 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Report < ActiveRecord::Base
belongs_to :bulk_upload
has_many :validations, dependent: :destroy
has_many :images, dependent: :destroy

validates :user_id, presence: true

def user_name
"#{user&.first_name} #{user&.last_name}".strip
Expand All @@ -43,8 +45,9 @@ def can_be_validated?
state&.downcase == "submitted"
end

# Rescue required because of invalid dates in database.
CONVERSIONS = {
"date_of_discovery" => lambda { |value| Date.strptime(value, "%d/%m/%Y") }
"date_of_discovery" => lambda { |value| Date.strptime(value, "%d/%m/%Y") rescue nil }
}

def answer_to question, page=nil, tab_index=0
Expand Down Expand Up @@ -88,7 +91,7 @@ def self.search(params)
query = self
query = SearchBuilder.by_report_id(query, params)
query = SearchBuilder.by_country_of_discovery(query, params)
query = SearchBuilder.by_date_created_range(query, params)
query = SearchBuilder.by_date_of_discovery_range(query, params)
query = SearchBuilder.by_agencies(query, params)
query = SearchBuilder.by_genus(query, params)
query = SearchBuilder.by_users(query, params)
Expand Down
2 changes: 1 addition & 1 deletion app/views/bulk_uploads/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<%= form_tag bulk_uploads_path, multipart: true do %>
<fieldset class="form__group">
<%= file_field_tag "file", required: true %>
<%= file_field_tag "files[]", multiple: true, required: true %>
</fieldset>

<%= submit_tag "Upload file", class: "button button-primary" %>
Expand Down
4 changes: 3 additions & 1 deletion app/views/layouts/_flash_notification.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
<div class="alert alert-<%= key %> <%= 'js-stay' if flash_should_persist? %>"
<% unless flash_should_persist? %>
<p class="alert__close"><i class="fa fa-close"></i> Close</p>
<% end %>
<h4 class="alert__title">
<i class="fa fa-<%= alert_key_to_fa_icon(key) %>"></i> <%= key.titleize %>!
</h4>
Expand Down
6 changes: 6 additions & 0 deletions app/views/reports/_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@
<a href="<%= report_summary_path(report.id) %>">View</a>
<% end %>
<% end %>

<% if can? :update, report %>
<a href="<%= edit_report_path(report.id) %>">Edit</a>
<% end %>

<% if can?(:validate, report) && report.can_be_validated? %>
<a href="<%= validate_report_path(report.id) %>">Validate</a>
<% end %>

<% if can?(:destroy, report) %>
<%= link_to 'Delete', "/reports/#{report.id}", method: :delete %>
<% end %>
</td>
</tr>
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/reports/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

<% if current_user.is_role?(:provider) %>
<h2>My reports</h2>
<%= render partial: "table", locals: {reports: @user_reports, table: "user"} %>
<%= render partial: "table", locals: { reports: @user_reports, table: "user" } %>

<h2>Agency reports</h2>
<%= render partial: "table", locals: {reports: @agency_reports, table: "agency"} %>
<%= render partial: "table", locals: { reports: @agency_reports, table: "agency" } %>
<% else %>
<%= render partial: "table", locals: {reports: @reports} %>
<%= render partial: "table", locals: { reports: @reports} %>
<% end %>

4 changes: 2 additions & 2 deletions app/views/reports/search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@

<%= label_tag :from_date, 'Date of discovery (from)' %>
<div class="form__field">
<%= date_select :from_date, params[:from_date], include_blank: true, order: [:day, :month, :year], default: nil %>
<%= date_select :from_date, params[:from_date], include_blank: true, start_year: @earliest_report_year, order: [:day, :month, :year], default: nil %>
</div>
</div>

<div class="form__group">
<%= label_tag :to_date, 'Date of discovery (to)' %>
<div class="form__field">
<%= date_select :to_date, params[:to_date], include_blank: true, order: [:day, :month, :year], default: nil %>
<%= date_select :to_date, params[:to_date], include_blank: true, start_year: @earliest_report_year, order: [:day, :month, :year], default: nil %>
</div>
</div>

Expand Down
Loading

0 comments on commit f7e5d22

Please sign in to comment.