Skip to content

Commit

Permalink
Error pages and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
hschne committed Dec 18, 2023
1 parent ab01bb5 commit 95aad3d
Show file tree
Hide file tree
Showing 27 changed files with 271 additions and 362 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ gem 'inline_svg', '~> 1.9'
gem 'annotate', '~> 3.2'

gem 'erb-formatter', '~> 0.6.0'

gem "active_storage_validations", "~> 1.1"
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ GEM
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
active_storage_validations (1.1.3)
activejob (>= 5.2.0)
activemodel (>= 5.2.0)
activestorage (>= 5.2.0)
activesupport (>= 5.2.0)
activejob (7.1.2)
activesupport (= 7.1.2)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -295,6 +300,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
active_storage_validations (~> 1.1)
annotate (~> 3.2)
bootsnap
debug
Expand Down
40 changes: 0 additions & 40 deletions app/controllers/drops_controller.rb

This file was deleted.

9 changes: 9 additions & 0 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ErrorsController < ApplicationController
def not_found
render status: 404
end

def internal_server_error
render status: 500
end
end
40 changes: 27 additions & 13 deletions app/controllers/uploads_controller.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
# frozen_string_literal: true

class UploadsController < ApplicationController
before_action :set_upload, only: %i[show preview]

def show; end

def preview; end
include ActiveStorage::SetCurrent
include ActionController::Live

def new
@upload = Upload.new
@upload = Upload.new(expiry: 10.minutes.from_now, remaining_uses: 1)
end

def create
def upload
@upload = Upload.new(upload_params)
@upload.key = Upload.generate_key

respond_to do |format|
if @upload.save
format.html { redirect_to upload_url(@upload), notice: 'Upload was successfully created.' }
format.json { render :show, status: :created, location: @upload }
format.html { redirect_to preview_url(@upload) }
format.json { render :preview, status: :created, location: @upload }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @upload.errors, status: :unprocessable_entity }
end
end
end

def preview
@upload = upload_scope.where(previewed: false).find_by!(key: params[:id])
@upload.update!(previewed: true)
respond_to do |format|
format.html { render :preview, status: :not_found }
end
end

def download
@upload = upload_scope.find_by!(key: params[:id])
@upload.decrement!(:remaining_uses)

redirect_to rails_blob_path(@upload.data, disposition: 'attachment')
# send_data @upload.data.download, filename: @upload.data.filename.to_s, content_type: @upload.data.content_type
end

private

# Use callbacks to share common setup or constraints between actions.
def set_upload
@upload = Upload.find(params[:id])
def upload_scope
Upload.where('expiry > ?', DateTime.now).where('remaining_uses > ?', 0)
end

# Only allow a list of trusted parameters through.
def upload_params
params.require(:upload).permit(:path, :data, :expiry, :remaining_uses, :previewed)
params
.require(:upload).permit(:data, :expiry, :remaining_uses)
end
end
2 changes: 2 additions & 0 deletions app/helpers/errors_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ErrorsHelper
end
6 changes: 4 additions & 2 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"
import "@hotwired/turbo-rails";
import "controllers";

Turbo.setFormMode("optin");
11 changes: 0 additions & 11 deletions app/models/dice_word.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,4 @@ class DiceWord < ApplicationRecord
def readonly?
true
end

class << self
def generate_password
ids = (1..4).map { (1..5).map { rand(1..6) }.join.to_i }
DiceWord
.find(*ids)
.map(&:words)
.map { |w| w.split(' ') }
.map { |t| t[rand(0..t.length - 1)] }.flatten.join('-')
end
end
end
31 changes: 31 additions & 0 deletions app/models/upload.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: uploads
#
# id :integer not null, primary key
# expiry :datetime not null
# key :string not null
# previewed :boolean default(FALSE), not null
# remaining_uses :integer default(1), not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_uploads_on_key (key) UNIQUE
#
class Upload < ApplicationRecord
has_one_attached :data

validates :data, attached: true, size: { less_than: 512.kilobytes, message: 'is must be smaller than 512 kb' }

def to_param
key
end

def self.generate_key
ids = (1..3).map { (1..5).map { rand(1..6) }.join.to_i }
DiceWord
.find(*ids)
.map(&:words)
.map { |w| w.split(' ') }
.map { |t| t[rand(0..t.length - 1)] }.flatten.join('-')
end
end
4 changes: 2 additions & 2 deletions app/views/application/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
block w-full rounded bg-red-600 px-12 py-3 text-sm font-medium text-white shadow
hover:bg-red-700 focus:outline-none focus:ring active:bg-red-500 sm:w-auto
"
href="<%= new_drop_path %>"
href="<%= new_path %>"
>
Get Started
</a>
Expand Down Expand Up @@ -53,7 +53,7 @@
<p class="text-gray-700 mb-4">Drop some data in our
<%= link_to(
"Web UI",
new_drop_path,
new_path,
class:
" text-red-600 no-underline hover:text-red-700 hover:underline active:bg-red-500",
) %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/errors/internal_server_error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="w-full h-full grid place-content-center bg-white">
<h1 class="uppercase tracking-widest font-semibold text-gray-700">500 | Server Error</h1>
</div>
3 changes: 3 additions & 0 deletions app/views/errors/not_found.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="w-full h-full grid place-content-center bg-white">
<h1 class="uppercase tracking-widest font-semibold text-gray-700">404 | Not Found</h1>
</div>
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<%= javascript_importmap_tags %>
</head>

<body>
<body class="h-screen flex flex-col">

<header class="bg-white">
<div
Expand Down Expand Up @@ -43,7 +43,7 @@
</a>
</div>
</header>
<main class="">
<main class="flex-grow">
<%= yield %>
</main>

Expand Down
35 changes: 18 additions & 17 deletions app/views/uploads/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<%= form_with(model: upload, class: "contents") do |form| %>
<%= form_with(model: upload, class: "contents", url: upload_path) do |form| %>
<% if upload.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(upload.errors.count, "error") %> prohibited this upload from being saved:</h2>
<div
id="error_explanation"
class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3"
>
<h2><%= pluralize(upload.errors.count, "error") %>
prohibited this upload from being saved:</h2>

<ul>
<% upload.errors.each do |error| %>
Expand All @@ -11,32 +15,29 @@
</div>
<% end %>

<div class="my-5">
<%= form.label :path %>
<%= form.text_field :path, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :data %>
<%= form.file_field :data, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
<%= form.file_field :data,
class:
"block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :expiry %>
<%= form.datetime_field :expiry, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
<%= form.datetime_field :expiry,
class:
"block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :remaining_uses %>
<%= form.number_field :remaining_uses, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :previewed %>
<%= form.check_box :previewed, class: "block mt-2 h-5 w-5" %>
<%= form.number_field :remaining_uses,
class:
"block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
<%= form.submit class:
"rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>
20 changes: 2 additions & 18 deletions app/views/uploads/_upload.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<div id="<%= dom_id upload %>">
<p class="my-5">
<strong class="block font-medium mb-1">Path:</strong>
<%= upload.path %>
</p>

<p class="my-5">
<strong class="block font-medium mb-1">Data:</strong>
<%= link_to upload.data.filename, upload.data if upload.data.attached? %>
<strong class="block font-medium mb-1">Key:</strong>
<%= upload.key %>
</p>

<p class="my-5">
Expand All @@ -18,15 +13,4 @@
<strong class="block font-medium mb-1">Remaining uses:</strong>
<%= upload.remaining_uses %>
</p>

<p class="my-5">
<strong class="block font-medium mb-1">Previewed:</strong>
<%= upload.previewed %>
</p>

<% if action_name != "show" %>
<%= link_to "Show this upload", upload, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Edit this upload", edit_upload_path(upload), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<hr class="mt-6">
<% end %>
</div>
8 changes: 2 additions & 6 deletions app/views/uploads/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<section>
<div class="mx-auto md:w-2/3 w-full">
<section class="px-4 py-32">
<div class="mx-auto max-w-2xl ">
<h1 class="font-bold text-4xl">New upload</h1>

<%= render "form", upload: @upload %>

<%= link_to "Back to uploads",
uploads_path,
class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
</section>
18 changes: 18 additions & 0 deletions app/views/uploads/preview.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<section>
<h2>A Preview</h2>
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p
class="
py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block
"
id="notice"
><%= notice %></p>
<% end %>

<%= render @upload %>

</div>
</div>
</section>
File renamed without changes.
15 changes: 0 additions & 15 deletions app/views/uploads/show.html.erb

This file was deleted.

3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ class Application < Rails::Application

# Don't generate system test files.
config.generators.system_tests = nil

# Use routes to render error pages
config.exceptions_app = routes
end
end
Loading

0 comments on commit 95aad3d

Please sign in to comment.