Module: Alchemy::Admin::BaseHelper
- Includes:
- NavigationHelper, BaseHelper
- Included in:
- AttachmentsHelper, IngredientsHelper, LinkDialog::BaseTab, PagesHelper
- Defined in:
- app/helpers/alchemy/admin/base_helper.rb
Overview
This module contains helper methods for rendering dialogs and confirmation windows.
The most important helpers for module developers are:
Instance Method Summary collapse
- #alchemy_admin_js_translations(locale = ::I18n.locale) ⇒ Object
-
#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
deprecated
Deprecated.
Use the
CurrentAlchemyUserNamecomponent instead. -
#delete_button(url, options = {}, html_options = {}) ⇒ Object
A delete button with confirmation window.
-
#hint_with_tooltip(text, icon: "alert", icon_class: nil) ⇒ String
Renders a hint with tooltip.
-
#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.
-
#page_layout_missing_warning ⇒ Object
Renders a warning icon with a hint that explains the user that the page layout is missing.
-
#render_alchemy_title ⇒ Object
(internal) Renders translated Module Names for html title element.
-
#render_hint_for(element, icon_options = {}) ⇒ Object
Render a hint icon with tooltip for given object.
-
#sites_for_select ⇒ Object
Used for site selector in Alchemy cockpit.
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
#render_icon, #render_message, #warning
Instance Method Details
#alchemy_admin_js_translations(locale = ::I18n.locale) ⇒ Object
72 73 74 75 76 77 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 72 def alchemy_admin_js_translations(locale = ::I18n.locale) render partial: "alchemy/admin/translations/#{locale}", formats: [:js] rescue ActionView::MissingTemplate # Fallback to default translations render partial: "alchemy/admin/translations/en", formats: [:js] end |
#alchemy_body_class ⇒ Object
Appends the current controller and action to body as css class.
251 252 253 254 255 256 257 258 259 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 251 def alchemy_body_class [ controller_name, action_name, content_for(:main_menu_style), content_for(:alchemy_body_class), "elements-window-visible" ].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') %>
227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 227 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&.iso8601 input_field = text_field object.class.name.demodulize.underscore.to_sym, method.to_sym, {type: "text", class: type, value: value}.merge() content_tag("alchemy-datepicker", input_field, "input-type" => type) 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?') %>
139 140 141 142 143 144 145 146 147 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 139 def (value = "", url = "", = {}, = {}) = { message: Alchemy.t(:confirm_to_proceed), title: Alchemy.t(:please_confirm) }.merge() form_tag url, {method: .delete(:method), class: "button-with-confirm"} do value, .merge("data-turbo-confirm" => [:message]) end end |
#clipboard_select_tag_options(items) ⇒ Object
(internal) Returns options for the clipboard select tag
262 263 264 265 266 267 268 269 270 271 272 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 262 def (items) = 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 () end |
#current_alchemy_user_name ⇒ Object
Use the CurrentAlchemyUserName component instead.
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 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 21 def current_alchemy_user_name render Alchemy::Admin::CurrentUserName.new(user: current_alchemy_user) end |
#delete_button(url, options = {}, html_options = {}) ⇒ Object
A delete button with confirmation window.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 158 def (url, = {}, = {}) = { title: Alchemy.t("Delete"), message: Alchemy.t("Are you sure?"), icon: "delete-bin-2" }.merge() if [:title] tooltip = .delete(:title) end = ( render_icon([:icon]), url, , { method: "delete", class: "icon_button #{.delete(:class)}".strip }.merge() ) if tooltip content_tag("sl-tooltip", , content: tooltip) else end end |
#hint_with_tooltip(text, icon: "alert", icon_class: nil) ⇒ String
Renders a hint with tooltip
Example
<%= hint_with_tooltip('Page layout is missing', icon: 'info') %>
289 290 291 292 293 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 289 def hint_with_tooltip(text, icon: "alert", icon_class: nil) content_tag :"sl-tooltip", class: "like-hint-tooltip", content: text, placement: "bottom" do render_icon(icon, class: icon_class) 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') %>
110 111 112 113 114 115 116 117 118 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 110 def link_to_confirm_dialog(link_string = "", = "", url = "", = {}) link_to(link_string, url, .merge( data: { "turbo-method": :delete, "turbo-confirm": } )) 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'}) %>
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 51 def link_to_dialog(content, url, = {}, = {}) = {modal: true} = .merge() if [:title] tooltip = .delete(:title) end anchor = if url.nil? tag.a(content, class: "disabled #{[:class]}".strip, tabindex: "-1") else link_to(content, url, .merge( "data-dialog-options" => .to_json, :is => "alchemy-dialog-link" )) end if tooltip content_tag("sl-tooltip", anchor, content: tooltip) else anchor end end |
#link_url_regexp ⇒ Object
Returns the regular expression used for external url validation in link dialog.
275 276 277 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 275 def link_url_regexp Alchemy.config.format_matchers.link_url || /^(mailto:|\/|[a-z]+:\/\/)/ end |
#page_layout_missing_warning ⇒ Object
Renders a warning icon with a hint that explains the user that the page layout is missing
297 298 299 300 301 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 297 def page_layout_missing_warning hint_with_tooltip( Alchemy.t(:page_definition_missing) ) end |
#render_alchemy_title ⇒ Object
(internal) Renders translated Module Names for html title element.
183 184 185 186 187 188 189 190 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 183 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
241 242 243 244 245 246 247 248 |
# File 'app/helpers/alchemy/admin/base_helper.rb', line 241 def render_hint_for(element, = {}) return unless element.has_hint? content_tag "sl-tooltip", class: "like-hint-tooltip", placement: "bottom-start" do render_icon("question", ) + content_tag(:span, element.hint.html_safe, slot: "content") end end |