Sha256: e44fb9f197b7411030532594e0f68cfcdb6a8ca442374a6cb83ed5422b2ea7f9

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module ActionView
  module Helpers
    module Tags
      class TrixEditor < Base
        delegate :dom_id, to: ActionView::RecordIdentifier

        def render
          options = @options.stringify_keys
          add_default_name_and_id(options)
          options['input'] ||= generate_trix_unique_id
          name = options.delete('name')
          value = value_before_type_cast(object)
          options.symbolize_keys!

          attributes = { class: "formatted_content #{options[:class]}".squish }
          attributes[:autofocus] = true if options[:autofocus]
          attributes[:placeholder] = options[:placeholder]
          attributes[:input] = options.fetch(:input) { "trix_input" }

          editor_tag = content_tag('trix-editor', '', attributes)
          input_tag = hidden_field_tag(name, value, id: attributes[:input])

          editor_tag + input_tag
        end

        private

        def generate_trix_unique_id
          tag_id + "_trix_input"
        end
      end
    end

    # Monkey patch the trix editor helper into Rails' FormBuilder
    module FormHelper
      def trix_editor(object_name, method, options = {})
        Tags::TrixEditor.new(object_name, method, self, options).render
      end
    end

    class FormBuilder
      def trix_editor(method, options = {})
        @template.trix_editor(@object_name, method, objectify_options(options))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
trix_rails-0.1.0 lib/core_extensions/action_view/helpers/tags/trix_editor.rb
trix_editor_helper-0.1.0 lib/core_extensions/action_view/helpers/tags/trix_editor.rb