Sha256: a4d5e7358f2c8d2ec43a169f12b1e08331954268f88d7b5ed1bf204f43280a21

Contents?: true

Size: 1.67 KB

Versions: 17

Compression:

Stored size: 1.67 KB

Contents

require 'action_view'
require 'active_support/core_ext'

module TrixEditorHelper
  mattr_accessor(:id, instance_accessor: false)
  class_variable_set('@@id', 0)

  def trix_editor_tag(name, value = nil, options = {})
    options.symbolize_keys!

    css_class = Array.wrap(options.delete(:class)).join(' ')
    attributes = {
      class: "formatted_content trix-content #{css_class}".squish,
      input: "trix_input_#{TrixEditorHelper.id += 1}"
    }.merge(options)

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

    input_tag + editor_tag
  end
end

module ActionView
  module Helpers
    include TrixEditorHelper

    module Tags
      class TrixEditor < Base
        include TrixEditorHelper
        delegate :dom_id, to: :'@template_object'

        def render
          options = @options.stringify_keys
          add_default_name_and_id(options)
          options['input'] ||= dom_id(object, [options['id'], :trix_input].compact.join('_'))

          value = if Rails.gem_version >= Gem::Version.new('5.2.x')
                    options.delete('value') { value_before_type_cast }
                  else
                    value_before_type_cast(object)
                  end

          trix_editor_tag(options.delete('name'), value, options)
        end
      end
    end

    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

17 entries across 17 versions & 2 rubygems

Version Path
trix-rails-2.4.0 lib/trix/form.rb
trix-rails-2.3.0 lib/trix/form.rb
trix-editmode-0.0.11 lib/trix/form.rb
trix-editmode-0.0.10 lib/trix/form.rb
trix-editmode-0.0.9 lib/trix/form.rb
trix-editmode-0.0.8 lib/trix/form.rb
trix-editmode-0.0.7 lib/trix/form.rb
trix-editmode-0.0.6 lib/trix/form.rb
trix-editmode-0.0.5 lib/trix/form.rb
trix-editmode-0.0.4 lib/trix/form.rb
trix-editmode-0.0.3 lib/trix/form.rb
trix-editmode-0.0.2 lib/trix/form.rb
trix-rails-2.2.0 lib/trix/form.rb
trix-rails-2.1.0 lib/trix/form.rb
trix-rails-2.0.0 lib/trix/form.rb
trix-rails-1.0.1 lib/trix/form.rb
trix-rails-1.0.0 lib/trix/form.rb