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
-
#alchemy_body_class ⇒ Object
Appends the current controller and action to body as css class.
-
#alchemy_datepicker(object, method, html_options = {}) ⇒ Object
Renders a textfield ready to display a datepicker.
-
#button_with_confirm(value = "", url = "", options = {}, html_options = {}) ⇒ Object
Returns a form and a button that opens a modal confirm dialog.
-
#clipboard_select_tag_options(items) ⇒ Object
(internal) Returns options for the clipboard select tag.
-
#current_alchemy_user_name ⇒ Object
Returns a string showing the name of the currently logged in user.
-
#delete_button(url, options = {}, html_options = {}) ⇒ Object
A delete button with confirmation window.
-
#js_filter_field(items, options = {}) ⇒ Object
Returns a javascript driven live filter for lists.
-
#link_to_confirm_dialog(link_string = "", message = "", url = "", html_options = {}) ⇒ Object
Returns a link that opens a modal confirmation to delete window.
-
#link_to_dialog(content, url, options = {}, html_options = {}) ⇒ Object
This helper renders the link to an dialog.
-
#link_url_regexp ⇒ Object
Returns the regular expression used for external url validation in link dialog.
-
#max_image_count ⇒ Object
(internal) Returns max image count as integer or nil.
-
#merge_params(p = {}) ⇒ Object
Merges the params-hash with the given hash.
-
#merge_params_only(includes, p = {}) ⇒ Object
Deletes all params from the params-hash except the given ones and merges some new params in.
-
#merge_params_without(excludes, p = {}) ⇒ Object
Deletes one or several params from the params-hash and merges some new params in.
-
#new_asset_path_with_session_information(asset_type) ⇒ Object
(internal) Used by upload form.
-
#render_alchemy_title ⇒ Object
(internal) Renders translated Module Names for html title element.
- #render_hint_for(element) ⇒ Object
-
#sites_for_select ⇒ Object
Used for site selector in Alchemy cockpit.
-
#toolbar(options = {}) ⇒ Object
Renders the toolbar shown on top of the records.
-
#toolbar_button(options = {}) ⇒ Object
Renders a toolbar button for the Alchemy toolbar.
-
#translations_for_select ⇒ Object
Used for translations selector in Alchemy cockpit user settings.
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, #render_flash_notice, #render_icon, #render_message, #shorten, #warning
Instance Method Details
#alchemy_body_class ⇒ Object
Appends the current controller and action to body as css class.
408 409 410 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 408 def alchemy_body_class "#{controller_name} #{action_name}" end |
#alchemy_datepicker(object, method, html_options = {}) ⇒ Object
Renders a textfield ready to display a datepicker
Uses a HTML5 <input type=“date”> field. A Javascript observer converts this into a fancy Datepicker. If you pass ‘datetime’ as :type
the datepicker will also have a Time select.
The date value gets localized via I18n.l
. The format on Time and Date is datepicker
or datetimepicker
, if you pass another type
.
Date Example
<%= alchemy_datepicker(@person, :birthday) %>
Datetime Example
<%= alchemy_datepicker(@page, :public_on, type: 'datetime') %>
362 363 364 365 366 367 368 369 370 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 362 def alchemy_datepicker(object, method, = {}) type = .delete(:type) || 'date' date = .delete(:value) || object.send(method.to_sym).presence date = Time.zone.parse(date) if date.is_a?(String) value = date ? l(date, format: "#{type}picker".to_sym) : nil text_field object.class.name.demodulize.underscore.to_sym, method.to_sym, {type: type, class: type, value: value}.merge() end |
#button_with_confirm(value = "", url = "", options = {}, html_options = {}) ⇒ Object
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?') %>
167 168 169 170 171 172 173 174 175 176 177 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 167 def (value = "", url = "", = {}, = {}) = { message: Alchemy.t(:confirm_to_proceed), ok_label: Alchemy.t("Yes"), title: Alchemy.t(:please_confirm), cancel_label: Alchemy.t("No") }.merge() form_tag url, {method: .delete(:method), class: 'button-with-confirm'} do value, .merge('data-alchemy-confirm' => .to_json) end end |
#clipboard_select_tag_options(items) ⇒ Object
(internal) Returns options for the clipboard select tag
413 414 415 416 417 418 419 420 421 422 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 413 def (items) if @page.persisted? && @page.can_have_cells? (grouped_elements_for_select(items, :id)) else = items.map do |item| [item.respond_to?(:display_name_with_preview_text) ? item.display_name_with_preview_text : item.name, item.id] end () end end |
#current_alchemy_user_name ⇒ Object
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
21 22 23 24 25 26 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 21 def current_alchemy_user_name name = current_alchemy_user.try(:alchemy_display_name) if name.present? content_tag :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.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 188 def (url, = {}, = {}) = { title: Alchemy.t('Delete'), message: Alchemy.t('Are you sure?'), icon: 'destroy' }.merge() ( render_icon([:icon]), url, { message: [:message] }, { method: 'delete', title: [:title], class: "icon_only #{.delete(:class)}".strip }.merge() ) 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>
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 98 def js_filter_field(items, = {}) = { class: 'js_filter_field', data: {'alchemy-list-filter' => items} }.merge() content_tag(:div, class: 'js_filter_field_box') do concat text_field_tag(nil, nil, ) concat render_icon(:search) concat link_to('', '', class: 'js_filter_field_clear', title: Alchemy.t(:click_to_show_all)) concat content_tag(:label, Alchemy.t(:search), for: [:id]) end end |
#link_to_confirm_dialog(link_string = "", message = "", url = "", html_options = {}) ⇒ Object
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') %>
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 135 def link_to_confirm_dialog(link_string = "", = "", url = "", = {}) link_to(link_string, url, .merge( 'data-alchemy-confirm-delete' => { title: Alchemy.t(:please_confirm), message: , ok_label: Alchemy.t("Yes"), cancel_label: Alchemy.t("No") }.to_json ) ) end |
#link_to_dialog(content, url, options = {}, html_options = {}) ⇒ Object
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'}) %>
52 53 54 55 56 57 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 52 def link_to_dialog(content, url, = {}, = {}) = {modal: true} = .merge() link_to content, url, .merge('data-alchemy-dialog' => .to_json) end |
#link_url_regexp ⇒ Object
Returns the regular expression used for external url validation in link dialog.
425 426 427 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 425 def link_url_regexp Alchemy::Config.get(:format_matchers)['link_url'] || /^(mailto:|\/|[a-z]+:\/\/)/ end |
#max_image_count ⇒ Object
(internal) Returns max image count as integer or nil. Used for the picture editor in element editor views.
217 218 219 220 221 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 217 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
373 374 375 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 373 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
389 390 391 392 393 394 395 396 397 398 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 389 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
378 379 380 381 382 383 384 385 386 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 378 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
324 325 326 327 328 329 330 331 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 324 def new_asset_path_with_session_information(asset_type) session_key = Rails.application.config.[:key] if asset_type == "picture" alchemy.admin_pictures_path(session_key => [session_key], request_forgery_protection_token => form_authenticity_token, :format => :js) elsif asset_type == "attachment" alchemy.(session_key => [session_key], request_forgery_protection_token => form_authenticity_token, :format => :js) end end |
#render_alchemy_title ⇒ Object
(internal) Renders translated Module Names for html title element.
207 208 209 210 211 212 213 214 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 207 def render_alchemy_title if content_for?(:title) title = content_for(:title) else title = Alchemy.t(controller_name, scope: :modules) end "Alchemy CMS - #{title}" end |
#render_hint_for(element) ⇒ Object
400 401 402 403 404 405 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 400 def render_hint_for(element) return unless element.has_hint? link_to '#', class: 'hint' do render_icon(:hint) + content_tag(:span, element.hint.html_safe, class: 'bubble') end end |
#sites_for_select ⇒ Object
Used for site selector in Alchemy cockpit.
67 68 69 70 71 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 67 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 = Alchemy.t("Create #{resource_name}", default: Alchemy.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]
}
]
) %>
308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 308 def ( = {}) defaults = { buttons: [], search: true } = defaults.merge() content_for(:toolbar) do content = <<-CONTENT #{[:].map { || () }.join} #{render('alchemy/admin/partials/search_form', url: [:search_url]) if [: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]
) %>
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 261 def ( = {}) = { dialog: true, skip_permission_check: false, active: false, link_options: {}, dialog_options: {}, loading_indicator: true }.merge(.symbolize_keys) = render( 'alchemy/admin/partials/toolbar_button', options: ) if [:skip_permission_check] || can?(*()) else "" end end |
#translations_for_select ⇒ Object
Used for translations selector in Alchemy cockpit user settings.
60 61 62 63 64 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 60 def translations_for_select Alchemy::I18n.available_locales.map do |locale| [Alchemy.t(locale, scope: :translations), locale] end end |