Skip to content

Commit f14aa08

Browse files
committed
Support Rails 6
Add custom scaffold generator that is same as the one inside the Rails 7 in order to make this work with Rails 6.
1 parent bfb324a commit f14aa08

File tree

11 files changed

+65
-8
lines changed

11 files changed

+65
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
/node_modules
1212
.byebug_history
1313
*.gem
14-
.idea/
14+
.idea/
15+
**/tmp/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'rails/generators/erb/scaffold/scaffold_generator'
2+
require "rails/generators/resource_helpers"
3+
4+
module Tailwindcss
5+
module Generators
6+
class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
7+
include Rails::Generators::ResourceHelpers
8+
9+
source_root File.expand_path("../templates", __FILE__)
10+
11+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
12+
13+
def create_root_folder
14+
empty_directory File.join("app/views", controller_file_path)
15+
end
16+
17+
def copy_view_files
18+
available_views.each do |view|
19+
formats.each do |format|
20+
filename = filename_with_extensions(view, format)
21+
template filename, File.join("app/views", controller_file_path, filename)
22+
end
23+
end
24+
25+
template "partial.html.erb", File.join("app/views", controller_file_path, "_#{singular_table_name}.html.erb")
26+
end
27+
28+
private
29+
def available_views
30+
%w(index edit show new _form)
31+
end
32+
end
33+
end
34+
end

lib/templates/erb/scaffold/edit.html.erb.tt renamed to lib/generators/tailwindcss/scaffold/templates/edit.html.erb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
<%%= link_to "Show this <%= human_name.downcase %>", @<%= singular_table_name %>, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
77
<%%= link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper %>_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
8-
</div>
8+
</div>

lib/templates/erb/scaffold/new.html.erb.tt renamed to lib/generators/tailwindcss/scaffold/templates/new.html.erb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
<%%= render "form", <%= singular_table_name %>: @<%= singular_table_name %> %>
55

66
<%%= link_to 'Back to <%= human_name.pluralize.downcase %>', <%= index_helper %>_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
7-
</div>
7+
</div>

lib/templates/erb/scaffold/partial.html.erb.tt renamed to lib/generators/tailwindcss/scaffold/templates/partial.html.erb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
<%%= link_to 'Edit this <%= human_name.downcase %>', edit_<%= singular_table_name %>_path(<%= singular_table_name %>), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
2020
<hr class="mt-6">
2121
<%% end %>
22-
</div>
22+
</div>

lib/tailwindcss/engine.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class Engine < ::Rails::Engine
2020
end if Rails.env.production?
2121
end
2222

23-
config.app_generators do |g|
24-
g.templates.unshift File::expand_path('../../templates', __FILE__)
25-
end
23+
config.app_generators do |g|
24+
#g.templates.unshift File::expand_path('../../templates', __FILE__)
25+
g.template_engine :tailwindcss
26+
end
2627
end
2728
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "test_helper"
2+
require "generators/tailwindcss/scaffold/scaffold_generator"
3+
4+
class Tailwindcss::Generators::ScaffoldGeneratorTest < Rails::Generators::TestCase
5+
GENERATION_PATH = File.expand_path("../tmp", File.dirname(__FILE__))
6+
7+
tests Tailwindcss::Generators::ScaffoldGenerator
8+
destination GENERATION_PATH
9+
10+
arguments %w(message title:string content:text)
11+
12+
Minitest.after_run do
13+
FileUtils.rm_rf GENERATION_PATH
14+
end
15+
16+
test "generates correct view templates" do
17+
run_generator
18+
19+
%w(index edit new show _form _message).each { |view| assert_file "app/views/messages/#{view}.html.erb" }
20+
end
21+
end

test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
require_relative "../lib/tailwindcss-rails"
88

99
require "rails/test_unit/reporter"
10-
Rails::TestUnitReporter.executable = 'bin/test'
10+
Rails::TestUnitReporter.executable = "bin/test"
1111

0 commit comments

Comments
 (0)