From ba71c73836c084c95ca89021f9c9a455d8c94134 Mon Sep 17 00:00:00 2001 From: Javan Makhmali <javan@javan.us> Date: Wed, 14 Feb 2018 14:11:54 -0500 Subject: Move hard coded URLs to computed data attributes --- app/helpers/active_text/tag_helper.rb | 8 +++++--- app/javascript/activetext/index.js | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/helpers/active_text/tag_helper.rb b/app/helpers/active_text/tag_helper.rb index c2ec7e5e8f..590ff93fd5 100644 --- a/app/helpers/active_text/tag_helper.rb +++ b/app/helpers/active_text/tag_helper.rb @@ -4,7 +4,11 @@ module ActiveText def active_text_field_tag(name, value = nil, options = {}) options = options.symbolize_keys + options[:input] ||= "trix_input_#{ActiveText::TagHelper.id += 1}" + options[:data] ||= {} + options[:data][:direct_upload_url] = rails_direct_uploads_url + options[:data][:blob_url_template] = rails_service_blob_url(":signed_id", ":filename") editor_tag = content_tag("trix-editor", "", options) input_tag = hidden_field_tag(name, value, id: options[:input]) @@ -16,15 +20,13 @@ end module ActionView::Helpers class Tags::ActiveText < Tags::Base - include ActiveText::TagHelper - delegate :dom_id, to: ActionView::RecordIdentifier def render options = @options.stringify_keys add_default_name_and_id(options) options["input"] ||= dom_id(object, [options["id"], :trix_input].compact.join("_")) - active_text_field_tag(options.delete("name"), editable_value, options) + @template_object.active_text_field_tag(options.delete("name"), editable_value, options) end def editable_value diff --git a/app/javascript/activetext/index.js b/app/javascript/activetext/index.js index cdcd1d311f..e1c59dd50d 100644 --- a/app/javascript/activetext/index.js +++ b/app/javascript/activetext/index.js @@ -1,14 +1,12 @@ import * as Trix from "trix" import { DirectUpload } from "activestorage" -// FIXME: Hard coded routes -const directUploadsURL = "/rails/active_storage/direct_uploads" -const blobsURL = "/rails/active_storage/blobs" - addEventListener("trix-attachment-add", event => { const { attachment } = event if (!attachment.file) return + const { directUploadUrl, blobUrlTemplate } = event.target.dataset + const delegate = { directUploadWillStoreFileWithXHR: (xhr) => { xhr.upload.addEventListener("progress", event => { @@ -18,17 +16,19 @@ addEventListener("trix-attachment-add", event => { } } - const directUpload = new DirectUpload(attachment.file, directUploadsURL, delegate) + const directUpload = new DirectUpload(attachment.file, directUploadUrl, delegate) directUpload.create((error, attributes) => { if (error) { console.warn("Failed to store file for attachment", attachment, error) } else { - console.log("Created blob for attachment", attributes, attachment) - attachment.setAttributes({ - url: `${blobsURL}/${attributes.signed_id}/${encodeURIComponent(attachment.file.name)}`, - sgid: attributes.attachable_sgid - }) + const sgid = attributes.attachable_sgid + + const url = blobUrlTemplate + .replace(":signed_id", attributes.signed_id) + .replace(":filename", encodeURIComponent(attributes.filename)) + + attachment.setAttributes({ sgid, url }) } }) }) -- cgit v1.2.3