Module: Inkpen::EditorHelper

Defined in:
app/helpers/inkpen/editor_helper.rb

Overview

View helpers for rendering Inkpen editors in Rails views.

Include this module in your ApplicationHelper or use it directly in views that need Inkpen editors.

Examples:

Basic form field

<%= inkpen_field :post, :body %>

With configuration

<%= inkpen_field :post, :body,
    toolbar: :floating,
    extensions: [:slash_commands, :drag_handle] %>

Standalone editor

<%= inkpen_editor name: "content", value: @content %>

Author:

  • Nauman Tariq

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#inkpen_editor(name:, value: nil, **options) ⇒ String

Render an Inkpen editor with explicit configuration.

Use this when you need full control over the field name and value, or when not binding to a model.

Examples:

Basic usage

<%= inkpen_editor name: "content", value: @content %>

Full-featured

<%= inkpen_editor name: "post[body]",
    toolbar: :sticky,
    extensions: [:slash_commands, :export_commands] %>

Parameters:

  • name (String)

    the form field name

  • value (String, nil) (defaults to: nil)

    initial HTML content

  • options (Hash)

    editor configuration options

Options Hash (**options):

  • :toolbar (Symbol)

    toolbar style

  • :extensions (Array<Symbol>)

    list of extensions

  • :sticky_toolbar (StickyToolbar)

    sticky toolbar config

Returns:

  • (String)

    rendered editor HTML

Since:

  • 0.1.0



92
93
94
95
96
97
98
99
100
# File 'app/helpers/inkpen/editor_helper.rb', line 92

def inkpen_editor(name:, value: nil, **options)
  editor = Inkpen::Editor.new(
    name: name,
    value: value,
    **options
  )

  render_inkpen_editor(editor)
end

#inkpen_field(object_name, method, options = {}) ⇒ String

Render an Inkpen editor field bound to a model attribute.

This helper automatically retrieves the value from the model instance variable if available.

Examples:

Basic usage

<%= inkpen_field :post, :body %>

With extensions

<%= inkpen_field :post, :body, extensions: [:mentions, :hashtags] %>

Parameters:

  • object_name (Symbol, String)

    the model name (e.g., :post)

  • method (Symbol, String)

    the attribute name (e.g., :body)

  • options (Hash) (defaults to: {})

    editor configuration options

Options Hash (options):

  • :value (String)

    override the attribute value

  • :toolbar (Symbol)

    toolbar style (:floating, :fixed, :none)

  • :extensions (Array<Symbol>)

    list of extensions

  • :extension_config (Hash)

    per-extension config

  • :placeholder (String)

    placeholder text

Returns:

  • (String)

    rendered editor HTML

Since:

  • 0.1.0



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/inkpen/editor_helper.rb', line 49

def inkpen_field(object_name, method, options = {})
  value = options.delete(:value)

  # Try to get value from object if not provided
  if value.nil? && (object = instance_variable_get("@#{object_name}"))
    value = object.public_send(method) if object.respond_to?(method)
  end

  name = "#{object_name}[#{method}]"

  editor = Inkpen::Editor.new(
    name: name,
    value: value,
    **options
  )

  render_inkpen_editor(editor)
end