Module: Alchemy::Admin::BaseHelper

Includes:
NavigationHelper, BaseHelper
Included in:
AttachmentsHelper, ContentsHelper, ElementsHelper
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

#admin_subnavigation, #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

#_t, #page_or_find, #parse_sitemap_name, #render_flash_notice, #render_icon, #render_message, #shorten, #warning

Instance Method Details

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

Renders a textfield ready to display a datepicker

Uses a HTML5 <input type="date"> field.

Example

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

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):

  • :type (String) — default: 'date'

    The type of text field

  • :class (String) — default: 'thin_border date'

    CSS classes of the input field

  • :value (String) — default: object.send(method.to_sym).nil? ? nil : l(object.send(method.to_sym), :format => :datepicker)

    The value the input displays



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'app/helpers/alchemy/admin/base_helper.rb', line 378

def alchemy_datepicker(object, method, html_options={})
  value = nil
  if object.send(method.to_sym).present?
    value = l(object.send(method.to_sym), format: :datepicker)
  elsif html_options[:value].present?
    date = html_options.delete(:value)
    date = Time.parse(date) if date.is_a?(String)
    value = l(date, format: :datepicker)
  end
  text_field object.class.name.underscore.to_sym, method.to_sym, html_options.merge({
    type: 'date',
    class: 'date',
    value: value
  })
end

#body_classObject

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



430
431
432
# File 'app/helpers/alchemy/admin/base_helper.rb', line 430

def body_class
  "#{controller_name} #{action_name}"
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.



168
169
170
171
172
173
174
175
176
177
178
# File 'app/helpers/alchemy/admin/base_helper.rb', line 168

def button_with_confirm(value = "", url = "", options = {}, html_options = {})
  options = {
    message: _t(:confirm_to_proceed),
    ok_label: _t("Yes"),
    title: _t(:please_confirm),
    cancel_label: _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



435
436
437
438
439
440
441
442
443
444
# File 'app/helpers/alchemy/admin/base_helper.rb', line 435

def clipboard_select_tag_options(items)
  if @page.persisted? && @page.can_have_cells?
    grouped_options_for_select(grouped_elements_for_select(items, :id))
  else
    options = items.map do |item|
      [item.respond_to?(:display_name_with_preview_text) ? item.display_name_with_preview_text : item.name, item.id]
    end
    options_for_select(options)
  end
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



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

def current_alchemy_user_name
  name = current_alchemy_user.try(:alchemy_display_name)
  if name.present?
     :span, "#{_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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/helpers/alchemy/admin/base_helper.rb', line 189

def delete_button(url, options = {}, html_options = {})
  options = {
    title: _t('Delete'),
    message: _t('Are you sure?'),
    icon: 'destroy'
  }.merge(options)
  button_with_confirm(
    render_icon(options[:icon]),
    url, {
      message: options[:message]
    }, {
      method: 'delete',
      title: options[:title],
      class: "icon_only #{html_options.delete(:class)}".strip
    }.merge(html_options)
  )
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



99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/helpers/alchemy/admin/base_helper.rb', line 99

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('', '', class: 'js_filter_field_clear', title: _t(:click_to_show_all))
    concat (:label, _t(:search), for: options[:id])
  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: _t(:please_confirm)

    The dialog title

  • :message (String) — default: message

    The message displayed in the dialog

  • :ok_label (String) — default: _t("Yes")

    The label for the ok button

  • :cancel_label (String) — default: _t("No")

    The label for the cancel button



136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/helpers/alchemy/admin/base_helper.rb', line 136

def link_to_confirm_dialog(link_string = "", message = "", url = "", html_options = {})
  link_to(link_string, url,
    html_options.merge(
      'data-alchemy-confirm-delete' => {
        title: _t(:please_confirm),
        message: message,
        ok_label: _t("Yes"),
        cancel_label: _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.



53
54
55
56
57
58
# File 'app/helpers/alchemy/admin/base_helper.rb', line 53

def link_to_dialog(content, url, options={}, html_options={})
  default_options = {modal: true}
  options = default_options.merge(options)
  link_to content, url,
    html_options.merge('data-alchemy-dialog' => options.to_json)
end

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



447
448
449
# File 'app/helpers/alchemy/admin/base_helper.rb', line 447

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

#max_image_countObject

(internal) Returns max image count as integer or nil. Used for the picture editor in element editor views.



218
219
220
221
222
# File 'app/helpers/alchemy/admin/base_helper.rb', line 218

def max_image_count
  return nil if !@options
  image_count = @options[:maximum_amount_of_images] || @options[:max_images]
  image_count.blank? ? nil : image_count.to_i
end

#merge_params(p = {}) ⇒ Object

Merges the params-hash with the given hash



395
396
397
# File 'app/helpers/alchemy/admin/base_helper.rb', line 395

def merge_params(p={})
  params.merge(p).delete_if { |k, v| v.blank? }
end

#merge_params_only(includes, p = {}) ⇒ Object

Deletes all params from the params-hash except the given ones and merges some new params in



411
412
413
414
415
416
417
418
419
420
# File 'app/helpers/alchemy/admin/base_helper.rb', line 411

def merge_params_only(includes, p={})
  current_params = params.clone.symbolize_keys
  if includes.is_a?(Array)
    symbolized_includes = includes.map(&:to_sym)
    current_params.delete_if { |k, v| !symbolized_includes.include?(k) }
  else
    current_params.delete_if { |k, v| k != includes.to_sym }
  end
  current_params.merge(p).delete_if { |k, v| v.blank? }
end

#merge_params_without(excludes, p = {}) ⇒ Object

Deletes one or several params from the params-hash and merges some new params in



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

def merge_params_without(excludes, p={})
  current_params = params.clone.symbolize_keys
  if excludes.is_a?(Array)
    excludes.map { |i| current_params.delete(i.to_sym) }
  else
    current_params.delete(excludes.to_sym)
  end
  current_params.merge(p).delete_if { |k, v| v.blank? }
end

#new_asset_path_with_session_information(asset_type) ⇒ Object

(internal) Used by upload form



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

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

#render_alchemy_titleObject

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



208
209
210
211
212
213
214
215
# File 'app/helpers/alchemy/admin/base_helper.rb', line 208

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

#render_hint_for(element) ⇒ Object



422
423
424
425
426
427
# File 'app/helpers/alchemy/admin/base_helper.rb', line 422

def render_hint_for(element)
  return unless element.has_hint?
  link_to '#', :class => 'hint' do
    render_icon(:hint) + (:span, element.hint.html_safe, :class => 'bubble')
  end
end

#render_resourcesObject

Renders the row for a resource record in the resources table.

This helper has a nice fallback. If you create a partial for your record then this partial will be rendered.

Otherwise the default app/views/alchemy/admin/resources/_resource.html.erb partial gets rendered.

Example

For a resource named Comment you can create a partial named _comment.html.erb

# app/views/admin/comments/_comment.html.erb
<tr>
  <td><%= comment.title %></td>
  <td><%= comment.body %></td>
</tr>

NOTE: Alchemy gives you a local variable named like your resource



342
343
344
345
346
# File 'app/helpers/alchemy/admin/base_helper.rb', line 342

def render_resources
  render :partial => resource_name, :collection => resources_instance_variable
rescue ActionView::MissingTemplate
  render :partial => 'resource', :collection => resources_instance_variable
end

#sites_for_selectObject

Used for site selector in Alchemy cockpit.



68
69
70
71
72
# File 'app/helpers/alchemy/admin/base_helper.rb', line 68

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

#toolbar(options = {}) ⇒ Object

Renders the toolbar shown on top of the records.

Example

<% label_title = _t("Create #{resource_name}", default: _t('Create')) %>
<% toolbar(
  buttons: [
    {
      icon: 'create',
      label: label_title,
      url: new_resource_path,
      title: label_title,
      hotkey: 'alt+n',
      dialog_options: {
        title: label_title,
        size: "430x400"
      },
      if_permitted_to: [:create, resource_model]
    }
  ]
) %>

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :buttons (Array) — default: []

    Pass an Array with button options. They will be passed to #toolbar_button helper.

  • :search (Boolean) — default: true

    Show searchfield.



309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'app/helpers/alchemy/admin/base_helper.rb', line 309

def toolbar(options = {})
  defaults = {
    :buttons => [],
    :search => true
  }
  options = defaults.merge(options)
  content_for(:toolbar) do
    content = <<-CONTENT
#{options[:buttons].map { |button_options| toolbar_button(button_options) }.join()}
    #{render('alchemy/admin/partials/search_form', :url => options[:search_url]) if options[:search]}
    CONTENT
    content.html_safe
  end
end

#toolbar_button(options = {}) ⇒ Object

Renders a toolbar button for the Alchemy toolbar

Example

<%= toolbar_button(
  icon: 'create',
  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.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'app/helpers/alchemy/admin/base_helper.rb', line 262

def toolbar_button(options = {})
  options = {
    dialog: true,
    skip_permission_check: false,
    active: false,
    link_options: {},
    dialog_options: {},
    loading_indicator: true
  }.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.



61
62
63
64
65
# File 'app/helpers/alchemy/admin/base_helper.rb', line 61

def translations_for_select
  Alchemy::I18n.available_locales.map do |locale|
    [_t(locale, :scope => :translations), locale]
  end
end