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 %> -
field_name | -field_type | -values | -definition | -definition_source | -parent_field_name | -Actions | +field_name | +field_type | +values | +definition | +definition_source | +parent_field_name | +Reorder | +Actions | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
<%= 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) %> | @@ -53,12 +58,21 @@