Skip to content

Modal Generator Improvements #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example_rails7/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ group :test do
gem "selenium-webdriver"
end

gem "rolemodel_rails", "~> 0.13.0", :group => :development, :path => ".."
gem "rolemodel_rails", :group => :development, :path => ".."
2 changes: 1 addition & 1 deletion example_rails8/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ group :test do
gem "selenium-webdriver"
end

gem "rolemodel_rails", "~> 0.13.0", :group => :development, :path => ".."
gem "rolemodel_rails", :group => :development, :path => ".."
47 changes: 33 additions & 14 deletions lib/generators/rolemodel/modals/modals_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@ module Rolemodel
class ModalsGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)

def configuration
say 'Configuring Generator, please answer 2 questions', :green
@panel_install = yes? 'Would you like to install RoleModel Panel as well? (Y/n)', :magenta
@node_install = yes? 'Will there be a build step for JavaScript compilation by Node? (Y/n)', :magenta
end

def install_turbo_confirm
say 'Installing Turbo Confirm package', :green

run 'yarn add @rolemodel/turbo-confirm'
if @node_install
run 'yarn add @rolemodel/turbo-confirm'
else
run 'bin/importmap pin @rolemodel/turbo-confirm'
end
end

def copy_files
say 'generating views & link helpers', :green

copy_file 'app/helpers/turbo_frame_link_helper.rb'
template 'app/helpers/turbo_frame_link_helper.rb'
template 'app/views/layouts/modal.html.slim'
template 'app/views/layouts/panel.html.slim'
copy_file 'app/views/application/_confirm.html.slim'
template('app/views/layouts/panel.html.slim') if @panel_install
template 'app/views/application/_confirm.html.slim'
end

def amend_javascript_entrypoint
say 'generating & importing javascript files', :green

copy_file 'app/javascript/controllers/toggle_controller.js'
copy_file 'app/javascript/initializers/turbo_confirm.js'
copy_file 'app/javascript/initializers/frame_missing_handler.js'
copy_file 'app/javascript/initializers/before_morph_handler.js'
template 'app/javascript/controllers/toggle_controller.js'
template 'app/javascript/initializers/turbo_confirm.js'
template 'app/javascript/initializers/frame_missing_handler.js'
template 'app/javascript/initializers/before_morph_handler.js'

append_to_file 'app/javascript/application.js', <<~JS
import './initializers/turbo_confirm.js'
Expand All @@ -33,7 +43,9 @@ def amend_javascript_entrypoint
end

def amend_stylesheet_entrypoint
say 'importing Optics stylesheets and defining custom properties', :green
return unless @panel_install

say 'importing Optics Panel add-on', :green

inject_into_file 'app/assets/stylesheets/application.scss',
after: "@import '@rolemodel/optics/dist/scss/optics';\n" do
Expand All @@ -47,11 +59,18 @@ def amend_application_layout
say 'amending application layout', :green

inject_into_file 'app/views/layouts/application.html.slim', after: /\bbody.*\n/ do
optimize_indentation <<~SLIM, 4
= turbo_frame_tag 'modal'
= turbo_frame_tag 'panel'
= render 'confirm'
SLIM
if @panel_install
optimize_indentation <<~SLIM, 4
= turbo_frame_tag 'modal'
= turbo_frame_tag 'panel'
= render 'confirm'
SLIM
else
optimize_indentation <<~SLIM, 4
= turbo_frame_tag 'modal'
= render 'confirm'
SLIM
end
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
module TurboFrameLinkHelper
def panel_link_to(name, url=nil, options={}, &block)
frame_link_helper('panel', name, url, options, &block)
def panel_link_to(...)
link_to_frame('panel', ...)
end

def modal_link_to(name, url=nil, options={}, &block)
frame_link_helper('modal', name, url, options, &block)
def modal_link_to(...)
link_to_frame('modal', ...)
end

private
def link_to_top(...)
link_to_frame('_top', ...)
end

def frame_link_helper(frame, name, url, options, &block)
def link_to_frame(frame, name, url, options = {}, &)
data_option = { data: options.fetch(:data, {}).reverse_merge({ turbo_frame: frame }) }

if block_given?
link_to(name, (url || {}).merge(data_option), &block)
link_to(name, (url || {}).merge(data_option), &)
else
link_to(name, url, options.merge(data_option))
end
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
document.addEventListener('turbo:frame-missing', (event) => {
if (<%= [("process.env.RAILS_ENV === 'production'" if @node_install), "event.target.id === 'modal'", ("event.target.id === 'panel'" if @panel_install)].compact_blank.join(" || ") -%>) {
event.preventDefault()
/**
* A "replace" visit to the page from which the modal or panel was opened is considered
* a "Page Refresh" by Turbo. Making it eligible for morphing & scroll preservation.
*/
event.detail.visit(event.detail.response.url, { action: 'replace' })
}
})
Loading