Skip to content

Add Tailwind styled scaffold templates #71

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

Merged
merged 4 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
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.
  • Loading branch information
dixpac committed Sep 13, 2021
commit f14aa08d0b5f6c4b883665e31f19be0346fad00c
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
/node_modules
.byebug_history
*.gem
.idea/
.idea/
**/tmp/
34 changes: 34 additions & 0 deletions lib/generators/tailwindcss/scaffold/scaffold_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'rails/generators/erb/scaffold/scaffold_generator'
require "rails/generators/resource_helpers"

module Tailwindcss
module Generators
class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
include Rails::Generators::ResourceHelpers

source_root File.expand_path("../templates", __FILE__)

argument :attributes, type: :array, default: [], banner: "field:type field:type"

def create_root_folder
empty_directory File.join("app/views", controller_file_path)
end

def copy_view_files
available_views.each do |view|
formats.each do |format|
filename = filename_with_extensions(view, format)
template filename, File.join("app/views", controller_file_path, filename)
end
end

template "partial.html.erb", File.join("app/views", controller_file_path, "_#{singular_table_name}.html.erb")
Copy link
Contributor

@dixpac dixpac Sep 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to get the partial in rails 6 I had to "re-implement/copy" Rails 7 scaffold generator :)

end

private
def available_views
%w(index edit show new _form)
end
end
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CR

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

<%%= 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" %>
<%%= 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" %>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<%%= render "form", <%= singular_table_name %>: @<%= singular_table_name %> %>

<%%= 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" %>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
<%%= 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" %>
<hr class="mt-6">
<%% end %>
</div>
</div>
7 changes: 4 additions & 3 deletions lib/tailwindcss/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class Engine < ::Rails::Engine
end if Rails.env.production?
end

config.app_generators do |g|
g.templates.unshift File::expand_path('../../templates', __FILE__)
end
config.app_generators do |g|
#g.templates.unshift File::expand_path('../../templates', __FILE__)
g.template_engine :tailwindcss
end
end
end
21 changes: 21 additions & 0 deletions test/lib/generators/tailwindcss/scaffold_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "test_helper"
require "generators/tailwindcss/scaffold/scaffold_generator"

class Tailwindcss::Generators::ScaffoldGeneratorTest < Rails::Generators::TestCase
GENERATION_PATH = File.expand_path("../tmp", File.dirname(__FILE__))

tests Tailwindcss::Generators::ScaffoldGenerator
destination GENERATION_PATH

arguments %w(message title:string content:text)

Minitest.after_run do
FileUtils.rm_rf GENERATION_PATH
end

test "generates correct view templates" do
run_generator

%w(index edit new show _form _message).each { |view| assert_file "app/views/messages/#{view}.html.erb" }
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CR

2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
require_relative "../lib/tailwindcss-rails"

require "rails/test_unit/reporter"
Rails::TestUnitReporter.executable = 'bin/test'
Rails::TestUnitReporter.executable = "bin/test"