Module: AdminTools::Helper

Defined in:
lib/admin_tools/helper.rb

Instance Method Summary collapse

Instance Method Details

#admin_tool(class_name = "", element = nil, **options) { ... } ⇒ Object

Renders content only for admin users

Examples:

Basic usage

<% admin_tool do %>
  <%= link_to "Delete", resource_path, method: :delete %>
<% end %>

With CSS classes

<% admin_tool("w-fit bg-red-50 p-2") do %>
  <p>Admin-only content</p>
<% end %>

With different wrapper element

<% admin_tool("inline-flex", :span) do %>
  Admin badge
<% end %>

With HTML attributes

<% admin_tool("", :div, data: { controller: "admin" }) do %>
  <%= render "admin/panel" %>
<% end %>

Parameters:

  • class_name (String) (defaults to: "")

    Additional CSS classes to add to the wrapper

  • element (Symbol, String) (defaults to: nil)

    HTML element to wrap content in (default: configured wrapper_element)

  • options (Hash)

    Additional HTML attributes for the wrapper element

Yields:

  • Block containing the content to render for admins



32
33
34
35
36
37
38
39
# File 'lib/admin_tools/helper.rb', line 32

def admin_tool(class_name = "", element = nil, **options, &block)
  return unless admin_tools_visible?

  element ||= AdminTools.configuration.wrapper_element
  css_classes = [AdminTools.configuration.css_class, class_name].reject(&:blank?).join(" ")

  concat (element, class: css_classes, **options, &block)
end

#admin_tool_if(condition, *args, **options) { ... } ⇒ Object

Conditionally renders content for admins only based on a condition

If condition is false, content is shown to ALL users. If condition is true, content is only shown to admins.

Examples:

Show draft badge to everyone, but admin actions only to admins

<% admin_tool_if(@post.published?) do %>
  <%= link_to "Unpublish", unpublish_path(@post) %>
<% end %>

Parameters:

  • condition (Boolean)

    When true, restrict to admins only

  • args (Array)

    Arguments passed to admin_tool

  • options (Hash)

    Options passed to admin_tool

Yields:

  • Block containing the content



56
57
58
59
60
# File 'lib/admin_tools/helper.rb', line 56

def admin_tool_if(condition, *args, **options, &block)
  yield and return unless condition

  admin_tool(*args, **options, &block)
end

#admin_tools_visible?Boolean

Returns whether admin tools should be visible to the current user

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/admin_tools/helper.rb', line 66

def admin_tools_visible?
  user = send(AdminTools.configuration.current_user_method)
  return false if user.nil?

  user.send(AdminTools.configuration.admin_method)
rescue NoMethodError
  false
end