Created
November 7, 2019 12:33
-
-
Save Whatapalaver/4850ebcc928ed39ad5bd5a90a685928d to your computer and use it in GitHub Desktop.
Add a csv download button to Rails view
This file contains hidden or 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
<p>Generate Outstanding Migration CSV Report: <%= link_to("CSV Report", admin_dashboard_collection_objects_path(format: :csv), class: "btn btn-success",) %></p> |
This file contains hidden or 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
def collection_objects | |
@museum_collections = museum_collections | |
respond_to do |format| | |
format.html | |
format.csv do | |
send_data @museum_collections.to_csv('outstanding'), | |
filename: "outstanding-#{Time.zone.today}.csv" | |
end | |
end | |
end |
This file contains hidden or 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
def self.to_csv(scope = 'outstanding') | |
CSV.generate do |csv| | |
csv << %w[museum_collection_id museum_collection_slug museum_collection_status | |
enhanced_object_id enhanced?] | |
all.find_each do |mc| | |
mc.enhanced_objects.each do |eo| | |
if scope == 'outstanding' | |
unless eo.accessioned | |
csv << [mc.id, mc.name, mc.state, eo.id, eo.accessioned] | |
end | |
else | |
csv << [mc.id, mc.name, mc.state, eo.id, eo.accessioned] | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment