Skip to content

Commit

Permalink
Replace rails ujs with turbo
Browse files Browse the repository at this point in the history
  • Loading branch information
codez committed Nov 18, 2023
1 parent 00cdd2e commit be17ba3
Show file tree
Hide file tree
Showing 44 changed files with 94 additions and 117 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gem "sprockets-rails"
gem "jsbundling-rails"
gem "cssbundling-rails"
gem "turbo-rails"
gem "stimulus-rails"

gem 'sassc-rails'

Expand Down
48 changes: 19 additions & 29 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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"
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/sample.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/actions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/dry_crud/table/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/views/shared/_error_messages.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<% if errors.any? %>
<div id='error_explanation' class='alert alert-danger'>
<h2>
<p>
<%= ti(:"errors.header", count: errors.count, model: object.to_s) %>
</h2>
</p>
<ul>
<% errors.full_messages.each do |msg| %>
<li><%= msg %></li>
Expand Down
2 changes: 1 addition & 1 deletion test/templates/app/controllers/admin/cities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Admin
# Cities Controller nested under /admin and countries
class CitiesController < AjaxController
class CitiesController < TurboController
self.nesting = :admin, Country

self.search_columns = :name, 'countries.name'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Admin
# Countries Controller nested under /admin
class CountriesController < AjaxController
class CountriesController < TurboController
self.nesting = :admin

self.search_columns = :name, :code
Expand Down
2 changes: 1 addition & 1 deletion test/templates/app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# People Controller
class PeopleController < AjaxController
class PeopleController < TurboController
self.search_columns = [:name, :email, :remarks, 'cities.name']

self.default_sort = 'people.name, countries.code, cities.name'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Crud controller responding to js as well
class AjaxController < CrudController
def ajax; end
class TurboController < CrudController
def turbo; end

def update
super do |format, _success|
format.js
format.turbo_stream
end
end
end
3 changes: 1 addition & 2 deletions test/templates/app/views/admin/cities/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<% remote ||= false %>
<%= crud_form remote: remote do |f| %>
<%= crud_form do |f| %>
<%= f.labeled(:name, caption: 'Called') do %>
<%= f.input_field :name %>
<% end %>
Expand Down
3 changes: 1 addition & 2 deletions test/templates/app/views/admin/cities/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
- remote ||= false
= crud_form remote: remote do |f|
= crud_form do |f|
= f.labeled(:name, caption: 'Called') do
= f.input_field :name
= f.labeled_input_field :person_ids, help: 'All people living in this city'
7 changes: 0 additions & 7 deletions test/templates/app/views/ajax/_actions_show.html.erb

This file was deleted.

7 changes: 0 additions & 7 deletions test/templates/app/views/ajax/_actions_show.html.haml

This file was deleted.

2 changes: 0 additions & 2 deletions test/templates/app/views/ajax/_form.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions test/templates/app/views/ajax/_form.html.haml

This file was deleted.

1 change: 0 additions & 1 deletion test/templates/app/views/ajax/_hello.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion test/templates/app/views/ajax/_hello.html.haml

This file was deleted.

1 change: 0 additions & 1 deletion test/templates/app/views/ajax/ajax.js.erb

This file was deleted.

1 change: 0 additions & 1 deletion test/templates/app/views/ajax/ajax.js.haml

This file was deleted.

1 change: 0 additions & 1 deletion test/templates/app/views/ajax/edit.js.erb

This file was deleted.

1 change: 0 additions & 1 deletion test/templates/app/views/ajax/edit.js.haml

This file was deleted.

1 change: 0 additions & 1 deletion test/templates/app/views/ajax/show.js.erb

This file was deleted.

1 change: 0 additions & 1 deletion test/templates/app/views/ajax/show.js.haml

This file was deleted.

5 changes: 0 additions & 5 deletions test/templates/app/views/ajax/update.js.erb

This file was deleted.

5 changes: 0 additions & 5 deletions test/templates/app/views/ajax/update.js.haml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<%= render partial: 'crud/actions_index' %>

<%= action_link(ti(:'link.ajax'),
<%= action_link(ti(:'link.turbo'),
nil,
{ action: 'ajax' },
method: :get, remote: true) %>
{ action: 'turbo' },
data: { 'turbo-stream': true }) %>

<div id="response"></div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
= render 'crud/actions_index'

= action_link(ti(:'link.ajax'),
= action_link(ti(:'link.turbo'),
nil,
{ action: 'ajax' },
method: :get, remote: true)
{ action: 'turbo' },
data: { 'turbo-stream': true })

#response

Expand Down
7 changes: 7 additions & 0 deletions test/templates/app/views/turbo/_actions_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= index_action_link %>
<%= edit_action_link %>
<%= action_link(ti(:"link.edit") + ' Turbo',
'pencil',
edit_polymorphic_path(path_args(entry)),
data: { 'turbo-stream': true }) %>
<%= destroy_action_link %>
7 changes: 7 additions & 0 deletions test/templates/app/views/turbo/_actions_show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
= index_action_link
= edit_action_link
= action_link(ti(:"link.edit") + ' Turbo',
'pencil',
edit_polymorphic_path(path_args(entry)),
data: { 'turbo-stream': true })
= destroy_action_link
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/_hello.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello from turbo
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/_hello.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello from turbo
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/edit.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= turbo_stream.update(:content, partial: 'form') %>
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/edit.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= turbo_stream.update(:content, partial: 'form')
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/show.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= turbo_stream.update(:content, partial: 'attrs') %>
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/show.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= turbo_stream.update(:content, partial: 'attrs')
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/turbo.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= turbo_stream.update(:response, partial: 'hello') %>
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/turbo.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= turbo_stream.update(:response, partial: 'hello')
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= turbo_stream.update(:content, partial: entry.errors.present? ? 'form' : 'attrs') %>
1 change: 1 addition & 0 deletions test/templates/app/views/turbo/update.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= turbo_stream.update(:content, partial: entry.errors.present? ? 'form' : 'attrs')
12 changes: 6 additions & 6 deletions test/templates/config/locales/cities.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ en:
vips: VIPs
link:
maps: Maps
ajax:
turbo:
global:
link:
ajax: Ajahx
turbo: Turbo
# cities controller
admin/cities:
global:
Expand All @@ -26,7 +26,7 @@ en:
update:
flash:
success: The %{model} got an update

# people controller
people:
global:
Expand All @@ -35,13 +35,13 @@ en:
i_think_its: I think it's
nice: Nice
check_google: Check Google

# vips controller
vips:
index:
title: Listing VIPs
no_list_entries: No VIPs found

# model associations
activerecord:
errors:
Expand All @@ -53,4 +53,4 @@ en:
no_entry: Nobody
none_available: Nobody here
city:
none_available: No City
none_available: No City
6 changes: 3 additions & 3 deletions test/templates/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

resources :people do
collection do
get :ajax
get :turbo
end
end

Expand All @@ -13,12 +13,12 @@
namespace :admin do
resources :countries do
collection do
get :ajax
get :turbo
end

resources :cities do
collection do
get :ajax
get :turbo
end
end
end
Expand Down
Loading

0 comments on commit be17ba3

Please sign in to comment.