Skip to content

Commit bac0038

Browse files
authored
Don't overwrite default opts in rich_text_area_tag (rails#43156)
You may want to use your own controller to authenticate requests or perform server-side validations.
1 parent 5438a23 commit bac0038

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

actiontext/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## Rails 7.0.0.alpha2 (September 15, 2021) ##
22

3-
* No changes.
3+
* Allow passing in a custom `direct_upload_url` or `blob_url_template` to `rich_text_area_tag`
44

5+
*Lucas Mansur*
56

67
## Rails 7.0.0.alpha1 (September 15, 2021) ##
78

actiontext/app/helpers/action_text/tag_helper.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module TagHelper
1313
# ==== Options
1414
# * <tt>:class</tt> - Defaults to "trix-content" so that default styles will be applied.
1515
# Setting this to a different value will prevent default styles from being applied.
16+
# * <tt>[:data][:direct_upload_url]</tt> - Defaults to +rails_direct_uploads_url+.
17+
# * <tt>[:data][:blob_url_template]</tt> - Defaults to <tt>rails_service_blob_url(":signed_id", ":filename")</tt>.
1618
#
1719
# ==== Example
1820
#
@@ -27,8 +29,8 @@ def rich_text_area_tag(name, value = nil, options = {})
2729
options[:class] ||= "trix-content"
2830

2931
options[:data] ||= {}
30-
options[:data][:direct_upload_url] = main_app.rails_direct_uploads_url
31-
options[:data][:blob_url_template] = main_app.rails_service_blob_url(":signed_id", ":filename")
32+
options[:data][:direct_upload_url] ||= main_app.rails_direct_uploads_url
33+
options[:data][:blob_url_template] ||= main_app.rails_service_blob_url(":signed_id", ":filename")
3234

3335
editor_tag = content_tag("trix-editor", "", options)
3436
input_tag = hidden_field_tag(name, value.try(:to_trix_html) || value, id: options[:input], form: form)
@@ -59,6 +61,8 @@ module FormHelper
5961
# ==== Options
6062
# * <tt>:class</tt> - Defaults to "trix-content" which ensures default styling is applied.
6163
# * <tt>:value</tt> - Adds a default value to the HTML input tag.
64+
# * <tt>[:data][:direct_upload_url]</tt> - Defaults to +rails_direct_uploads_url+.
65+
# * <tt>[:data][:blob_url_template]</tt> - Defaults to +rails_service_blob_url(":signed_id", ":filename")+.
6266
#
6367
# ==== Example
6468
# form_with(model: @message) do |form|

actiontext/test/template/form_helper_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,32 @@ def form_with(*, **)
154154
"</form>",
155155
output_buffer
156156
end
157+
158+
test "form with rich text area with data[direct_upload_url]" do
159+
form_with model: Message.new, scope: :message do |form|
160+
form.rich_text_area :content, data: { direct_upload_url: "http://test.host/direct_uploads" }
161+
end
162+
163+
assert_dom_equal \
164+
'<form action="/messages" accept-charset="UTF-8" method="post">' \
165+
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
166+
'<trix-editor id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
167+
"</trix-editor>" \
168+
"</form>",
169+
output_buffer
170+
end
171+
172+
test "form with rich text area with data[blob_url_template]" do
173+
form_with model: Message.new, scope: :message do |form|
174+
form.rich_text_area :content, data: { blob_url_template: "http://test.host/blobs/:signed_id/:filename" }
175+
end
176+
177+
assert_dom_equal \
178+
'<form action="/messages" accept-charset="UTF-8" method="post">' \
179+
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
180+
'<trix-editor id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/blobs/:signed_id/:filename">' \
181+
"</trix-editor>" \
182+
"</form>",
183+
output_buffer
184+
end
157185
end

0 commit comments

Comments
 (0)