Skip to content

Commit

Permalink
WIP: Explore in Datasette
Browse files Browse the repository at this point in the history
TODO: Tests for setting cors headers
TODO: Add DATASETTE_INSTANCE config item (so that we can use our own)
TODO: Add `explore_in_datasette` helper:

   # Something like this?
   def explore_in_datasette(attachment)
     return unless attachment.content_type == 'text/csv'

     link_to 'Explore in Datasette',
     datasette_url(attachment_url(attachment))
   end
  • Loading branch information
garethrees committed Oct 17, 2023
1 parent e99f6c8 commit b2bbbea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AttachmentsController < ApplicationController

before_action :authenticate_attachment
before_action :authenticate_attachment_as_html, only: :show_as_html
before_action :set_cors

around_action :cache_attachments, only: :show_as_html

Expand Down Expand Up @@ -135,6 +136,14 @@ def authenticate_attachment_as_html
raise ActiveRecord::RecordNotFound, 'Attachment HTML not found.'
end

def set_cors
# Allow CSVs to be explored in a Datasette instance
if @attachment.content_type == 'text/csv'

Check warning on line 141 in app/controllers/attachments_controller.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 [Correctable] Style/GuardClause: Use a guard clause (return unless @attachment.content_type == 'text/csv') instead of wrapping the code inside a conditional expression. (https://rubystyle.guide#no-nested-conditionals) Raw Output: app/controllers/attachments_controller.rb:141:5: C: [Correctable] Style/GuardClause: Use a guard clause (return unless @attachment.content_type == 'text/csv') instead of wrapping the code inside a conditional expression. (https://rubystyle.guide#no-nested-conditionals) if @attachment.content_type == 'text/csv' ^^
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Request-Method'] = 'GET'
end
end

# special caching code so mime types are handled right
def cache_attachments
if !params[:skip_cache].nil?
Expand Down
3 changes: 3 additions & 0 deletions app/views/request/_attachments.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<% if a.has_body_as_html? && incoming_message.info_request.prominence(:decorate => true).is_public? %>
<%= link_to "View as HTML", attachment_path(a, :html => true) %>
<% end %>
<% if a.content_type == 'text/csv' && incoming_message.info_request.prominence(:decorate => true).is_public? %>
<%= link_to _('Explore in Datasette'), "https://lite.datasette.io/?csv=#{attachment_url(a)}" %>
<% end %>
<%= a.extra_note %>
</p>
<% end %>
Expand Down

0 comments on commit b2bbbea

Please sign in to comment.