From 217077d2880aed7f6a9808cd5b04445f7c3f3fca Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Thu, 19 Dec 2024 15:11:51 -0600 Subject: [PATCH] WIP 4: Ready for review. --- .../document_data_dictionaries_controller.rb | 7 ++- ...ment_data_dictionary_entries_controller.rb | 18 ++++-- app/models/document.rb | 2 +- app/models/document_data_dictionary.rb | 14 ++++- .../csv_header_validator.rb | 2 +- .../document_data_dictionaries/_form.html.erb | 2 +- .../document_data_dictionaries/new.html.erb | 2 - .../document_data_dictionaries/show.html.erb | 62 ++++++++++++------- .../geoblacklight_admin/config_generator.rb | 1 + 9 files changed, 70 insertions(+), 40 deletions(-) diff --git a/app/controllers/admin/document_data_dictionaries_controller.rb b/app/controllers/admin/document_data_dictionaries_controller.rb index 87f38d5..38a7ba6 100644 --- a/app/controllers/admin/document_data_dictionaries_controller.rb +++ b/app/controllers/admin/document_data_dictionaries_controller.rb @@ -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 @@ -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 @@ -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, @@ -159,4 +160,4 @@ def document_data_dictionary_params ) end end -end \ No newline at end of file +end diff --git a/app/controllers/admin/document_data_dictionary_entries_controller.rb b/app/controllers/admin/document_data_dictionary_entries_controller.rb index bf2d113..a933617 100644 --- a/app/controllers/admin/document_data_dictionary_entries_controller.rb +++ b/app/controllers/admin/document_data_dictionary_entries_controller.rb @@ -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 @@ -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 } @@ -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. @@ -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, @@ -121,4 +129,4 @@ def document_data_dictionary_entry_params ) end end -end \ No newline at end of file +end diff --git a/app/models/document.rb b/app/models/document.rb index 8ed4610..9b896bf 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -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 diff --git a/app/models/document_data_dictionary.rb b/app/models/document_data_dictionary.rb index 9eb318c..8b6fbd0 100644 --- a/app/models/document_data_dictionary.rb +++ b/app/models/document_data_dictionary.rb @@ -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 @@ -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 diff --git a/app/models/document_data_dictionary/csv_header_validator.rb b/app/models/document_data_dictionary/csv_header_validator.rb index 61e89da..9dabb2a 100644 --- a/app/models/document_data_dictionary/csv_header_validator.rb +++ b/app/models/document_data_dictionary/csv_header_validator.rb @@ -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 diff --git a/app/views/admin/document_data_dictionaries/_form.html.erb b/app/views/admin/document_data_dictionaries/_form.html.erb index a396fbd..14ae6a8 100644 --- a/app/views/admin/document_data_dictionaries/_form.html.erb +++ b/app/views/admin/document_data_dictionaries/_form.html.erb @@ -12,7 +12,7 @@ <%= f.file_field :csv_file, direct_upload: true %> -
+
<%= f.submit class: 'btn btn-primary' %>
<% end %> diff --git a/app/views/admin/document_data_dictionaries/new.html.erb b/app/views/admin/document_data_dictionaries/new.html.erb index 8102440..b863ca9 100644 --- a/app/views/admin/document_data_dictionaries/new.html.erb +++ b/app/views/admin/document_data_dictionaries/new.html.erb @@ -11,8 +11,6 @@
<%= render "form", document_data_dictionary: @document_data_dictionary %> - - <%= link_to "Back to document data dictionaries", admin_document_document_data_dictionaries_path(@document) %>
diff --git a/app/views/admin/document_data_dictionaries/show.html.erb b/app/views/admin/document_data_dictionaries/show.html.erb index 02ebdb2..570aa06 100644 --- a/app/views/admin/document_data_dictionaries/show.html.erb +++ b/app/views/admin/document_data_dictionaries/show.html.erb @@ -2,46 +2,51 @@
+ <%= link_to "Back to document data dictionaries", admin_document_document_data_dictionaries_path(@document) %> +

<%= @document.title %> · Data Dictionaries · <%= @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' } %>

- <%= render @document_data_dictionary %> -
-
+
+ + <%== pagy_info(@pagy) %> + + + <%== pagy_bootstrap_nav(@pagy) %> + +
-
-
-

- 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' } %> -

- + - - - - - - - + + + + + + + + <% @document_data_dictionary.document_data_dictionary_entries.each do |entry| %> - + + @@ -53,12 +58,21 @@
field_namefield_typevaluesdefinitiondefinition_sourceparent_field_nameActionsfield_namefield_typevaluesdefinitiondefinition_sourceparent_field_nameReorderActions
<%= entry.field_name %> <%= entry.field_type %> <%= entry.values %> <%= entry.definition %> <%= entry.definition_source %> <%= entry.parent_field_name %> <%= link_to 'Edit', edit_admin_document_document_data_dictionary_document_data_dictionary_entry_path(@document, @document_data_dictionary, entry.id) %>
+ +
+ + <%== pagy_info(@pagy) %> + + + <%== pagy_bootstrap_nav(@pagy) %> + +
-
- <%= 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) %> -
- <%= button_to "Destroy this document data dictionary", admin_document_document_data_dictionary_path(@document, @document_data_dictionary), method: :delete %> -
+ diff --git a/lib/generators/geoblacklight_admin/config_generator.rb b/lib/generators/geoblacklight_admin/config_generator.rb index 59af4f6..3fc0784 100644 --- a/lib/generators/geoblacklight_admin/config_generator.rb +++ b/lib/generators/geoblacklight_admin/config_generator.rb @@ -217,6 +217,7 @@ def set_routes resources :document_data_dictionary_entries, path: "entries" do collection do + post "sort" get "destroy_all" post "destroy_all" end