Skip to content

Commit

Permalink
Action Text: call #to_trix_html on rich_text_area_tag value
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaynikam authored Feb 28, 2021
1 parent 14ff158 commit adc5eb6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
8 changes: 2 additions & 6 deletions actiontext/app/helpers/action_text/tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def rich_text_area_tag(name, value = nil, options = {})
options[:data][:blob_url_template] = main_app.rails_service_blob_url(":signed_id", ":filename")

editor_tag = content_tag("trix-editor", "", options)
input_tag = hidden_field_tag(name, value, id: options[:input])
input_tag = hidden_field_tag(name, value.try(:to_trix_html) || value, id: options[:input])

input_tag + editor_tag
end
Expand All @@ -46,11 +46,7 @@ def render
options = @options.stringify_keys
add_default_name_and_id(options)
options["input"] ||= dom_id(object, [options["id"], :trix_input].compact.join("_")) if object
@template_object.rich_text_area_tag(options.delete("name"), options.fetch("value") { editable_value }, options.except("value"))
end

def editable_value
value&.body.try(:to_trix_html)
@template_object.rich_text_area_tag(options.delete("name"), options.fetch("value") { value }, options.except("value"))
end
end

Expand Down
4 changes: 4 additions & 0 deletions actiontext/app/models/action_text/rich_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def to_plain_text
body&.to_plain_text.to_s
end

def to_trix_html
body&.to_trix_html
end

delegate :blank?, :empty?, :present?, to: :to_plain_text
end
end
Expand Down
22 changes: 19 additions & 3 deletions actiontext/test/template/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ def form_with(*, **)
)
end

test "rich text area tag" do
message = Message.new

form_with model: message, scope: :message do |form|
rich_text_area_tag :content, message.content, { input: "trix_input_1" }
end

assert_dom_equal \
'<form action="/messages" accept-charset="UTF-8" data-remote="true" method="post">' \
'<input type="hidden" name="content" id="trix_input_1" />' \
'<trix-editor 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/redirect/:signed_id/:filename">' \
"</trix-editor>" \
"</form>",
output_buffer
end

test "form with rich text area" do
form_with model: Message.new, scope: :message do |form|
form.rich_text_area :content
Expand Down Expand Up @@ -69,13 +85,13 @@ def form_with(*, **)

test "modelless form with rich text area" do
form_with url: "/messages", scope: :message do |form|
form.rich_text_area :content
form.rich_text_area :content, { input: "trix_input_2" }
end

assert_dom_equal \
'<form action="/messages" accept-charset="UTF-8" data-remote="true" method="post">' \
'<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/redirect/:signed_id/:filename">' \
'<input type="hidden" name="message[content]" id="trix_input_2" />' \
'<trix-editor id="message_content" input="trix_input_2" 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/redirect/:signed_id/:filename">' \
"</trix-editor>" \
"</form>",
output_buffer
Expand Down

0 comments on commit adc5eb6

Please sign in to comment.