From be17ba31922c57e9183660d5c5f61b535e983e5d Mon Sep 17 00:00:00 2001 From: Pascal Zumkehr Date: Sat, 18 Nov 2023 16:51:29 +0100 Subject: [PATCH] Replace rails ujs with turbo --- Gemfile | 1 + Rakefile | 48 ++++++++----------- app/assets/stylesheets/sample.scss | 2 +- app/helpers/actions_helper.rb | 6 +-- app/helpers/dry_crud/table/actions.rb | 6 +-- app/views/shared/_error_messages.html.erb | 4 +- .../controllers/admin/cities_controller.rb | 2 +- .../controllers/admin/countries_controller.rb | 2 +- .../app/controllers/people_controller.rb | 2 +- ...ajax_controller.rb => turbo_controller.rb} | 6 +-- .../app/views/admin/cities/_form.html.erb | 3 +- .../app/views/admin/cities/_form.html.haml | 3 +- .../app/views/ajax/_actions_show.html.erb | 7 --- .../app/views/ajax/_actions_show.html.haml | 7 --- test/templates/app/views/ajax/_form.html.erb | 2 - test/templates/app/views/ajax/_form.html.haml | 2 - test/templates/app/views/ajax/_hello.html.erb | 1 - .../templates/app/views/ajax/_hello.html.haml | 1 - test/templates/app/views/ajax/ajax.js.erb | 1 - test/templates/app/views/ajax/ajax.js.haml | 1 - test/templates/app/views/ajax/edit.js.erb | 1 - test/templates/app/views/ajax/edit.js.haml | 1 - test/templates/app/views/ajax/show.js.erb | 1 - test/templates/app/views/ajax/show.js.haml | 1 - test/templates/app/views/ajax/update.js.erb | 5 -- test/templates/app/views/ajax/update.js.haml | 5 -- .../{ajax => turbo}/_actions_index.html.erb | 6 +-- .../{ajax => turbo}/_actions_index.html.haml | 6 +-- .../app/views/turbo/_actions_show.html.erb | 7 +++ .../app/views/turbo/_actions_show.html.haml | 7 +++ .../templates/app/views/turbo/_hello.html.erb | 1 + .../app/views/turbo/_hello.html.haml | 1 + .../app/views/turbo/edit.turbo_stream.erb | 1 + .../app/views/turbo/edit.turbo_stream.haml | 1 + .../app/views/turbo/show.turbo_stream.erb | 1 + .../app/views/turbo/show.turbo_stream.haml | 1 + .../app/views/turbo/turbo.turbo_stream.erb | 1 + .../app/views/turbo/turbo.turbo_stream.haml | 1 + .../app/views/turbo/update.turbo_stream.erb | 1 + .../app/views/turbo/update.turbo_stream.haml | 1 + test/templates/config/locales/cities.en.yml | 12 ++--- test/templates/config/routes.rb | 6 +-- .../controllers/people_controller_spec.rb | 14 +++--- .../controllers/people_controller_test.rb | 22 ++++----- 44 files changed, 94 insertions(+), 117 deletions(-) rename test/templates/app/controllers/{ajax_controller.rb => turbo_controller.rb} (56%) delete mode 100644 test/templates/app/views/ajax/_actions_show.html.erb delete mode 100644 test/templates/app/views/ajax/_actions_show.html.haml delete mode 100644 test/templates/app/views/ajax/_form.html.erb delete mode 100644 test/templates/app/views/ajax/_form.html.haml delete mode 100644 test/templates/app/views/ajax/_hello.html.erb delete mode 100644 test/templates/app/views/ajax/_hello.html.haml delete mode 100644 test/templates/app/views/ajax/ajax.js.erb delete mode 100644 test/templates/app/views/ajax/ajax.js.haml delete mode 100644 test/templates/app/views/ajax/edit.js.erb delete mode 100644 test/templates/app/views/ajax/edit.js.haml delete mode 100644 test/templates/app/views/ajax/show.js.erb delete mode 100644 test/templates/app/views/ajax/show.js.haml delete mode 100644 test/templates/app/views/ajax/update.js.erb delete mode 100644 test/templates/app/views/ajax/update.js.haml rename test/templates/app/views/{ajax => turbo}/_actions_index.html.erb (60%) rename test/templates/app/views/{ajax => turbo}/_actions_index.html.haml (55%) create mode 100644 test/templates/app/views/turbo/_actions_show.html.erb create mode 100644 test/templates/app/views/turbo/_actions_show.html.haml create mode 100644 test/templates/app/views/turbo/_hello.html.erb create mode 100644 test/templates/app/views/turbo/_hello.html.haml create mode 100644 test/templates/app/views/turbo/edit.turbo_stream.erb create mode 100644 test/templates/app/views/turbo/edit.turbo_stream.haml create mode 100644 test/templates/app/views/turbo/show.turbo_stream.erb create mode 100644 test/templates/app/views/turbo/show.turbo_stream.haml create mode 100644 test/templates/app/views/turbo/turbo.turbo_stream.erb create mode 100644 test/templates/app/views/turbo/turbo.turbo_stream.haml create mode 100644 test/templates/app/views/turbo/update.turbo_stream.erb create mode 100644 test/templates/app/views/turbo/update.turbo_stream.haml diff --git a/Gemfile b/Gemfile index a05bb177..5a1f943c 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ gem "sprockets-rails" gem "jsbundling-rails" gem "cssbundling-rails" gem "turbo-rails" +gem "stimulus-rails" gem 'sassc-rails' diff --git a/Rakefile b/Rakefile index 41903633..86e60288 100644 --- a/Rakefile +++ b/Rakefile @@ -37,12 +37,15 @@ namespace :test do end namespace :app do - task :environment do - ENV['RAILS_ROOT'] = TEST_APP_ROOT - ENV['RAILS_ENV'] = 'test' - require(File.join(TEST_APP_ROOT, 'config', 'environment')) - end + desc "Initializes the test application with a couple of classes" + task init: [:seed, :customize] + + desc "Customize some of the functionality provided by dry_crud" + task customize: ['test:app:add_pagination', + 'test:app:use_bootstrap', + 'test:app:build_assets' + ] desc "Create a rails test application" task :create do @@ -53,10 +56,6 @@ namespace :test do File.read(File.join(File.dirname(__FILE__), 'test', 'templates', 'Gemfile.append'))) sh "cd #{TEST_APP_ROOT}; bundle install --local" # update Gemfile.lock - #sh "cd #{TEST_APP_ROOT}; bundle binstubs bundler" - #sh "cd #{TEST_APP_ROOT}; rails javascript:install:esbuild" - #sh "cd #{TEST_APP_ROOT}; yarn add esbuild from \".\"" - #sh "cd #{TEST_APP_ROOT}; gem install foreman from \".\"" sh "cd #{TEST_APP_ROOT}; rails g rspec:install" FileUtils.rm_f(File.join(TEST_APP_ROOT, @@ -87,6 +86,13 @@ namespace :test do destination_root: TEST_APP_ROOT).invoke_all end + task :environment do + ENV['RAILS_ROOT'] = TEST_APP_ROOT + ENV['RAILS_ENV'] = 'test' + + require(File.join(TEST_APP_ROOT, 'config', 'environment')) + end + desc "Populates the test application with some models and controllers" task populate: [:generate_crud] do # copy test app templates @@ -117,15 +123,6 @@ namespace :test do end end - desc "Initializes the test application with a couple of classes" - task init: [:seed, :customize] - - desc "Customize some of the functionality provided by dry_crud" - task customize: ['test:app:add_pagination', - 'test:app:add_ujs', - 'test:app:use_bootstrap' - ] - desc "Adds pagination to the test app" task :add_pagination do list_ctrl = File.join(TEST_APP_ROOT, @@ -145,17 +142,6 @@ namespace :test do "= paginate entries\n\n= render 'list'") end - desc "Adds Rails UJS to the test app" - task :add_ujs do - sh "cd #{TEST_APP_ROOT}; yarn add @rails/ujs" - app_js = File.join(TEST_APP_ROOT, 'app', 'javascript', 'application.js') - if File.exist?(app_js) && File.read(app_js) !~ /ujs/ - file_replace(app_js, - /\n\z/, - "\nimport Rails from '@rails/ujs'\nRails.start()\n") - end - end - desc "Use Boostrap Icons in the test app" task :use_bootstrap do sh "cd #{TEST_APP_ROOT}; yarn add bootstrap-icons" @@ -178,6 +164,10 @@ namespace :test do 'app', 'assets', 'stylesheets', 'sample.scss')) end + desc "Build javascript and css in the test app" + task :build_assets do + sh "cd #{TEST_APP_ROOT}; rails javascript:build css:build" + end end end diff --git a/app/assets/stylesheets/sample.scss b/app/assets/stylesheets/sample.scss index c67c680a..cf1a995f 100644 --- a/app/assets/stylesheets/sample.scss +++ b/app/assets/stylesheets/sample.scss @@ -222,7 +222,7 @@ a.icon img { .icon-plus { background-image: image-url("actions/add.png"); } -.icon-remove { +.icon-trash { background-image: image-url("actions/delete.png"); } .icon-pencil { diff --git a/app/helpers/actions_helper.rb b/app/helpers/actions_helper.rb index 163d55e6..d0e4c8e0 100644 --- a/app/helpers/actions_helper.rb +++ b/app/helpers/actions_helper.rb @@ -38,9 +38,9 @@ def edit_action_link(path = nil) # Uses the current +entry+ if no path is given. def destroy_action_link(path = nil) path ||= path_args(entry) - action_link(ti('link.delete'), 'remove', path, - data: { confirm: ti(:confirm_delete), - method: :delete }) + action_link(ti('link.delete'), 'trash', path, + data: { 'turbo-confirm': ti(:confirm_delete), + 'turbo-method': :delete }) end # Standard list action to the given path. diff --git a/app/helpers/dry_crud/table/actions.rb b/app/helpers/dry_crud/table/actions.rb index 3902202c..a812c88b 100644 --- a/app/helpers/dry_crud/table/actions.rb +++ b/app/helpers/dry_crud/table/actions.rb @@ -57,11 +57,11 @@ def destroy_action_col(**html_options, &block) action_col do |entry| path = action_path(entry, &block) if path - table_action_link('remove', + table_action_link('trash', path, **html_options.merge( - data: { confirm: ti(:confirm_delete), - method: :delete } + data: { 'turbo-confirm': ti(:confirm_delete), + 'turbo-method': :delete } )) end end diff --git a/app/views/shared/_error_messages.html.erb b/app/views/shared/_error_messages.html.erb index 575e82c4..409f20d9 100644 --- a/app/views/shared/_error_messages.html.erb +++ b/app/views/shared/_error_messages.html.erb @@ -1,8 +1,8 @@ <% if errors.any? %>
-

+

<%= ti(:"errors.header", count: errors.count, model: object.to_s) %> -

+