Module: MatViews::Admin::UiHelper

Defined in:
app/helpers/mat_views/admin/ui_helper.rb

Overview

MatViews::Admin::UiHelper


View helper methods for the MatViews admin UI.

Responsibilities:

  • Provides consistent button, link, drawer, badge, and icon components.

  • Wraps standard Rails helpers (‘link_to`, `button_to`, `button_tag`, etc.) with MatViews-specific styling and Stimulus integration.

  • Defines inline SVG icon snippets for use across the admin dashboard.

Key Components:

Instance Method Summary collapse

Instance Method Details

#mv_badge(status, text) ⇒ String

Renders a badge element styled by status.

Parameters:

  • status (String, Symbol)

    status name (‘success`, `running`, `failed`, etc.)

  • text (String)

    text to display inside the badge

Returns:

  • (String)

    HTML-safe span tag with badge classes



168
169
170
171
172
173
174
175
176
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 168

def mv_badge(status, text)
  klass = case status.to_s.downcase
          when 'success' then 'mv-badge mv-badge--success'
          when 'running' then 'mv-badge mv-badge--running'
          when 'failed'  then 'mv-badge mv-badge--failed'
          else 'mv-badge'
          end
  (:span, text, class: klass)
end

#mv_button_classes(variant, size) ⇒ String

Builds CSS classes for a MatViews-styled button.

Parameters:

  • variant (Symbol)

    one of ‘:primary`, `:secondary`, `:ghost`, `:negative`

  • size (Symbol)

    one of ‘:sm`, `:md`, `:lg`

Returns:

  • (String)

    concatenated CSS class string



33
34
35
36
37
38
39
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 33

def mv_button_classes(variant, size)
  [
    'mv-btn',
    "mv-btn--#{variant}",
    "mv-btn--#{size}"
  ].join(' ')
end

Renders a styled link button.

Parameters:

  • href (String)

    target URL

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

    options for styling, data attributes, etc.

Yields:

  • link body content

Returns:

  • (String)

    HTML-safe link tag



47
48
49
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 47

def mv_button_link(href, opts = {}, &)
  link_to capture(&), href, **link_options(assign_test_id(opts))
end

#mv_button_to(href, opts = {}) { ... } ⇒ String

Renders a styled ‘button_to` element.

Parameters:

  • href (String)

    target URL

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

    options for styling, data attributes, etc.

Yields:

  • button content

Returns:

  • (String)

    HTML-safe button tag



70
71
72
73
74
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 70

def mv_button_to(href, opts = {}, &)
  button_to href, **link_options(assign_test_id(opts)) do
    capture(&)
  end
end

#mv_cancel_button(opts = {}) { ... } ⇒ String

Renders a styled cancel button.

Parameters:

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

    options for styling, data attributes, etc.

Yields:

  • button content

Returns:

  • (String)

    HTML-safe button tag



217
218
219
220
221
222
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 217

def mv_cancel_button(opts = {}, &)
  opts = link_options(assign_test_id(opts))
  opts[:type] = 'button'
  opts.delete(:method)
  button_tag(capture(&), **opts)
end

#mv_drawer_action_button(label, action, tooltip = nil, tooltip_placement = nil, args_org = {}) { ... } ⇒ String

Renders a drawer action button with optional tooltip.

Parameters:

  • label (String)

    ARIA label for accessibility

  • action (String)

    Stimulus action method

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

    optional tooltip text

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

    placement of tooltip (default: “top”)

  • args_orig (Hash)

    additional HTML options

Yields:

  • button content

Returns:

  • (String)

    HTML-safe button tag



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 85

def mv_drawer_action_button(label, action, tooltip = nil, tooltip_placement = nil, args_org = {}, &)
  args = assign_test_id(args_org)
  data = { action: "drawer##{action}" }
  data = data.merge(args[:data] || {})
  if tooltip
    data[:controller] = 'tooltip'
    data[:'tooltip-text-value'] = tooltip
    data[:'tooltip-placement'] = tooltip_placement || 'top'
  end

  args[:data] = data

  button_tag(type: 'button', class: 'mv-drawer-action', 'aria-label': label, **args, &)
end

Renders a button link that opens a drawer via Stimulus.

Parameters:

  • drawer_url (String)

    URL to load into the drawer

  • drawer_title (String)

    title for the drawer

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

    additional options

Yields:

  • button body

Returns:

  • (String)

    HTML-safe link tag



58
59
60
61
62
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 58

def mv_drawer_link(drawer_url, drawer_title, args = {}, &)
  data = { action: 'click->drawer#open', drawer_title: drawer_title, drawer_url: drawer_url }
  args[:data] = (args[:data] || {}).merge(data)
  mv_button_link '#', assign_test_id(args), &
end

#mv_icon(name, size: 16, class_name: nil) ⇒ String

Renders an inline SVG icon.

Parameters:

  • name (String, Symbol)

    icon name (method suffix after ‘svg_icon_`)

  • size (Integer) (defaults to: 16)

    icon width/height in pixels

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

    optional extra CSS class

Returns:

  • (String)

    HTML-safe SVG element



184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 184

def mv_icon(name, size: 16, class_name: nil)
  icon_method_name = :"svg_icon_#{name}"
   :svg,
              class: ['mv-icon', class_name].compact.join(' '),
              xmlns: 'http://www.w3.org/2000/svg',
              width: size, height: size, viewBox: '0 0 24 24',
              fill: 'none', stroke: 'currentColor',
              'stroke-width': '2', 'stroke-linecap': 'round', 'stroke-linejoin': 'round',
              'aria-hidden': 'true' do
    respond_to?(icon_method_name, true) ? raw(send(icon_method_name)) : ''.html_safe
  end
end

Renders a styled external or internal link, with optional tooltip.

Parameters:

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

    link text (nil if using block form)

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

    target URL

  • args_orig (Hash) (defaults to: nil)

    HTML options (supports ‘:tooltip`, `:is_blank`)

Yields:

  • link body when block form is used

Returns:

  • (String)

    HTML-safe link tag



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 107

def mv_link_to(text = nil, url = nil, args_orig = nil, &block)
  args = assign_test_id(args_orig || {})
  if block_given?
    args = assign_test_id(url || {})
    url = text
  end

  tooltip = args.fetch(:tooltip, nil)
  is_blank = args.fetch(:is_blank, true)
  underline = args.fetch(:underline, true)
  args_to_apply = args.except(:tooltip, :is_blank)
  if is_blank
    args_to_apply[:target] = '_blank'
    args_to_apply[:rel] = 'noopener noreferrer'
  end
  args_to_apply[:class] = 'underline' if underline
  if tooltip
    args_to_apply[:'data-controller'] = 'tooltip'
    args_to_apply[:'data-tooltip-text-value'] = tooltip
  end

  if block_given?
    link_to url, args_to_apply do
      capture(&block)
    end
  else
    link_to text, url, **args_to_apply
  end
end

#mv_submit_button(opts = {}) { ... } ⇒ String

Renders a styled submit button.

Parameters:

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

    options for styling, data attributes, etc.

Yields:

  • button content

Returns:

  • (String)

    HTML-safe button tag



204
205
206
207
208
209
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 204

def mv_submit_button(opts = {}, &)
  opts = link_options(assign_test_id(opts))
  opts[:type] = 'submit'
  opts.delete(:method)
  button_tag(capture(&), **opts)
end

#mv_t(key, **args) ⇒ String

Shortcut for translating keys under the ‘mat_views` namespace.

Parameters:

  • key (String, Symbol)

    translation key under ‘mat_views.*`

  • args (Hash)

    interpolation values

Returns:

  • (String)

    translated string



159
160
161
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 159

def mv_t(key, **args)
  I18n.t("mat_views.#{key}", **args)
end

Renders a tab link for the tabs component.

Parameters:

  • tab_name (String)

    unique name of the tab

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

    additional HTML options

Yields:

  • tab link content

Returns:

  • (String)

    HTML-safe link tag



144
145
146
147
148
149
150
151
152
# File 'app/helpers/mat_views/admin/ui_helper.rb', line 144

def mv_tab_link(tab_name, args_org = {}, &)
  args = assign_test_id(args_org)
  classes = ['mv-tab']
  selected = args.delete(:selected)
  classes << (selected ? 'mv-tab--on' : '')
  args[:class] = [args[:class], classes.compact.join(' ')].compact.join(' ').strip
  args[:data] = args.fetch(:data, {}).merge({ action: 'click->tabs#show', 'tabs-target': 'link', name: tab_name })
  link_to '#', **args, &
end