Skip to content

Commit

Permalink
added a button for direct download of measurment series based on sugg…
Browse files Browse the repository at this point in the history
…estion of dendrochonologist
  • Loading branch information
MartinHinz committed Dec 18, 2024
1 parent abbdbf8 commit 3677234
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/controllers/dendros_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'csv'

class DendrosController < ApplicationController
include Tabulatable
include Pagy::Backend
Expand Down Expand Up @@ -99,6 +101,23 @@ def destroy
format.json { head :no_content }
end
end

def export_measurements_csv
@dendro = Dendro.find(params[:id])

csv_data = CSV.generate(headers: true) do |csv|
csv << ["Year", "Width (mm)"]
@dendro.measurements.each do |measurement|
csv << [measurement["year"], measurement["value"]]
end
end

file_name = "dendro_measurements_#{@dendro.series_code.parameterize}.csv"

respond_to do |format|
format.csv { send_data csv_data, filename: file_name }
end
end

private
# Use callbacks to share common setup or constraints between actions.
Expand Down
1 change: 1 addition & 0 deletions app/views/dendros/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
</tbody>
</table>
</div>
<%= link_to "Download as CSV", export_measurements_csv_dendro_path(@dendro, format: :csv), class: "btn btn-primary btn-sm ms-2" %>
</section>

<%# References Section %>
Expand Down
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Rails.application.routes.draw do
resources :dendros
use_doorkeeper

# Static pages
Expand Down Expand Up @@ -46,6 +45,12 @@
resources :typos do
get 'search', on: :collection
end

resources :dendros do
member do
get :export_measurements_csv
end
end

# Secondary data resources (no independent show/index views)
resources :taxons, except: [:index, :show] do
Expand Down

0 comments on commit 3677234

Please sign in to comment.