Module: ClipboardHelper

Defined in:
app/helpers/clipboard_helper.rb

Overview

Unified clipboard copy functionality.
Provides consistent copy-to-clipboard behavior across the application.

All modes include visual feedback on click via CSS animation.

Link behavior:

  • If url: is provided → automatically copies as rich HTML link
  • Plain text always copies the display text (not the URL)
  • With include_url_in_text: true → plain text includes URL: "Text "

Examples:

Icon button (default) - copies plain text

<%= clipboard_copy(@text) %>

Text button - shows text, copies plain text

<%= clipboard_copy(@reference_number, mode: :text) %>

Text button with rich link (plain text = "TTK51040", rich = link)

<%= clipboard_copy(@reference_number, mode: :text, url: order_url(@order)) %>

Include URL in plain text copy ("TTK51040 https://...")

<%= clipboard_copy(@reference_number, mode: :text, url: order_url(@order), include_url_in_text: true) %>

Display label but copy different value

<%= clipboard_copy('Copy Link', mode: :text, copy_value: @url) %>

Custom button class

<%= clipboard_copy(@text, button_class: 'btn btn-primary') %>

Instance Method Summary collapse

Instance Method Details

#clipboard_copy(text, mode: :icon, url: nil, link_text: nil, copy_value: nil, include_url_in_text: false, button_class: nil, title: nil) ⇒ String

Renders a clipboard copy button with visual feedback.

Parameters:

  • text (String)

    The text to display (and copy if mode: :text and no copy_value)

  • mode (Symbol) (defaults to: :icon)

    :icon (default) shows copy icon, :text shows the text as button

  • url (String) (defaults to: nil)

    URL for rich link (if provided, automatically copies as rich link)

  • link_text (String) (defaults to: nil)

    Display text in the copied link (defaults to text param)

  • copy_value (String) (defaults to: nil)

    Explicit value to copy (overrides default text/link_text)

  • include_url_in_text (Boolean) (defaults to: false)

    Include URL in plain text copy (default false)

  • button_class (String) (defaults to: nil)

    CSS class for the button

  • title (String) (defaults to: nil)

    Tooltip text

Returns:

  • (String)

    Rendered HTML



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/clipboard_helper.rb', line 43

def clipboard_copy(text, mode: :icon, url: nil, link_text: nil, copy_value: nil, include_url_in_text: false, button_class: nil, title: nil)
  return nil if text.blank?

  render Crm::ClipboardButtonComponent.new(
    text: text,
    mode: mode,
    url: url,
    link_text: link_text,
    copy_value: copy_value,
    include_url_in_text: include_url_in_text,
    button_class: button_class,
    title: title
  )
end