Skip to content

Commit

Permalink
Document Assets: updates per Karen
Browse files Browse the repository at this point in the history
* Allow asset label editing
* Add downloadable assets to multiple downloads

Addresses #69
  • Loading branch information
ewlarson committed Jul 24, 2024
1 parent b4155f5 commit ea67244
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 16 deletions.
36 changes: 21 additions & 15 deletions app/controllers/admin/document_assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +23,6 @@ def index

def show
@asset = Asset.find_by_friendlier_id!(params[:id])
authorize! :read, @asset

return unless @asset.stored?

Expand All @@ -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
Expand Down Expand Up @@ -188,18 +185,27 @@ 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,
:role, {admin_note_attributes: []}, :dct_references_for]
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
11 changes: 11 additions & 0 deletions app/models/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
19 changes: 19 additions & 0 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions app/views/admin/document_assets/_form.html.erb
Original file line number Diff line number Diff line change
@@ -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? %>

<div class="form-inputs">
<%= f.input :parent_id, input_html: { value: @document.friendlier_id, readonly: true } %>
<%= f.input :title %>
<%= f.input :label %>
<%= f.input :dct_references_uri_key, collection: I18n.t("activemodel.enum_values.document/reference.category").invert.sort.insert(0, ["Choose Reference Type", nil]), label: 'Reference' %>
</div>

<div class="form-actions">
<%= f.button :submit, 'Save Asset', {class: 'btn btn-primary'} %>
</div>
<% end %>
5 changes: 5 additions & 0 deletions app/views/admin/document_assets/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Editing Document Asset</h1>

<%= render 'form', document_asset: @document_asset %>

<%= link_to 'Back', admin_document_document_assets_path(@document) %>
7 changes: 6 additions & 1 deletion app/views/admin/document_assets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<th>Thumbnail</th>
<th>Preview</th>
<th>Title</th>
<th>Label</th>
<th>MimeType</th>
<th>Reference URI Type</th>
<th>Actions</th>
Expand All @@ -58,14 +59,18 @@
<% end %>
</td>
<td><%= link_to(document_asset.title, document_asset.file.url) %></td>
<td><%= document_asset.label %></td>
<td>
<%= document_asset.file_data["metadata"]["mime_type"] %>
<span class="sr-only"><%= document_asset.inspect %></span>
</td>
<td>
<%= document_asset.dct_references_uri_key %>
</td>
<td><%= link_to 'Destroy', admin_document_document_asset_path(document_asset.parent, document_asset), method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td>
<%= link_to 'Edit', edit_admin_document_document_asset_path(document_asset.parent, document_asset) %>
<%= link_to 'Destroy', admin_document_document_asset_path(document_asset.parent, document_asset), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
</tbody>
Expand Down

0 comments on commit ea67244

Please sign in to comment.