diff options
| author | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-13 18:03:43 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-13 18:03:43 +0100 |
| commit | a7791635aa98f012d35f97c3c5288494bbb28e38 (patch) | |
| tree | bad987e6b4d10ad2f7cd02e40658d464907595ee /app/controllers/exports_controller.rb | |
| download | operum-a7791635aa98f012d35f97c3c5288494bbb28e38.tar.gz operum-a7791635aa98f012d35f97c3c5288494bbb28e38.zip | |
Initial commit
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'app/controllers/exports_controller.rb')
| -rw-r--r-- | app/controllers/exports_controller.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/app/controllers/exports_controller.rb b/app/controllers/exports_controller.rb new file mode 100644 index 0000000..4303ea6 --- /dev/null +++ b/app/controllers/exports_controller.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +class ExportsController < ApplicationController + before_action :set_search + before_action :set_results, only: %i[show] + + def show + respond_to do |format| + format.csv { set_file_name!(ext: 'csv') } + format.uoc { set_file_name!(ext: 'tex') } + end + end + + def new; end + + protected + + # Sets the file name for the attachment according to the @search's name, and + # it adds the given string `ext` as the extension. + def set_file_name!(ext:) + response.headers['Content-Disposition'] = "attachment; filename=\"#{@search.name}.#{ext}\"" + end + + # Set @search. If there was no `:search_id` parameter, then we assume that the + # ID = 0, which is the fake search we use for the base 'Home' (i.e. get + # everything). + def set_search + @search = if params[:search_id].to_i.zero? + Search.new(id: 0, name: 'Home') + else + Search.find(params[:search_id]) + end + end + + def set_results + @results = @search.results.fetch(:things, []) + end +end |
