|
2 | 2 |
|
3 | 3 | module ApplicationHelper |
4 | 4 | include Baseline::Helper |
5 | | - |
6 | | - def page_classes |
7 | | - [ |
8 | | - controller.controller_name, |
9 | | - { "create" => "new", "update" => "edit" }.fetch(controller.action_name, controller.action_name) |
10 | | - ].join(" ") |
11 | | - end |
12 | | - |
13 | | - def alert(level, text = nil, &block) |
14 | | - tag.div class: "alert alert-#{level} alert-dismissible fade show" do |
15 | | - concat tag.button(type: "button", class: "btn-close", data: { bs_dismiss: "alert" }) |
16 | | - concat text&.html_safe || capture_haml(&block) |
17 | | - end |
18 | | - end |
19 | | - |
20 | | - def flash_messages |
21 | | - flash.map do |level, message| |
22 | | - level = { notice: "success", alert: "danger" }[level.to_sym] || level |
23 | | - alert level, message |
24 | | - end.compact.join("\n").html_safe |
25 | | - end |
26 | | - |
27 | | - def async_turbo_frame(name, **attributes, &block) |
28 | | - # If a ActiveRecord record is passed to `turbo_frame_tag`, |
29 | | - # `dom_id` is called to determine its DOM ID. |
30 | | - # This exposes the record ID, which is not desirable if the record has a slug. |
31 | | - if name.is_a?(ActiveRecord::Base) && name.respond_to?(:slug) |
32 | | - name = [name.class.to_s.underscore, name.slug].join("_") |
33 | | - end |
34 | | - |
35 | | - unless url = attributes[:src] |
36 | | - raise "async_turbo_frame needs a `src` attribute." |
37 | | - end |
38 | | - |
39 | | - uris = [ |
40 | | - url_for(url), |
41 | | - request.fullpath |
42 | | - ].map { Addressable::URI.parse _1 } |
43 | | - uris_match = %i(path query_values).all? { uris.map(&_1).uniq.size == 1 } |
44 | | - |
45 | | - if uris_match |
46 | | - turbo_frame_tag name, &block |
47 | | - else |
48 | | - turbo_frame_tag name, **attributes do |
49 | | - render "shared/loading" |
50 | | - end |
51 | | - end |
52 | | - end |
53 | 5 | end |
0 commit comments