Module: Hydra::AssetsControllerHelper
- Included in:
- Controller::AssetsControllerBehavior
- Defined in:
- lib/hydra/assets_controller_helper.rb
Overview
TODO: All of this should be moved into module Hydra::Assets and this file should be eliminated.
Instance Method Summary collapse
-
#apply_depositor_metadata(asset) ⇒ Object
This makes sure that assets have the current user marked as their depositor/owner.
-
#sanitize_update_params ⇒ Hash
Builds a Hash that you can feed into ActiveFedora::Base.update_datstream_attributes If params is empty, returns an empty Hash.
- #set_collection_type(asset, collection) ⇒ Object
-
#tidy_response_from_update(response_from_update) ⇒ Hash
Tidies up the response from updating the document, making it more JSON-friendly.
-
#update_document(document, params) ⇒ Object
Updates the document based on the provided parameters This method can be overridden to perform additional update work in the hydra head.
Instance Method Details
#apply_depositor_metadata(asset) ⇒ Object
This makes sure that assets have the current user marked as their depositor/owner.
Relies on the asset’s model to define what apply_depositor_metadata should do. Most Hydra assets will rely on ModelMethods#apply_depositor_metadata
Attempts to call apply_depositor_metadata on the asset
, passing user_key as the depositor Does nothing if the asset doesn’t respond to .apply_depositor_metadata
11 12 13 14 15 |
# File 'lib/hydra/assets_controller_helper.rb', line 11 def (asset) if asset.respond_to?(:apply_depositor_metadata) && user_key asset.(user_key) end end |
#sanitize_update_params ⇒ Hash
Builds a Hash that you can feed into ActiveFedora::Base.update_datstream_attributes If params is empty, returns an empty Hash
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/hydra/assets_controller_helper.rb', line 30 def sanitize_update_params @sanitized_params ||= {} unless params["asset"].nil? params["asset"].each_pair do |datastream_name,fields| @sanitized_params[datastream_name] = {} # TEMPORARY HACK: special case for supporting textile if params["field_id"]=="abstract_0" params[:field_selectors] = {"descMetadata" => {"abstract" => [:abstract]}} end if params.fetch("field_selectors",false) && params["field_selectors"].fetch(datastream_name, false) # If there is an entry in field_selectors for the datastream (implying a nokogiri datastream), retrieve the field_selector for this field. # if no field selector, exists, use the field name fields.each_pair do |field_name,field_values| parent_select = OM.destringify( params["field_selectors"][datastream_name].fetch(field_name, field_name) ) # calling on unique because duplicate keys can be problematic for multi-valued fields. (HYDRA-785) parent_select.uniq! if parent_select.respond_to?(:uniq!) @sanitized_params[datastream_name][parent_select] = field_values end else @sanitized_params[datastream_name] = unescape_keys(params[:asset][datastream_name]) end end end return @sanitized_params end |
#set_collection_type(asset, collection) ⇒ Object
17 18 19 20 21 |
# File 'lib/hydra/assets_controller_helper.rb', line 17 def set_collection_type(asset, collection) if asset.respond_to?(:set_collection_type) asset.set_collection_type(collection) end end |
#tidy_response_from_update(response_from_update) ⇒ Hash
Tidies up the response from updating the document, making it more JSON-friendly
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hydra/assets_controller_helper.rb', line 65 def tidy_response_from_update(response_from_update) response = Hash["updated"=>[]] last_result_value = "" response_from_update.each_pair do |field_name,changed_values| changed_values.each_pair do |index,value| response["updated"] << {"field_name"=>field_name,"index"=>index,"value"=>value} last_result_value = value end end # If handling submission from jeditable (which will only submit one value at a time), return the value it submitted if params.has_key?(:field_id) response = last_result_value end return response end |
#update_document(document, params) ⇒ Object
Updates the document based on the provided parameters This method can be overridden to perform additional update work in the hydra head.
86 87 88 89 90 91 |
# File 'lib/hydra/assets_controller_helper.rb', line 86 def update_document(document, params) # this will only work if there is only one datastream being updated. # once ActiveFedora::MetadataDatastream supports .update_datastream_attributes, use that method instead (will also be able to pass through params["asset"] as-is without usin prep_updater_method_args!) # result = document.update_indexed_attributes(params[:params], params[:opts]) result = document.update_datastream_attributes(params) end |