Module: Alchemy::Admin::BaseHelper

Includes:
NavigationHelper, BaseHelper
Included in:
AttachmentsHelper, IngredientsHelper, PagesHelper
Defined in:
app/helpers/alchemy/admin/base_helper.rb

Overview

This module contains helper methods for rendering dialogs, toolbar buttons and confirmation windows.

The most important helpers for module developers are:

Instance Method Summary collapse

Methods included from NavigationHelper

#alchemy_main_navigation_entry, #entry_active?, #main_navigation_css_classes, #navigate_module, #sorted_alchemy_modules, #url_for_module, #url_for_module_sub_navigation

Methods included from BaseHelper

#message_icon_class, #page_or_find, #render_flash_notice, #render_icon, #render_message, #shorten, #warning

Instance Method Details

#alchemy_body_classObject

Appends the current controller and action to body as css class.



359
360
361
362
363
364
365
366
# File 'app/helpers/alchemy/admin/base_helper.rb', line 359

def alchemy_body_class
  [
    controller_name,
    action_name,
    content_for(:main_menu_style),
    content_for(:alchemy_body_class)
  ].compact
end

#alchemy_datepicker(object, method, html_options = {}) ⇒ Object

Renders a textfield ready to display a datepicker

A Javascript observer converts this into a fancy Datepicker. If you pass ‘datetime’ as :type the datepicker will also have a Time select. If you pass ‘time’ as :type the datepicker will only have a Time select.

This helper always renders “text” as input type because: HTML5 supports input types like ‘date’ but Browsers are using the users OS settings to validate the input format. Since Alchemy is localized in the backend the date formats should be aligned with the users locale setting in the backend but not the OS settings.

Date Example

<%= alchemy_datepicker(@person, :birthday) %>

Datetime Example

<%= alchemy_datepicker(@page, :public_on, type: 'datetime') %>

Time Example

<%= alchemy_datepicker(@meeting, :starts_at, type: 'time') %>

Parameters:

  • object (ActiveModel::Base)

    An instance of a model

  • method (String or Symbol)

    The attribute method to be called for the date value

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

    a customizable set of options

Options Hash (html_options):

  • :data-datepicker-type (String) — default: type

    The value of the data attribute for the type

  • :class (String) — default: type

    CSS classes of the input field

  • :value (String) — default: value of method on object

    The value the input displays. If you pass a String its parsed with Time.parse



335
336
337
338
339
340
341
342
343
344
345
# File 'app/helpers/alchemy/admin/base_helper.rb', line 335

def alchemy_datepicker(object, method, html_options = {})
  type = html_options.delete(:type) || "date"
  date = html_options.delete(:value) || object.send(method.to_sym).presence
  date = Time.zone.parse(date) if date.is_a?(String)
  value = date&.iso8601

  input_field = text_field object.class.name.demodulize.underscore.to_sym,
    method.to_sym, {type: "text", class: type, value: value}.merge(html_options)

  ("alchemy-datepicker", input_field, "input-type" => type)
end

#button_with_confirm(value = "", url = "", options = {}, html_options = {}) ⇒ Object

Note:

The method option in the html_options hash gets passed to the form_tag helper!

Returns a form and a button that opens a modal confirm dialog.

After confirmation it proceeds to send the form’s action.

Example:

<%= button_with_confirm('pay', '/admin/orders/1/pay', message: 'Do you really want to mark this order as payed?') %>

Parameters:

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

    The content inside the <a> tag

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

    The url that gets opened after confirmation

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

    Options for the Alchemy confirm dialog (see also app/assets/javascripts/alchemy/alchemy.confirm_dialog.js.coffee)

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

    HTML options that get passed to the button_tag helper.



177
178
179
180
181
182
183
184
185
186
187
# File 'app/helpers/alchemy/admin/base_helper.rb', line 177

def button_with_confirm(value = "", url = "", options = {}, html_options = {})
  options = {
    message: Alchemy.t(:confirm_to_proceed),
    ok_label: Alchemy.t("Yes"),
    title: Alchemy.t(:please_confirm),
    cancel_label: Alchemy.t("No")
  }.merge(options)
  form_tag url, {method: html_options.delete(:method), class: "button-with-confirm"} do
    button_tag value, html_options.merge("data-alchemy-confirm" => options.to_json)
  end
end

#clipboard_select_tag_options(items) ⇒ Object

(internal) Returns options for the clipboard select tag



369
370
371
372
373
374
375
376
377
378
379
# File 'app/helpers/alchemy/admin/base_helper.rb', line 369

def clipboard_select_tag_options(items)
  options = items.map do |item|
    name = if item.respond_to?(:display_name_with_preview_text)
      item.display_name_with_preview_text
    else
      item.name
    end
    [name, item.id]
  end
  options_for_select(options)
end

#current_alchemy_user_nameObject

Returns a string showing the name of the currently logged in user.

In order to represent your own User‘s class instance, you should add a alchemy_display_name method to your User class



23
24
25
26
27
28
# File 'app/helpers/alchemy/admin/base_helper.rb', line 23

def current_alchemy_user_name
  name = current_alchemy_user.try(:alchemy_display_name)
  if name.present?
     :span, "#{Alchemy.t("Logged in as")} #{name}", class: "current-user-name"
  end
end

#delete_button(url, options = {}, html_options = {}) ⇒ Object

A delete button with confirmation window.

Parameters:

  • title (Hash)

    a customizable set of options

  • message (Hash)

    a customizable set of options

  • icon (Hash)

    a customizable set of options



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/helpers/alchemy/admin/base_helper.rb', line 198

def delete_button(url, options = {}, html_options = {})
  options = {
    title: Alchemy.t("Delete"),
    message: Alchemy.t("Are you sure?"),
    icon: "delete-bin-2"
  }.merge(options)

  if html_options[:title]
    tooltip = html_options.delete(:title)
  end
  button = button_with_confirm(
    render_icon(options[:icon]),
    url, options, {
      method: "delete",
      class: "icon_button #{html_options.delete(:class)}".strip
    }.merge(html_options)
  )
  if tooltip
    ("sl-tooltip", button, content: tooltip)
  else
    button
  end
end

#hint_with_tooltip(text, icon: "alert") ⇒ String

Renders a hint with tooltip

Example

<%= hint_with_tooltip('Page layout is missing', icon: 'info') %>

Parameters:

  • text (String)
    • The text displayed in the tooltip

  • icon: ('alert'String) (defaults to: "alert")
    • Icon name

Returns:

  • (String)


396
397
398
399
400
# File 'app/helpers/alchemy/admin/base_helper.rb', line 396

def hint_with_tooltip(text, icon: "alert")
   :"sl-tooltip", class: "like-hint-tooltip", content: text, placement: "bottom" do
    render_icon(icon)
  end
end

#js_filter_field(items, options = {}) ⇒ Object

Returns a javascript driven live filter for lists.

The items must have a html name attribute that holds the filterable value.

Example

Given a list of items:

<%= js_filter_field('#products .product') %>

<ul id="products">
  <li class="product" name="kat litter">Kat Litter</li>
  <li class="product" name="milk">Milk</li>
</ul>

Parameters:

  • items (String)

    A jquery compatible selector string that represents the items to filter

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

    HTML options passed to the input field

Options Hash (options):

  • :class (String) — default: 'js_filter_field'

    The css class of the <input> tag

  • :data (String or Hash) — default: {'alchemy-list-filter' => items}

    A HTML data attribute that holds the jQuery selector that represents the list to be filtered



110
111
112
113
114
115
116
117
118
119
120
# File 'app/helpers/alchemy/admin/base_helper.rb', line 110

def js_filter_field(items, options = {})
  options = {
    class: "js_filter_field",
    data: {"alchemy-list-filter" => items}
  }.merge(options)
  (:div, class: "js_filter_field_box") do
    concat text_field_tag(nil, nil, options)
    concat render_icon(:search)
    concat link_to(render_icon(:times, size: "xs"), "", class: "js_filter_field_clear", title: Alchemy.t(:click_to_show_all))
  end
end

Returns a link that opens a modal confirmation to delete window.

Example:

<%= link_to_confirm_dialog('delete', 'Do you really want to delete this comment?', '/admin/comments/1') %>

Parameters:

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

    The content inside the <a> tag

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

    The message that is displayed in the dialog

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

    The url that gets opened after confirmation (Note: This is an Ajax request with a method of DELETE!)

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

    HTML options get passed to the link

Options Hash (html_options):

  • :title (String) — default: Alchemy.t(:please_confirm)

    The dialog title

  • :message (String) — default: message

    The message displayed in the dialog

  • :ok_label (String) — default: Alchemy.t("Yes")

    The label for the ok button

  • :cancel_label (String) — default: Alchemy.t("No")

    The label for the cancel button



146
147
148
149
150
151
152
153
154
155
156
# File 'app/helpers/alchemy/admin/base_helper.rb', line 146

def link_to_confirm_dialog(link_string = "", message = "", url = "", html_options = {})
  link_to(link_string, url,
    html_options.merge(
      "data-alchemy-confirm-delete" => {
        title: Alchemy.t(:please_confirm),
        message: message,
        ok_label: Alchemy.t("Yes"),
        cancel_label: Alchemy.t("No")
      }.to_json
    ))
end

This helper renders the link to an dialog.

We use this for our fancy modal dialogs in the Alchemy cockpit.

Example

<%= link_to_dialog('Edit', edit_product_path, {size: '200x300'}, {class: 'icon_button'}) %>

Parameters:

  • content (String)

    The string inside the link tag

  • url (String or Hash)

    The url of the action displayed inside the dialog.

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

    options for the dialog.

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

    HTML options passed to the link_to helper

Options Hash (options):

  • :size (String)

    String with format of “WidthxHeight”. I.E. (“420x280”)

  • :title (String)

    Text for the dialog title bar.

  • :modal (Boolean) — default: true

    Show as modal window.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/alchemy/admin/base_helper.rb', line 54

def link_to_dialog(content, url, options = {}, html_options = {})
  default_options = {modal: true}
  options = default_options.merge(options)
  if html_options[:title]
    tooltip = html_options.delete(:title)
  end
  anchor = link_to(content, url, html_options.merge(
    "data-dialog-options" => options.to_json,
    :is => "alchemy-dialog-link"
  ))
  if tooltip
    ("sl-tooltip", anchor, content: tooltip)
  else
    anchor
  end
end

Returns the regular expression used for external url validation in link dialog.



382
383
384
# File 'app/helpers/alchemy/admin/base_helper.rb', line 382

def link_url_regexp
  Alchemy::Config.get(:format_matchers)["link_url"] || /^(mailto:|\/|[a-z]+:\/\/)/
end

#new_asset_path_with_session_information(asset_type) ⇒ Object

(internal) Used by upload form



291
292
293
294
295
296
297
298
# File 'app/helpers/alchemy/admin/base_helper.rb', line 291

def new_asset_path_with_session_information(asset_type)
  session_key = Rails.application.config.session_options[:key]
  if asset_type == "picture"
    alchemy.admin_pictures_path(session_key => cookies[session_key], request_forgery_protection_token => form_authenticity_token, :format => :js)
  elsif asset_type == "attachment"
    alchemy.admin_attachments_path(session_key => cookies[session_key], request_forgery_protection_token => form_authenticity_token, :format => :js)
  end
end

#page_layout_missing_warningObject

Renders a warning icon with a hint that explains the user that the page layout is missing



404
405
406
407
408
# File 'app/helpers/alchemy/admin/base_helper.rb', line 404

def page_layout_missing_warning
  hint_with_tooltip(
    Alchemy.t(:page_definition_missing)
  )
end

#render_alchemy_titleObject

(internal) Renders translated Module Names for html title element.



223
224
225
226
227
228
229
230
# File 'app/helpers/alchemy/admin/base_helper.rb', line 223

def render_alchemy_title
  title = if content_for?(:title)
    content_for(:title)
  else
    Alchemy.t(controller_name, scope: :modules)
  end
  "Alchemy CMS - #{title}"
end

#render_hint_for(element, icon_options = {}) ⇒ Object

Render a hint icon with tooltip for given object. The model class needs to include the hints module



349
350
351
352
353
354
355
356
# File 'app/helpers/alchemy/admin/base_helper.rb', line 349

def render_hint_for(element, icon_options = {})
  return unless element.has_hint?

   "sl-tooltip", class: "like-hint-tooltip", placement: "bottom-start" do
    render_icon("question", icon_options) +
      (:span, element.hint.html_safe, slot: "content")
  end
end

#sites_for_selectObject

Used for site selector in Alchemy cockpit.



79
80
81
82
83
# File 'app/helpers/alchemy/admin/base_helper.rb', line 79

def sites_for_select
  Alchemy::Site.all.map do |site|
    [site.name, site.id]
  end
end

#toolbar_button(options = {}) ⇒ Object

Renders a toolbar button for the Alchemy toolbar

Example

<%= toolbar_button(
  icon: :plus,
  label: 'Create',
  url: new_resource_path,
  title: 'Create Resource',
  hotkey: 'alt+n',
  dialog_options: {
    title: 'Create Resource',
    size: "430x400"
  },
  if_permitted_to: [:create, resource_model]
) %>

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :icon (String)

    Icon class. See app/assets/stylesheets/alchemy/icons.css.sccs for available icons, or make your own.

  • :label (String)

    Text for button label.

  • :url (String)

    Url for link.

  • :title (String)

    Text for title tag.

  • :hotkey (String)

    Keyboard shortcut for this button. I.E alt-n

  • :dialog (Boolean) — default: true

    Open the link in a modal dialog.

  • :dialog_options (Hash)

    Overlay options. See link_to_dialog helper.

  • :if_permitted_to (Array) — default: [: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.

  • :skip_permission_check (Boolean) — default: false

    Skip the permission check. NOT RECOMMENDED!

  • :loading_indicator (Boolean) — default: true

    Shows the please wait dialog while loading. Only for buttons not opening an dialog.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'app/helpers/alchemy/admin/base_helper.rb', line 270

def toolbar_button(options = {})
  options = {
    dialog: true,
    skip_permission_check: false,
    active: false,
    link_options: {},
    dialog_options: {},
    loading_indicator: false
  }.merge(options.symbolize_keys)
  button = render(
    "alchemy/admin/partials/toolbar_button",
    options: options
  )
  if options[:skip_permission_check] || can?(*permission_from_options(options))
    button
  else
    ""
  end
end

#translations_for_selectObject

Used for translations selector in Alchemy cockpit user settings.



72
73
74
75
76
# File 'app/helpers/alchemy/admin/base_helper.rb', line 72

def translations_for_select
  Alchemy::I18n.available_locales.sort.map do |locale|
    [Alchemy.t(locale, scope: :translations), locale]
  end
end