-
Notifications
You must be signed in to change notification settings - Fork 179
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
Add Tailwind styled scaffold templates #71
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ | |
/node_modules | ||
.byebug_history | ||
*.gem | ||
.idea/ | ||
.idea/ | ||
**/tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
end | ||
|
||
private | ||
def available_views | ||
%w(index edit show new _form) | ||
end | ||
end | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add CR |
43 changes: 43 additions & 0 deletions
43
lib/generators/tailwindcss/scaffold/templates/_form.html.erb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<%%= form_with(model: <%= model_resource_name %>, class: "contents") do |form| %> | ||
<%% if <%= singular_table_name %>.errors.any? %> | ||
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3"> | ||
<h2><%%= pluralize(<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2> | ||
|
||
<ul> | ||
<%% <%= singular_table_name %>.errors.each do |error| %> | ||
<li><%%= error.full_message %></li> | ||
<%% end %> | ||
</ul> | ||
</div> | ||
<%% end %> | ||
|
||
<% attributes.each do |attribute| -%> | ||
<div class="my-5"> | ||
<% if attribute.password_digest? -%> | ||
<%%= form.label :password %> | ||
<%%= form.password_field :password %> | ||
</div> | ||
|
||
<div class="my-5"> | ||
<%%= form.label :password_confirmation %> | ||
<%%= form.password_field :password_confirmation, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %> | ||
<% elsif attribute.attachments? -%> | ||
<%%= form.label :<%= attribute.column_name %> %> | ||
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %> | ||
<% else -%> | ||
<%%= form.label :<%= attribute.column_name %> %> | ||
<% if attribute.field_type == :text_area -%> | ||
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, rows: 4, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %> | ||
<% elsif attribute.field_type == :check_box -%> | ||
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, class: "block mt-2 h-5 w-5" %> | ||
<% else -%> | ||
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %> | ||
<% end -%> | ||
<% end -%> | ||
</div> | ||
|
||
<% end -%> | ||
<div class="inline"> | ||
<%%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium" %> | ||
</div> | ||
<%% end %> |
8 changes: 8 additions & 0 deletions
8
lib/generators/tailwindcss/scaffold/templates/edit.html.erb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div class="mx-auto md:w-2/3 w-full"> | ||
<h1 class="font-bold text-4xl">Editing <%= human_name.downcase %></h1> | ||
|
||
<%%= render "form", <%= singular_table_name %>: @<%= singular_table_name %> %> | ||
|
||
<%%= 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> |
14 changes: 14 additions & 0 deletions
14
lib/generators/tailwindcss/scaffold/templates/index.html.erb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<div class="w-full"> | ||
<%% if notice.present? %> | ||
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%%= notice %></p> | ||
<%% end %> | ||
|
||
<div class="flex justify-between items-center"> | ||
<h1 class="text-lg font-bold text-4xl"><%= human_name.pluralize %></h1> | ||
<%%= link_to 'New <%= human_name.downcase %>', new_<%= singular_route_name %>_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> | ||
</div> | ||
|
||
<div id="<%= plural_table_name %>" class="min-w-full"> | ||
<%%= render @<%= plural_table_name %> %> | ||
</div> | ||
</div> |
7 changes: 7 additions & 0 deletions
7
lib/generators/tailwindcss/scaffold/templates/new.html.erb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div class="mx-auto md:w-2/3 w-full"> | ||
<h1 class="text-lg font-bold text-4xl">New <%= human_name.downcase %></h1> | ||
|
||
<%%= 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> |
22 changes: 22 additions & 0 deletions
22
lib/generators/tailwindcss/scaffold/templates/partial.html.erb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div id="<%%= dom_id <%= singular_table_name %> %>"> | ||
<% attributes.reject(&:password_digest?).each do |attribute| -%> | ||
<p class="my-5"> | ||
<strong class="block font-medium mb-1"><%= attribute.human_name %>:</strong> | ||
<% if attribute.attachment? -%> | ||
<%%= link_to <%= singular_table_name %>.<%= attribute.column_name %>.filename, <%= singular_table_name %>.<%= attribute.column_name %> if <%= singular_table_name %>.<%= attribute.column_name %>.attached? %> | ||
<% elsif attribute.attachments? -%> | ||
<%% <%= singular_table_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>| %> | ||
<div><%%= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %> %></div> | ||
<%% end %> | ||
<% else -%> | ||
<%%= <%= singular_table_name %>.<%= attribute.column_name %> %> | ||
<% end -%> | ||
</p> | ||
|
||
<% end -%> | ||
<%% if action_name != "show" %> | ||
<%%= link_to "Show this <%= human_name.downcase %>", <%= singular_table_name %>, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
<%%= 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> |
15 changes: 15 additions & 0 deletions
15
lib/generators/tailwindcss/scaffold/templates/show.html.erb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="mx-auto md:w-2/3 w-full flex"> | ||
<div class="mx-auto"> | ||
<%% if notice.present? %> | ||
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%%= notice %></p> | ||
<%% end %> | ||
|
||
<%%= render @<%= singular_table_name %> %> | ||
|
||
<%%= link_to 'Edit this <%= singular_table_name %>', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
<div class="inline-block ml-2"> | ||
<%%= button_to 'Delete this <%= singular_table_name %>', <%= singular_table_name %>_path(@<%= singular_table_name %>), method: :delete, data: { confirm: "Are you sure you want to delete this <%= singular_table_name %>?" }, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %> | ||
</div> | ||
<%%= link_to 'Back to <%= plural_table_name %>', <%= index_helper %>_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
test/lib/generators/tailwindcss/scaffold_generator_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add CR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)