diff --git a/app/controllers/admin/document_assets_controller.rb b/app/controllers/admin/document_assets_controller.rb index 1f76495f..ee824bcd 100644 --- a/app/controllers/admin/document_assets_controller.rb +++ b/app/controllers/admin/document_assets_controller.rb @@ -4,6 +4,7 @@ module Admin class DocumentAssetsController < Admin::AdminController before_action :set_document + before_action :set_document_asset, only: %i[show edit update destroy] def index scope = Asset @@ -22,7 +23,6 @@ def index def show @asset = Asset.find_by_friendlier_id!(params[:id]) - authorize! :read, @asset return unless @asset.stored? @@ -32,23 +32,20 @@ def show end def edit - @asset = Asset.find_by_friendlier_id!(params[:id]) - authorize! :update, @asset end # PATCH/PUT /works/1 # PATCH/PUT /works/1.json def update - @asset = Asset.find_by_friendlier_id!(params[:id]) - authorize! :update, @asset + @document_asset = DocumentAsset.find_by_friendlier_id!(params[:id]) respond_to do |format| - if @asset.update(asset_params) - format.html { redirect_to admin_asset_url(@asset), notice: "Asset was successfully updated." } - format.json { render :show, status: :ok, location: @asset } + if @document_asset.update(document_asset_params) + format.html { redirect_to admin_document_document_assets_path(@document, @document_asset.parent), notice: "Asset was successfully updated." } + format.json { render :show, status: :ok, location: @document_asset } else format.html { render :edit } - format.json { render json: @asset.errors, status: :unprocessable_entity } + format.json { render json: @document_asset.errors, status: :unprocessable_entity } end end end @@ -188,12 +185,6 @@ def parent_path(asset) private - def set_document - return unless params[:document_id] # If not nested - - @document = Document.includes(:leaf_representative).find_by!(friendlier_id: params[:document_id]) - end - def asset_params allowed_params = [:title, :derivative_storage_type, :alt_text, :caption, :transcription, :english_translation, @@ -201,5 +192,20 @@ def asset_params allowed_params << :published if can?(:publish, @asset) params.require(:asset).permit(*allowed_params) end + + def set_document + return unless params[:document_id] # If not nested + + @document = Document.includes(:leaf_representative).find_by!(friendlier_id: params[:document_id]) + end + + def set_document_asset + @document_asset = DocumentAsset.find_by_friendlier_id(params[:id]) + end + + # Only allow a list of trusted parameters through. + def document_asset_params + params.require(:asset).permit(:title, :label, :dct_references_uri_key) + end end end diff --git a/app/models/asset.rb b/app/models/asset.rb index 032f260f..a94c2509 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -11,6 +11,7 @@ class Asset < Kithe::Asset attr_json :thumbnail, :boolean, default: "false" attr_json :derivative_storage_type, :string, default: "public" attr_json :dct_references_uri_key, :string + attr_json :label, :string DERIVATIVE_STORAGE_TYPE_LOCATIONS = { "public" => :kithe_derivatives @@ -37,4 +38,14 @@ def set_parent_dct_references_uri def remove_parent_dct_references_uri GeoblacklightAdmin::RemoveParentDctReferencesUriJob.perform_later(self) if parent_id.present? end + + # After Save Callbacks + after_save :reindex_parent + + def reindex_parent + parent.save + end end + +# Allow DocumentAsset to be used as a synonym for Asset +DocumentAsset = Asset diff --git a/app/models/document.rb b/app/models/document.rb index 89d859b1..413738cb 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -44,6 +44,10 @@ def document_assets scope.includes(:parent) end + def downloadable_assets + document_assets.select { |a| a.dct_references_uri_key == "download" } + end + include Statesman::Adapters::ActiveRecordQueries[ transition_class: DocumentTransition, initial_state: :draft @@ -117,6 +121,14 @@ def references_json references.to_json end + def asset_label(asset) + if asset.label.present? + asset.label + else + asset.title + end + end + def apply_downloads(references) dct_downloads = references["http://schema.org/downloadUrl"] # Make sure downloads exist! @@ -126,6 +138,13 @@ def apply_downloads(references) multiple_downloads << {label: download_text(send(GeoblacklightAdmin::Schema.instance.solr_fields[:format])), url: dct_downloads} end + + if downloadable_assets.present? + downloadable_assets.each do |asset| + multiple_downloads << {label: asset_label(asset), url: asset.file.url} + end + end + references[:"http://schema.org/downloadUrl"] = multiple_downloads end references diff --git a/app/views/admin/document_assets/_form.html.erb b/app/views/admin/document_assets/_form.html.erb new file mode 100644 index 00000000..1124da06 --- /dev/null +++ b/app/views/admin/document_assets/_form.html.erb @@ -0,0 +1,17 @@ +<%= @document_asset.class %> + +<%= simple_form_for [:admin, @document, @document_asset], :url => admin_document_document_asset_path(@document, @document_asset) do |f| %> + <%= f.error_notification %> + <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + +