module Alchemy module Admin # Renders a toolbar button for the Alchemy toolbar # # == Example # # <%= render Alchemy::Admin::ToolbarButton.new( # url: new_resource_path, # icon: :plus, # label: 'Create Resource', # hotkey: 'alt+n', # dialog_options: { # title: 'Create Resource', # size: "430x400" # }, # if_permitted_to: [:create, resource_model] # ) %> # # @param [String] :url # Url for link. # @param [String] :icon # Icon name. See https://remixicon.com for available icons. # @param [String] :label # Text for button tooltip. # @param [String] :hotkey # Keyboard shortcut for this button. I.E +alt-n+ # @param [Hash] :dialog_options # Overlay options. See link_to_dialog helper. # @param [Array] :if_permitted_to ([:action, :controller]) # Check permission for button. Exactly how you defined the permission in your +authorization_rules.rb+. Defaults to controller and action from button url. # @param [Boolean] :skip_permission_check (false) # Skip the permission check. NOT RECOMMENDED! # class ToolbarButton < ViewComponent::Base erb_template <<-ERB
ERB delegate :can?, :link_to, :link_to_dialog, :render_icon, to: :helpers attr_reader :url, :icon, :label, :hotkey, :dialog, :dialog_options, :skip_permission_check, :if_permitted_to, :active, :link_options, :icon_style, :tooltip_placement def initialize( url:, icon:, label:, hotkey: nil, title: nil, dialog: true, dialog_options: {}, skip_permission_check: false, if_permitted_to: [], active: false, link_options: {}, icon_style: "line", tooltip_placement: "top-start" ) @url = url @icon = icon @label = label @hotkey = hotkey @dialog = dialog @dialog_options = dialog_options @skip_permission_check = skip_permission_check @if_permitted_to = if_permitted_to @active = active @link_options = link_options @icon_style = icon_style @tooltip_placement = tooltip_placement end def render? skip_permission_check || can?(*permission_options) end private def css_classes = ["icon_button", active && "active"].compact def permission_options = if_permitted_to.presence || permissions_from_url def permissions_from_url action_controller = url.gsub(/\A\//, "").split("/") [ action_controller.last.to_sym, action_controller[0..action_controller.length - 2].join("_").to_sym ] end end end end