Class: Spree::AgentTools::CreateMedia
- Inherits:
-
Spree::AgentTool
- Object
- Spree::AgentTool
- Spree::AgentTools::CreateMedia
- Defined in:
- app/tools/spree/agent_tools/create_media.rb
Overview
Puts a file into the media library, optionally on a product.
Creating media needs a file, which an agent had no way to supply until
upload_file existed — which is why the resource reached the map
read-only. The id that tool returns is what this one takes.
A row with no owner is a library file: uploaded, not yet placed. Naming a product places it in that product's gallery, which is what a merchant handing over photographs usually means.
Constant Summary
Constants inherited from Spree::AgentTool
Spree::AgentTool::PARENT_ID_DESCRIPTION
Instance Attribute Summary
Attributes inherited from Spree::AgentTool
Instance Method Summary collapse
Methods inherited from Spree::AgentTool
additive!, audience, description, destructive?, #initialize, input_schema, mutating!, mutating?, param, params, permission, #permitted?, tool_name, #unknown_resource
Constructor Details
This class inherits a constructor from Spree::AgentTool
Instance Method Details
#call(file_id:, product_id: nil, alt: nil) ⇒ Object
25 26 27 28 29 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 61 62 63 64 |
# File 'app/tools/spree/agent_tools/create_media.rb', line 25 def call(file_id:, product_id: nil, alt: nil) # Placement is the path: nested under a product puts the file in that # product's gallery, the flat one leaves it in the library. The # endpoint resolves the parent, checks it and verifies the signed id, # so there is nothing to look up here first. path = if product_id.present? "/api/v3/admin/products/#{ERB::Util.url_encode(product_id)}/media" else '/api/v3/admin/media' end response = ApiDispatch.new(context).call( method: :post, path: path, body: { signed_id: file_id, alt: alt.presence }.compact ) unless response.success? # The endpoint speaks about signed references in general, since many # of its parameters are one. Here it is always a file, so the # refusal names the tool that produces a valid id. = response. if response.status == 422 && .to_s.match?(/signed reference/i) = "#{file_id.inspect} is not a file this store uploaded, or it has expired. " \ 'Call upload_file first and pass the file_id it returns.' end return { error: } end media = response.data.to_h { ok: true, id: media['id'], filename: media['filename'], media_type: media['media_type'], product_id: product_id.presence, # The media library has no page of its own in the map, so the link # that helps is the product's — where the merchant will look at it. dashboard_path: product_id.presence && dashboard_path_for(product_id) }.compact end |
#summary(arguments) ⇒ Object
66 67 68 69 70 |
# File 'app/tools/spree/agent_tools/create_media.rb', line 66 def summary(arguments) return "Add a file to #{arguments[:product_id]}'s gallery" if arguments[:product_id].present? 'Add a file to the media library' end |