Skip to content

Commit

Permalink
feat: Downloading a zip with CSV documents
Browse files Browse the repository at this point in the history
The new update allows the administrator to download a zip containing all the current CSV documents. Issue #596.
  • Loading branch information
franpb14 committed Apr 16, 2021
1 parent e04dfcc commit d3ab098
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ruby File.read('.ruby-version').strip
gem 'rails', '~> 6.1.1'
gem 'rails-i18n', '~> 6.0.0'
gem 'rdiscount', '~> 2.2.0.1'
gem 'rubyzip', '~> 2.3.0'
gem 'activeadmin', '~> 2.9.0'
gem 'has_scope', '~> 0.7.2'
gem 'pundit', '~> 2.1.0'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ DEPENDENCIES
rspec-rails (~> 4.0.0)
rubocop (~> 1.6)
rubocop-rails (~> 2.9)
rubyzip (~> 2.3.0)
sassc-rails (~> 2.1.2)
select2-rails (~> 4.0.13)
selenium-webdriver (~> 3.142)
Expand Down
5 changes: 5 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,8 @@ label[required]::after{
display: inline;
margin: 0 !important;
}

.hr-5 {
margin-top: 5px;
margin-bottom: 5px;
}
60 changes: 60 additions & 0 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'zip'
class ReportsController < ApplicationController
before_action :authenticate_user!

Expand Down Expand Up @@ -31,6 +32,18 @@ def transfer_list
report_responder('Transfer', current_organization, @transfers)
end

def all_list
filename = "#{current_organization.name.gsub(' ', '_')}.zip"
temp_file = Tempfile.new(filename)
Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile|
add_csvs_to_zip(%w[Member Transfer Inquiries Offers], zipfile)
end
zip_data = File.read(temp_file.path)
send_data(zip_data, type: 'application/zip', disposition: 'attachment', filename: filename)
temp_file.close
temp_file.unlink
end

private

def report_responder(report_class, *args)
Expand All @@ -45,4 +58,51 @@ def download_report(report_class, *args)
report = report_class.constantize.new(*args)
send_data report.run, filename: report.name, type: report.mime_type
end

def add_csvs_to_zip(report_classes, zip)
report_classes.each do |report_class|
collection = return_collection(report_class)
report = do_report(report_class, collection)
file = Tempfile.new
file.write(report.run)
file.rewind
zip.add("#{report_class}.csv", file.path)
end
end

def return_collection(report_class)
case report_class
when 'Member'
current_organization.members.active.includes(:user).order('members.member_uid')
when 'Transfer'
current_organization.all_transfers_with_accounts
when 'Inquiries'
return_collection_posts('Inquiry')
when 'Offers'
return_collection_posts('Offer')
else
[]
end
end

def return_collection_posts(type)
@post_type = type.constantize
@posts = current_organization.posts.of_active_members.active.
merge(@post_type.all).
includes(:user, :category).
group_by(&:category).
to_a.
sort_by { |category, _| category.try(:name).to_s }
end

def do_report(report_class, collection)
case report_class
when 'Inquiries'
'Report::Csv::Post'.constantize.new(current_organization, collection, 'Inquiry'.constantize)
when 'Offers'
'Report::Csv::Post'.constantize.new(current_organization, collection, 'Offer'.constantize)
else
"Report::Csv::#{report_class}".constantize.new(current_organization, collection)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@
<%= Transfer.model_name.human(count: :many) %>
<% end %>
</li>
<hr class="hr-5">
<li>
<%= link_to all_list_report_path do %>
<%= glyph :download %>
<%= t 'reports.download_all' %>
<% end %>
</li>
</ul>
</li>
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ en:
cat_with_users:
title: Offered Services
download: Download
download_all: Download all
user_list:
title: User List
shared:
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
get "offer_list" => :post_list, type: "offer"
get "inquiry_list" => :post_list, type: "inquiry"
get "transfer_list"
get "all_list"
end
end

Expand Down

0 comments on commit d3ab098

Please sign in to comment.