Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocumentReferences renamed to DocumentDistributions #114

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .standard.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
format: progress
ignore:
- 'db/seeds.rb'
- 'db/migrate/**'
- 'db/migrate/**'
- 'app/models/document/bbox_validator.rb'
172 changes: 172 additions & 0 deletions app/controllers/admin/document_distributions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# frozen_string_literal: true

# Admin::DocumentDistributionsController
#
# This controller manages the CRUD operations for Document Distributions within the Admin namespace.
# It includes actions for listing, showing, creating, updating, and deleting document distributions.
# Additionally, it provides functionality for importing and destroying all distributions.
module Admin
class DocumentDistributionsController < Admin::AdminController
before_action :set_document
before_action :set_document_distribution, only: %i[show edit update destroy]

# GET /admin/document_distributions or /admin/document_distributions.json
#
# Lists all document distributions. If a document_id is provided, it lists distributions
# associated with that document, ordered by position. Otherwise, it paginates all
# document distributions.
def index
@document_distributions = DocumentDistribution.all
if params[:document_id]
@document_distributions = DocumentDistribution.where(friendlier_id: @document.friendlier_id).order(position: :asc)
else
@pagy, @document_distributions = pagy(DocumentDistribution.all.order(friendlier_id: :asc, updated_at: :desc), items: 20)
end
end

# GET /admin/document_distributions/1 or /admin/document_distributions/1.json
#
# Shows a specific document distribution.
def show
end

# GET /admin/document_references/new
#
# Initializes a new document distribution.
def new
@document_distribution = DocumentDistribution.new
end

# GET /admin/document_distributions/1/edit
#
# Edits an existing document distribution.
def edit
end

# POST /admin/document_distributions or /admin/document_distributions.json
#
# Creates a new document distribution. If successful, redirects to the document distributions
# list with a success notice. Otherwise, renders the new form with errors.
def create
@document_distribution = DocumentDistribution.new(document_distribution_params)

respond_to do |format|
if @document_distribution.save
format.html { redirect_to admin_document_document_distributions_path(@document), notice: "Document distribution was successfully created." }
format.json { render :show, status: :created, location: @document_distribution }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @document_distribution.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /admin/document_distributions/1 or /admin/document_distributions/1.json
#
# Updates an existing document distribution. If successful, redirects to the document distribution
# with a success notice. Otherwise, renders the edit form with errors.
def update
respond_to do |format|
if @document_distribution.update(document_distribution_params)
format.html { redirect_to admin_document_document_distributions_path(@document), notice: "Document distribution was successfully updated." }
format.json { render :show, status: :ok, location: @document_distribution }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @document_distribution.errors, status: :unprocessable_entity }
end
end
end

# DELETE /admin/document_distributions/1 or /admin/document_distributions/1.json
#
# Deletes a document distribution. Redirects to the document distributions list with a success notice.
def destroy
@document_distribution.destroy!

respond_to do |format|
format.html { redirect_to admin_document_document_distributions_path(@document), status: :see_other, notice: "Document distribution was successfully destroyed." }
format.json { head :no_content }
end
end

# DELETE /admin/document_distributions/destroy_all
#
# Destroys all document distributions provided in the file parameter. If successful, redirects
# with a success notice. Otherwise, redirects with an error notice.
def destroy_all
return if request.get?

logger.debug("Destroy Distributions")
unless params.dig(:document_distribution, :distributions, :file)
raise ArgumentError, "File does not exist or is invalid."
end

respond_to do |format|
if DocumentDistribution.destroy_all(params.dig(:document_distribution, :distributions, :file))
format.html { redirect_to admin_document_distributions_path, notice: "Distributions were destroyed." }
else
format.html { redirect_to admin_document_distributions_path, notice: "Distributions could not be destroyed." }
end
rescue => e
format.html { redirect_to admin_document_distributions_path, notice: "Distributions could not be destroyed. #{e}" }
end
end

# GET/POST /documents/1/distributions/import
#
# Imports document distributions from a file. If successful, redirects with a success notice.
# Otherwise, redirects with an error notice.
def import
return if request.get?

logger.debug("Import Distributions")

unless params.dig(:document_distribution, :distributions, :file)
raise ArgumentError, "File does not exist or is invalid."
end

if DocumentDistribution.import(params.dig(:document_distribution, :distributions, :file))
logger.debug("Distributions were created successfully.")
if params[:document_id]
redirect_to admin_document_document_distributions_path(@document), notice: "Distributions were created successfully."
else
redirect_to admin_document_document_distributions_path, notice: "Distributions were created successfully."
end
else
logger.debug("Distributions could not be created.")
if params[:document_id]
redirect_to admin_document_document_distributions_path(@document), warning: "Distributions could not be created."
else
redirect_to admin_document_document_distributions_path, warning: "Distributions could not be created."
end
end
rescue => e
logger.debug("Distributions could not be created. #{e}")
if params[:document_id]
redirect_to admin_document_document_distributions_path(@document), notice: "Distributions could not be created. #{e}"
else
redirect_to admin_document_document_distributions_path, notice: "Distributions could not be created. #{e}"
end
end

private

# Sets the document based on the document_id parameter.
# If not nested, it does nothing.
def set_document
return unless params[:document_id] # If not nested

@document = Document.includes(:leaf_representative).find_by!(friendlier_id: params[:document_id])
end

# Sets the document distribution based on the id parameter.
def set_document_distribution
@document_distribution = DocumentDistribution.find(params[:id])
end

# Permits only the trusted parameters for document distributions.
def document_distribution_params
params.require(:document_distribution).permit(:friendlier_id, :reference_type_id, :url, :label, :position)
end
end
end
172 changes: 0 additions & 172 deletions app/controllers/admin/document_references_controller.rb

This file was deleted.

8 changes: 4 additions & 4 deletions app/controllers/admin/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def index
ExportJob.perform_later(@request, current_user, query_params, ExportCsvDocumentAccessLinksService)
head :no_content
end
format.csv_document_references do
ExportJob.perform_later(@request, current_user, query_params, ExportCsvDocumentReferencesService)
format.csv_document_distributions do
ExportJob.perform_later(@request, current_user, query_params, ExportCsvDocumentDistributionsService)
head :no_content
end
end
Expand Down Expand Up @@ -102,8 +102,8 @@ def fetch
ExportJob.perform_later(@request, current_user, {ids: @documents.pluck(:friendlier_id), format: "csv_document_access_links"}, ExportCsvDocumentAccessLinksService)
head :no_content
end
format.csv_document_references do
ExportJob.perform_later(@request, current_user, {ids: @documents.pluck(:friendlier_id), format: "csv_document_references"}, ExportCsvDocumentReferencesService)
format.csv_document_distributions do
ExportJob.perform_later(@request, current_user, {ids: @documents.pluck(:friendlier_id), format: "csv_document_distributions"}, ExportCsvDocumentDistributionsService)
head :no_content
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/javascript/controllers/results_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ export default class extends Controller {
}
}

exportCsvDocumentReferences() {
console.log('Export - CsvDocumentReferences')
exportCsvDocumentDistributions() {
console.log('Export - CsvDocumentDistributions')
var scope = this.checkSelectionScope();
var el = document.querySelector('#result-selected-options');
if(scope === 'pageset') {
window.location = el.dataset.pageset + "&format=csv_document_references"
window.location = el.dataset.pageset + "&format=csv_document_distributions"
} else {
window.location = el.dataset.resultset + "&format=csv_document_references"
window.location = el.dataset.resultset + "&format=csv_document_distributions"
}
}

Expand Down
Loading
Loading