Module: RailsAdminNext::ApplicationHelper
- Included in:
- FormBuilder, MainController
- Defined in:
- app/helpers/rails_admin_next/application_helper.rb
Instance Method Summary collapse
- #action(key, abstract_model = nil, object = nil) ⇒ Object
- #actions(scope = :all, abstract_model = nil, object = nil) ⇒ Object
- #authorized?(action_name, abstract_model = nil, object = nil) ⇒ Boolean
- #breadcrumb(action = @action, _acc = []) ⇒ Object
- #bulk_menu(abstract_model = @abstract_model) ⇒ Object
- #current_action ⇒ Object
- #current_action?(action, abstract_model = @abstract_model, object = @object) ⇒ Boolean
- #edit_user_link ⇒ Object
- #flash_alert_class(flash_key) ⇒ Object
-
#image_tag(source, options = {}) ⇒ Object
Workaround for https://github.com/rails/rails/issues/31325.
- #logout_method ⇒ Object
- #logout_path ⇒ Object
- #main_navigation ⇒ Object
-
#menu_for(parent, abstract_model = nil, object = nil, only_icon = false) ⇒ Object
parent => :root, :collection, :member perf matters here (no action view trickery).
- #navigation(parent_groups, nodes, level = 0) ⇒ Object
-
#rails_admin_icon(name, **html_options) ⇒ Object
Render an engine icon as inline SVG.
- #root_navigation ⇒ Object
- #static_navigation ⇒ Object
- #wording_for(label, action = @action, abstract_model = @abstract_model, object = @object) ⇒ Object
Instance Method Details
#action(key, abstract_model = nil, object = nil) ⇒ Object
20 21 22 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 20 def action(key, abstract_model = nil, object = nil) RailsAdminNext::Config::Actions.find(key, controller: controller, abstract_model: abstract_model, object: object) end |
#actions(scope = :all, abstract_model = nil, object = nil) ⇒ Object
32 33 34 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 32 def actions(scope = :all, abstract_model = nil, object = nil) RailsAdminNext::Config::Actions.all(scope, controller: controller, abstract_model: abstract_model, object: object) end |
#authorized?(action_name, abstract_model = nil, object = nil) ⇒ Boolean
5 6 7 8 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 5 def (action_name, abstract_model = nil, object = nil) object = nil if object.try :new_record? action(action_name, abstract_model, object).try(:authorized?) end |
#breadcrumb(action = @action, _acc = []) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 135 def (action = @action, _acc = []) begin (parent_actions ||= []) << action end while action. && (action = action(*action.)) # rubocop:disable Lint/Loop content_tag(:ol, class: "breadcrumb") do parent_actions.collect do |a| am = a.send(:eval, "bindings[:abstract_model]") o = a.send(:eval, "bindings[:object]") content_tag(:li, class: ["breadcrumb-item", current_action?(a, am, o) && "active"]) do if current_action?(a, am, o) wording_for(:breadcrumb, a, am, o) elsif a.http_methods.include?(:get) link_to rails_admin_next.url_for(action: a.action_name, controller: "rails_admin_next/main", model_name: am.try(:to_param), id: o.try(:persisted?) && o.try(:id) || nil) do wording_for(:breadcrumb, a, am, o) end else content_tag(:span, wording_for(:breadcrumb, a, am, o)) end end end.reverse.join.html_safe end end |
#bulk_menu(abstract_model = @abstract_model) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 184 def (abstract_model = @abstract_model) actions = actions(:bulkable, abstract_model) return "" if actions.empty? content_tag :li, class: "nav-item dropdown" do content_tag(:button, type: "button", id: "bulk_menu_toggle", class: "nav-link dropdown-toggle", popovertarget: "bulk_menu", data: {controller: "dropdown", action: "keydown->dropdown#onKeydown"}) { t("admin.misc.bulk_menu_title").html_safe + " " + '<b class="caret"></b>'.html_safe } + content_tag(:ul, id: "bulk_menu", class: "dropdown-menu dropdown-menu-end", role: "menu", popover: "auto") do actions.collect do |action| content_tag :li, role: "none" do link_to wording_for(:bulk_link, action, abstract_model), "#", class: "dropdown-item bulk-link", role: "menuitem", tabindex: "-1", data: {action: action.action_name} end end.join.html_safe end end.html_safe end |
#current_action ⇒ Object
10 11 12 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 10 def current_action params[:action].in?(%w[create new]) ? "create" : "update" end |
#current_action?(action, abstract_model = @abstract_model, object = @object) ⇒ Boolean
14 15 16 17 18 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 14 def current_action?(action, abstract_model = @abstract_model, object = @object) @action.custom_key == action.custom_key && abstract_model.try(:to_param) == @abstract_model.try(:to_param) && (@object.try(:persisted?) ? @object.id == object.try(:id) : !object.try(:persisted?)) end |
#edit_user_link ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 36 def edit_user_link return nil unless _current_user.try(:email).present? return nil unless (abstract_model = RailsAdminNext.config(_current_user.class).abstract_model) edit_action = action(:edit, abstract_model, _current_user) = edit_action.try(:authorized?) content = edit_user_link_label if edit_url = rails_admin_next.url_for( action: edit_action.action_name, model_name: abstract_model.to_param, controller: "rails_admin_next/main", id: _current_user.id ) link_to content, edit_url, class: "nav-link" else content_tag :span, content, class: "nav-link" end end |
#flash_alert_class(flash_key) ⇒ Object
200 201 202 203 204 205 206 207 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 200 def flash_alert_class(flash_key) case flash_key.to_s when "error" then "alert-danger" when "alert" then "alert-warning" when "notice" then "alert-info" else "alert-#{flash_key}" end end |
#image_tag(source, options = {}) ⇒ Object
Workaround for https://github.com/rails/rails/issues/31325
210 211 212 213 214 215 216 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 210 def image_tag(source, = {}) if %w[ActiveStorage::Variant ActiveStorage::VariantWithRecord ActiveStorage::Preview].include? source.class.to_s super(main_app.route_for(ActiveStorage.resolve_model_to_route, source), ) else super end end |
#logout_method ⇒ Object
62 63 64 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 62 def logout_method :delete end |
#logout_path ⇒ Object
58 59 60 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 58 def logout_path main_app.logout_path if main_app.respond_to?(:logout_path) end |
#main_navigation ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 79 def nodes_stack = RailsAdminNext::Config.visible_models(controller: controller) node_model_names = nodes_stack.collect { |c| c.abstract_model.model_name } parent_groups = nodes_stack.group_by { |n| n.parent&.to_s } nodes_stack.group_by(&:navigation_label).collect do |, nodes| nodes = nodes.select { |n| n.parent.nil? || !n.parent.to_s.in?(node_model_names) } li_stack = parent_groups, nodes label = || t("admin.misc.navigation") collapsible_stack(label, "main", li_stack) end.join.html_safe end |
#menu_for(parent, abstract_model = nil, object = nil, only_icon = false) ⇒ Object
parent => :root, :collection, :member perf matters here (no action view trickery)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 161 def (parent, abstract_model = nil, object = nil, only_icon = false) actions = actions(parent, abstract_model, object).select { |a| a.http_methods.include?(:get) && a. } actions.collect do |action| wording = wording_for(:menu, action) li_class = ["nav-item", "icon", "#{action.key}_#{parent}_link"] .concat(action.enabled? ? [] : ["disabled"]) content_tag(:li, {class: li_class}.merge(only_icon ? {title: wording, rel: "tooltip"} : {})) do label = rails_admin_icon(action.link_icon) + " " + content_tag(:span, wording, (only_icon ? {style: "display:none"} : {})) if action.enabled? || !only_icon href = if action.enabled? rails_admin_next.url_for(action: action.action_name, controller: "rails_admin_next/main", model_name: abstract_model.try(:to_param), id: object.try(:persisted?) && object.try(:id) || nil) else "javascript:void(0)" end content_tag(:a, label, {href: href, target: action.link_target, class: ["nav-link", current_action?(action) && "active", !action.enabled? && "disabled"].compact}.merge(action.turbo? ? {} : {data: {turbo: "false"}})) else content_tag(:span, label) end end end.join(" ").html_safe end |
#navigation(parent_groups, nodes, level = 0) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 118 def (parent_groups, nodes, level = 0) nodes.collect do |node| abstract_model = node.abstract_model model_param = abstract_model.to_param url = rails_admin_next.url_for(action: :index, controller: "rails_admin_next/main", model_name: model_param) nav_icon = rails_admin_icon(node.) css_classes = ["nav-link"] css_classes.push("nav-level-#{level}") if level > 0 css_classes.push("active") if @action && current_action?(@action, model_param) li = content_tag :li, data: {model: model_param} do link_to nav_icon + " " + node.label_plural, url, class: css_classes.join(" ") end child_nodes = parent_groups[abstract_model.model_name] child_nodes ? li + (parent_groups, child_nodes, level + 1) : li end.join.html_safe end |
#rails_admin_icon(name, **html_options) ⇒ Object
Render an engine icon as inline SVG. name is a logical icon name (see
RailsAdminNext::Icons::GLYPHS), e.g. :edit, :delete, :list. A nil/unknown
name renders nothing rather than raising; a legacy Font Awesome class string
is resolved best-effort and warns via RailsAdminNext.deprecator.
28 29 30 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 28 def rails_admin_icon(name, **) RailsAdminNext::Icons.svg(name, **)&.html_safe || "".html_safe end |
#root_navigation ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 94 def actions(:root).select(&:show_in_sidebar).group_by(&:sidebar_label).collect do |label, nodes| li_stack = nodes.map do |node| url = rails_admin_next.url_for(action: node.action_name, controller: "rails_admin_next/main") nav_icon = rails_admin_icon(node.link_icon) content_tag :li do link_to nav_icon + " " + wording_for(:menu, node), url, class: "nav-link" end end.join.html_safe label ||= t("admin.misc.root_navigation") collapsible_stack(label, "action", li_stack) end.join.html_safe end |
#static_navigation ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 109 def li_stack = RailsAdminNext::Config..collect do |title, url| content_tag(:li, link_to(title.to_s, url, target: "_blank", rel: "noopener noreferrer", class: "nav-link")) end.join.html_safe label = RailsAdminNext::Config. || t("admin.misc.navigation_static_label") collapsible_stack(label, "static", li_stack) || "" end |
#wording_for(label, action = @action, abstract_model = @abstract_model, object = @object) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/helpers/rails_admin_next/application_helper.rb', line 66 def wording_for(label, action = @action, abstract_model = @abstract_model, object = @object) model_config = abstract_model.try(:config) object = nil unless abstract_model && object.is_a?(abstract_model.model) action = RailsAdminNext::Config::Actions.find(action.to_sym) if action.is_a?(Symbol) || action.is_a?(String) I18n.t( "admin.actions.#{action.i18n_key}.#{label}", model_label: model_config&.label, model_label_plural: model_config&.label_plural, object_label: model_config && object.try(model_config.object_label_method) ) end |