1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix adding a rich_text_area to a form with no model

This commit is contained in:
George Claghorn 2018-12-31 12:16:21 -05:00
parent dce22ba477
commit f2a5c1a541
5 changed files with 65 additions and 39 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.bundle/
.byebug_history
log/*.log
node_modules/
pkg/

View file

@ -73,6 +73,7 @@ GEM
remote: https://rubygems.org/
specs:
builder (3.2.3)
byebug (10.0.2)
concurrent-ruby (1.0.5)
crass (1.0.4)
erubi (1.7.1)
@ -133,6 +134,7 @@ PLATFORMS
DEPENDENCIES
actiontext!
bundler (~> 1.15)
byebug
mini_magick
rails!
sqlite3

View file

@ -22,6 +22,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "mini_magick"
s.add_development_dependency "sqlite3"
s.add_development_dependency "webpacker", "~> 3.2.2"
s.add_development_dependency "byebug"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- test/*`.split("\n")

View file

@ -40,7 +40,7 @@ module ActionView::Helpers
def render
options = @options.stringify_keys
add_default_name_and_id(options)
options["input"] ||= dom_id(object, [options["id"], :trix_input].compact.join("_"))
options["input"] ||= dom_id(object, [options["id"], :trix_input].compact.join("_")) if object
@template_object.rich_text_area_tag(options.delete("name"), editable_value, options)
end

View file

@ -5,45 +5,67 @@ require 'test_helper'
class ActionText::FormHelperTest < ActionView::TestCase
tests ActionText::TagHelper
test "form with rich text area" do
form_with model: Message.new, scope: :message do |form|
form.rich_text_area :content
end
assert_dom_equal \
'<form action="/messages" accept-charset="UTF-8" data-remote="true" method="post">' \
'<input name="utf8" type="hidden" value="&#x2713;" />' \
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
'<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/rails/active_storage/blobs/:signed_id/:filename">' \
'</trix-editor>' \
'</form>',
output_buffer
end
test "form with rich text area having class" do
form_with model: Message.new, scope: :message do |form|
form.rich_text_area :content, class: "custom-class"
end
assert_dom_equal \
'<form action="/messages" accept-charset="UTF-8" data-remote="true" method="post">' \
'<input name="utf8" type="hidden" value="&#x2713;" />' \
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
'<trix-editor id="message_content" input="message_content_trix_input_message" class="custom-class" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/:signed_id/:filename">' \
'</trix-editor>' \
'</form>',
output_buffer
end
test "form with rich text area for non-attribute" do
form_with model: Message.new, scope: :message do |form|
form.rich_text_area :not_an_attribute
end
assert_dom_equal \
'<form action="/messages" accept-charset="UTF-8" data-remote="true" method="post">' \
'<input name="utf8" type="hidden" value="&#x2713;" />' \
'<input type="hidden" name="message[not_an_attribute]" id="message_not_an_attribute_trix_input_message" />' \
'<trix-editor id="message_not_an_attribute" input="message_not_an_attribute_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/rails/active_storage/blobs/:signed_id/:filename">' \
'</trix-editor>' \
'</form>',
output_buffer
end
test "modelless form with rich text area" do
form_with url: "/messages", scope: :message do |form|
form.rich_text_area :content
end
assert_dom_equal \
'<form action="/messages" accept-charset="UTF-8" data-remote="true" method="post">' \
'<input name="utf8" type="hidden" value="&#x2713;" />' \
'<input type="hidden" name="message[content]" id="trix_input_1" />' \
'<trix-editor id="message_content" input="trix_input_1" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/:signed_id/:filename">' \
'</trix-editor>' \
'</form>',
output_buffer
end
def form_with(*)
@output_buffer = super
end
test "rich_text_area doesn't raise when attributes don't exist in the model" do
assert_nothing_raised do
form_with(model: Message.new, scope: :message, id: "create-message") do |form|
form.rich_text_area(:not_an_attribute)
end
end
assert_match "message[not_an_attribute]", output_buffer
end
test "form with rich_text_area" do
expected = '<form id="create-message" action="/messages" accept-charset="UTF-8" data-remote="true" method="post">'\
'<input name="utf8" type="hidden" value="&#x2713;" />'\
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />'\
'<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/rails/active_storage/blobs/:signed_id/:filename">'\
'</trix-editor></form>'
form_with(model: Message.new, scope: :message, id: "create-message") do |form|
form.rich_text_area(:content)
end
assert_dom_equal expected, output_buffer
end
test "form with rich_text_area providing class option" do
expected = '<form id="create-message" action="/messages" accept-charset="UTF-8" data-remote="true" method="post">'\
'<input name="utf8" type="hidden" value="&#x2713;" />'\
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />'\
'<trix-editor id="message_content" input="message_content_trix_input_message" class="custom-class" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/:signed_id/:filename">'\
'</trix-editor></form>'
form_with(model: Message.new, scope: :message, id: "create-message") do |form|
form.rich_text_area(:content, class: "custom-class")
end
assert_dom_equal expected, output_buffer
end
end