Created
October 20, 2015 14:59
-
-
Save javan/d316ff74ee189f5d2c2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module RichTextHelper | |
| cattr_accessor(:id, instance_accessor: false) { 0 } | |
| def rich_text_field_tag(name, value = nil, options = {}) | |
| options.symbolize_keys! | |
| attributes = { | |
| class: "formatted_content #{options[:class]}".squish | |
| } | |
| attributes[:autofocus] = true if options[:autofocus] | |
| attributes[:placeholder] = options[:placeholder] if options[:placeholder] | |
| attributes[:input] = options[:input] || "trix_input_#{RichTextHelper.id += 1}" | |
| attributes[:toolbar] = options[:toolbar] if options[:toolbar] | |
| editor_tag = content_tag("trix-editor", "", attributes) | |
| input_tag = hidden_field_tag(name, value, id: attributes[:input]) | |
| editor_tag + input_tag | |
| end | |
| end | |
| module ActionView | |
| module Helpers | |
| include RichTextHelper | |
| module Tags | |
| class RichText < Base | |
| include RichTextHelper | |
| 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("_")) | |
| rich_text_field_tag(options.delete("name"), value_before_type_cast(object), options) | |
| end | |
| end | |
| end | |
| module FormHelper | |
| def rich_text_field(object_name, method, options = {}) | |
| Tags::RichText.new(object_name, method, self, options).render | |
| end | |
| end | |
| class FormBuilder | |
| def rich_text_field(method, options = {}) | |
| @template.rich_text_field(@object_name, method, objectify_options(options)) | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@javan Thanks for this!
For rails master branch this did not work for me.
https://gist.github.com/javan/d316ff74ee189f5d2c2e#file-rich_text_field-rb-L36I fixed it with:
rich_text_field_tag(options.delete("name"), options.delete("value") { value_before_type_cast }, options)