Last active
June 10, 2020 21:07
-
-
Save existentialmutt/317ac2adfa22a2fd32aa659f2fa96bea to your computer and use it in GitHub Desktop.
Confirm Navigation: Rails partial for confirming navigation with a custom modal. Built with Bootstrap and StimulusJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= render_with_slots "application/confirm_navigation" do |slots| | |
- slots.content_for :trigger do | |
%a.btn.btn-outline-danger{href: reboot_universe_path, data: {action: "confirm-navigation#confirm", method: "POST"}} | |
%i.fas.fa-trash.mr-1 | |
Reboot Universe | |
- slots.content_for :modal_content do | |
.modal-body | |
Are you sure you want to reboot the universe and restart from the big bang? This operation cannot be undone. | |
.modal-footer | |
%button.btn.btn-outline-primary{:type => "button", data: {action: "confirm-navigation#reject"}} Cancel | |
%button.btn.btn-danger{:type => "button", data: {action: "confirm-navigation#accept"}} | |
Reboot Universe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/helpers/render_with_slots_helper.rb | |
module RenderWithSlotsHelper | |
def render_with_slots(partial_path, locals = {}, &block) | |
collector = SlotCollector.new(self) | |
yield(collector) | |
render partial_path, locals.merge(slots: collector) | |
end | |
class SlotCollector | |
def initialize(view_context) | |
@slots = {} | |
@view_context = view_context | |
end | |
def content_for(slot_name, &block) | |
if block | |
@slots[slot_name] = block | |
else | |
@view_context.capture_haml(&@slots[slot_name]) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment