Module: SMEditor::Rails::FormHelper

Defined in:
lib/smeditor/rails/form_helper.rb

Overview

View helper that mounts a SMEditor editor inside a Rails form.

<%= form.smeditor_editor :content %>      # via FormBuilder
<%= smeditor_editor(form, :content) %>    # standalone

Emits a hidden field carrying the document HTML plus an empty mount point. The boot script (smeditor.js) finds the mount, attaches a real editor seeded from the hidden field, and writes editor.getHTML() back into that field on every change — so a normal form submit persists the content with no extra wiring.

Instance Method Summary collapse

Instance Method Details

#smeditor_editor(form, method, options = {}) ⇒ Object

form - a Rails FormBuilder method - the model attribute (stored as an HTML string) options - :kit ("starter" | "full"), :class, :placeholder, :upload_url



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/smeditor/rails/form_helper.rb', line 19

def smeditor_editor(form, method, options = {})
  object = form.object
  current = object.respond_to?(method) ? object.public_send(method) : nil
  kit = (options[:kit] || SMEditor.config.default_kit).to_s
  upload_url = options.fetch(:upload_url, smeditor_upload_url)

  hidden = form.hidden_field(
    method,
    value: current.to_s,
    data: { smeditor_input: true },
  )

  mount = (
    :div,
    "",
    class: "smeditor-mount",
    data: {
      smeditor: true,
      smeditor_kit: kit,
      smeditor_placeholder: options[:placeholder],
      smeditor_upload_url: upload_url,
    },
  )

  wrapper_class = ["smeditor-field", options[:class]].compact.join(" ")
  (:div, safe_join([hidden, mount]), class: wrapper_class)
end