-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update document storage library from ActiveSupport to CarrierWave for…
… easier customization
- Loading branch information
1 parent
6cf5c93
commit dace0fd
Showing
34 changed files
with
570 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# frozen_string_literal: true | ||
|
||
# Allows documents to be downloaded. | ||
class DocumentsController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :check_editor!, only: [:new, :create, :edit, :update, :destroy] | ||
before_action :find_category_folder_document_or_redirect, only: [:download] | ||
before_action :find_document_or_redirect, only: [ | ||
:show, :edit, :update, :destroy | ||
] | ||
|
||
layout "layouts/full_page_sidebar" | ||
|
||
# # GET /docs | ||
# def index | ||
# @documents = Document.all | ||
# end | ||
|
||
# # GET /documents/1 | ||
# def show | ||
# end | ||
|
||
# GET /docs/:category/:folder/:filename | ||
def download | ||
params[:disposition] ||= "attachment" # "inline" | ||
@document.increment! :download_count | ||
if Rails.env.production? | ||
redirect_to @document.file.url(query: { "response-content-disposition" => params[:disposition] }) | ||
else | ||
send_file_if_present @document.file, disposition: params[:disposition] | ||
end | ||
end | ||
|
||
# # GET /documents/new | ||
# def new | ||
# @document = Document.new | ||
# end | ||
|
||
# # GET /documents/1/edit | ||
# def edit | ||
# end | ||
|
||
# # POST /documents | ||
# def create | ||
# @document = Document.new(document_params) | ||
# if @document.save | ||
# redirect_to @document, notice: "Document was successfully created." | ||
# else | ||
# render :new | ||
# end | ||
# end | ||
|
||
# PATCH /documents/1 | ||
def update | ||
if @document.update(document_params) | ||
redirect_to category_folder_path(@document.folder.category, @document.folder), notice: "#{@document.filename} was successfully updated." | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
# DELETE /documents/1 | ||
def destroy | ||
@category = @document.folder.category | ||
@folder = @document.folder | ||
@document.destroy | ||
redirect_to category_folder_path(@category, @folder), notice: 'Document was successfully deleted.' | ||
end | ||
|
||
private | ||
|
||
def find_document_or_redirect | ||
@document = Document.find_by(id: params[:id]) | ||
empty_response_or_root_path(documents_path) unless @document | ||
end | ||
|
||
def find_category_folder_document_or_redirect | ||
@category = Category.find_by_param(params[:category]) | ||
@folder = @category.folders.find_by_param(params[:folder]) if @category | ||
@document = @folder.documents.find_by(filename: params[:filename]) if @folder | ||
empty_response_or_root_path(folders_path) unless @category && @folder && @document | ||
end | ||
|
||
def document_params | ||
params.require(:document).permit(:featured) | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
# Generate links to download documents. | ||
module DocumentsHelper | ||
# This intentionally builds the URL since ActionCable jobs and emails may not | ||
# know the current request host. | ||
def document_download_link(document) | ||
"#{ENV["website_url"]}/docs/#{document.folder.category.to_param}/#{document.folder.to_param}/#{document.filename}" | ||
end | ||
end |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
# Represents a file uploaded to a folder. A document can be featured to be | ||
# displayed on the dashboard, and tracks the number of times the file has been | ||
# downloaded. | ||
class Document < ApplicationRecord | ||
# Uploaders | ||
mount_uploader :file, GenericUploader | ||
|
||
# Constants | ||
ORDERS = { | ||
"size" => "documents.byte_size", | ||
"size desc" => "documents.byte_size desc", | ||
"oldest" => "documents.created_at", | ||
"latest" => "documents.created_at desc", | ||
"name" => "LOWER(documents.filename)", | ||
"name desc" => "LOWER(documents.filename) desc" | ||
} | ||
DEFAULT_ORDER = "LOWER(documents.filename)" | ||
|
||
# Concerns | ||
include PgSearch | ||
multisearchable against: [:filename, :content_type] | ||
|
||
# Validations | ||
validates :filename, presence: true, | ||
uniqueness: { case_sensitive: false, scope: [:folder_id] } | ||
|
||
# Scopes | ||
scope :latest_files, -> do | ||
joins(:folder).merge(Folder.where(archived: false).joins(:category).merge(Category.sidebar)).order(created_at: :desc).limit(10) | ||
end | ||
|
||
# Relationships | ||
belongs_to :folder, counter_cache: true | ||
|
||
# Methods | ||
|
||
def self.content_type(filename) | ||
MIME::Types.type_for(filename).first.content_type | ||
end | ||
end |
This file contains 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
Oops, something went wrong.