Skip to content

Commit

Permalink
WIP 4: Ready for review.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewlarson committed Jan 14, 2025
1 parent 60b957d commit 62b50fd
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Admin
class DocumentDataDictionariesController < Admin::AdminController
before_action :set_document
before_action :set_document_data_dictionary, only: %i[ show edit update destroy ]
before_action :set_document_data_dictionary, only: %i[show edit update destroy]

# GET /document_data_dictionaries or /document_data_dictionaries.json
def index
Expand All @@ -21,6 +21,7 @@ def index

# GET /document_data_dictionaries/1 or /document_data_dictionaries/1.json
def show
@pagy, @document_data_dictionary_entries = pagy(@document_data_dictionary.document_data_dictionary_entries.order(position: :asc), items: 100)
end

# GET /document_data_dictionaries/new
Expand Down Expand Up @@ -146,7 +147,7 @@ def set_document_data_dictionary
@document_data_dictionary = DocumentDataDictionary.find(params[:id])
end

# Only allow a list of trusted parameters through.
# Only allow a list of trusted parameters through.
def document_data_dictionary_params
params.require(:document_data_dictionary).permit(
:friendlier_id,
Expand All @@ -159,4 +160,4 @@ def document_data_dictionary_params
)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
# This controller manages the document data dictionary entries within the admin namespace.
# It provides actions to list, show, edit, update, destroy, and import data dictionaries.
module Admin
class DocumentDataDictionaryEntriesController < Admin::AdminController
class DocumentDataDictionaryEntriesController < Admin::AdminController
before_action :set_document
before_action :set_document_data_dictionary
before_action :set_document_data_dictionary_entry, only: %i[ show edit update destroy ]
before_action :set_document_data_dictionary_entry, only: %i[show edit update destroy]

# GET /document_data_dictionaries/1/entries or /document_data_dictionaries/1/entries.json
def index
Expand Down Expand Up @@ -49,7 +49,7 @@ def update
respond_to do |format|
if @document_data_dictionary_entry.update(document_data_dictionary_entry_params)
format.html { redirect_to admin_document_document_data_dictionary_path(@document, @document_data_dictionary), notice: "Document data dictionary entry was successfully updated." }
format.json { render :show, status: :ok, location: @document_data_dictionary_entry }
format.json { render :show, status: :ok, location: @document_data_dictionary_entry }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @document_data_dictionary_entry.errors, status: :unprocessable_entity }
Expand Down Expand Up @@ -87,6 +87,14 @@ def destroy_all
end
end

# POST /document_data_dictionaries/1/entries/sort
# Sorts document data dictionary entries based on the provided list of IDs.
# Renders an empty response body.
def sort
DocumentDataDictionary.sort_entries(params[:id_list])
render body: nil
end

private

# Sets the document based on the document_id parameter.
Expand All @@ -106,7 +114,7 @@ def set_document_data_dictionary_entry
@document_data_dictionary_entry = DocumentDataDictionaryEntry.find(params[:id])
end

# Only allow a list of trusted parameters through.
# Only allow a list of trusted parameters through.
def document_data_dictionary_entry_params
params.require(:document_data_dictionary_entry).permit(
:friendlier_id,
Expand All @@ -121,4 +129,4 @@ def document_data_dictionary_entry_params
)
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def item_viewer
# - Publication State
has_many :document_transitions, foreign_key: "kithe_model_id", autosave: false, dependent: :destroy,
inverse_of: :document

# - Thumbnail State
has_many :document_thumbnail_transitions, foreign_key: "kithe_model_id", autosave: false, dependent: :destroy,
inverse_of: :document
Expand Down
14 changes: 11 additions & 3 deletions app/models/document_data_dictionary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DocumentDataDictionary < ApplicationRecord
# Associations
has_one_attached :csv_file
belongs_to :document, foreign_key: :friendlier_id, primary_key: :friendlier_id
has_many :document_data_dictionary_entries, dependent: :destroy
has_many :document_data_dictionary_entries, -> { order(position: :asc) }, dependent: :destroy

# Validations
validates :name, presence: true
Expand All @@ -23,9 +23,17 @@ def parse_csv_file
if csv_file.attached?
csv_data = CSV.parse(csv_file.download, headers: true)
csv_data.each do |row|
self.document_data_dictionary_entries.create!(row.to_h)
document_data_dictionary_entries.create!(row.to_h)
end
end
end
end

def self.sort_entries(id_array)
transaction do
logger.debug { id_array.inspect }
id_array.each_with_index do |entry_id, i|
DocumentDataDictionaryEntry.update(entry_id, position: i)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def validate(record)
unless valid_csv_headers?(record&.csv_file)
valid_csv_header = false
record.errors.add(:csv_file,
"Missing the required CSV header. friendlier_id, field_name, field_type, values, definition, definition_source, and parent_field_name are required.")
"Missing the required CSV header. friendlier_id, field_name, field_type, values, definition, definition_source, and parent_field_name are required.")
end

valid_csv_header
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/document_data_dictionaries/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<%= f.file_field :csv_file, direct_upload: true %>
</div>

<div class="form-group">
<div class="form-group mt-3">
<%= f.submit class: 'btn btn-primary' %>
</div>
<% end %>
2 changes: 0 additions & 2 deletions app/views/admin/document_data_dictionaries/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
<div class="row">
<div class="col-5">
<%= render "form", document_data_dictionary: @document_data_dictionary %>

<%= link_to "Back to document data dictionaries", admin_document_document_data_dictionaries_path(@document) %>
</div>

<div class="col-6">
Expand Down
62 changes: 38 additions & 24 deletions app/views/admin/document_data_dictionaries/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,51 @@

<div class="row mb-2">
<div class="col">
<%= link_to "Back to document data dictionaries", admin_document_document_data_dictionaries_path(@document) %>

<h1 style="width:100%;">
<%= @document.title %> &middot; Data Dictionaries &middot; <%= @document_data_dictionary.name %>

<%= link_to '+ New Entry', new_admin_document_document_data_dictionary_document_data_dictionary_entry_path(@document, @document_data_dictionary), { class: 'btn btn-primary float-right' } %>
</h1>
</div>
</div>

<div class="row">
<div class="col-12">
<%= render @document_data_dictionary %>
</div>
</div>
<h6>
<span class='float-left mt-3'>
<%== pagy_info(@pagy) %>
</span>
<span class='float-right'>
<%== pagy_bootstrap_nav(@pagy) %>
</span>
</h6>

<div class="row">
<div class="col-12">
<h3>
Data Dictionary Entries
<%= link_to '+ New Entry', new_admin_document_document_data_dictionary_document_data_dictionary_entry_path(@document, @document_data_dictionary), { class: 'btn btn-primary float-right' } %>
</h3>
<div class="table-responsive">
<table class="table table-striped table-bordered sortable">
<thead class="thead-dark">
<thead class="thead-dark">
<tr>
<th>field_name</th>
<th>field_type</th>
<th>values</th>
<th>definition</th>
<th>definition_source</th>
<th>parent_field_name</th>
<th colspan="2">Actions</th>
<th class="header">field_name</th>
<th class="header">field_type</th>
<th class="header">values</th>
<th class="header">definition</th>
<th class="header">definition_source</th>
<th class="header">parent_field_name</th>
<th class="header">Reorder</th>
<th class="header" colspan="2">Actions</th>
</tr>
</thead>
<tbody>
<% @document_data_dictionary.document_data_dictionary_entries.each do |entry| %>
<tr>
<tr data-id="<%=entry.id%>">
<td><%= entry.field_name %></td>
<td><%= entry.field_type %></td>
<td><%= entry.values %></td>
<td><%= entry.definition %></td>
<td><%= entry.definition_source %></td>
<td><%= entry.parent_field_name %></td>
<td class="handle" style="text-align:center">&varr;</td>
<td>
<%= link_to 'Edit', edit_admin_document_document_data_dictionary_document_data_dictionary_entry_path(@document, @document_data_dictionary, entry.id) %>
</td>
Expand All @@ -53,12 +58,21 @@
</tbody>
</table>
</div>

<h6>
<span class='float-left mt-3'>
<%== pagy_info(@pagy) %>
</span>
<span class='float-right'>
<%== pagy_bootstrap_nav(@pagy) %>
</span>
</h6>
</div>
</div>
<div>
<%= link_to "Edit this document data dictionary", edit_admin_document_document_data_dictionary_path(@document, @document_data_dictionary) %> |
<%= link_to "Back to document data dictionaries", admin_document_document_data_dictionaries_path(@document) %>

<br>
<%= button_to "Destroy this document data dictionary", admin_document_document_data_dictionary_path(@document, @document_data_dictionary), method: :delete %>
</div>
<script>
GBLADMIN.SortElements(
$("tbody"),
"<%= sort_admin_document_document_data_dictionary_document_data_dictionary_entries_path(@document, @document_data_dictionary) %>"
);
</script>
1 change: 1 addition & 0 deletions lib/generators/geoblacklight_admin/config_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def set_routes
resources :document_data_dictionary_entries, path: "entries" do
collection do
post "sort"
get "destroy_all"
post "destroy_all"
end
Expand Down

0 comments on commit 62b50fd

Please sign in to comment.