diff --git a/.gitignore b/.gitignore index 62527ab3..f1417ac5 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ /node_modules .byebug_history *.gem -.idea/ \ No newline at end of file +.idea/ +**/tmp/ \ No newline at end of file diff --git a/lib/generators/tailwindcss/scaffold/scaffold_generator.rb b/lib/generators/tailwindcss/scaffold/scaffold_generator.rb new file mode 100644 index 00000000..3ae3c99d --- /dev/null +++ b/lib/generators/tailwindcss/scaffold/scaffold_generator.rb @@ -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 \ No newline at end of file diff --git a/lib/generators/tailwindcss/scaffold/templates/_form.html.erb.tt b/lib/generators/tailwindcss/scaffold/templates/_form.html.erb.tt new file mode 100644 index 00000000..a6d0dea1 --- /dev/null +++ b/lib/generators/tailwindcss/scaffold/templates/_form.html.erb.tt @@ -0,0 +1,43 @@ +<%%= form_with(model: <%= model_resource_name %>, class: "contents") do |form| %> + <%% if <%= singular_table_name %>.errors.any? %> +
+

<%%= pluralize(<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:

+ + +
+ <%% end %> + +<% attributes.each do |attribute| -%> +
+<% if attribute.password_digest? -%> + <%%= form.label :password %> + <%%= form.password_field :password %> +
+ +
+ <%%= 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 -%> +
+ +<% end -%> +
+ <%%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium" %> +
+<%% end %> diff --git a/lib/generators/tailwindcss/scaffold/templates/edit.html.erb.tt b/lib/generators/tailwindcss/scaffold/templates/edit.html.erb.tt new file mode 100644 index 00000000..34756518 --- /dev/null +++ b/lib/generators/tailwindcss/scaffold/templates/edit.html.erb.tt @@ -0,0 +1,8 @@ +
+

Editing <%= human_name.downcase %>

+ + <%%= 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" %> +
diff --git a/lib/generators/tailwindcss/scaffold/templates/index.html.erb.tt b/lib/generators/tailwindcss/scaffold/templates/index.html.erb.tt new file mode 100644 index 00000000..e5aee531 --- /dev/null +++ b/lib/generators/tailwindcss/scaffold/templates/index.html.erb.tt @@ -0,0 +1,14 @@ +
+ <%% if notice.present? %> +

<%%= notice %>

+ <%% end %> + +
+

<%= human_name.pluralize %>

+ <%%= 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" %> +
+ +
+ <%%= render @<%= plural_table_name %> %> +
+
diff --git a/lib/generators/tailwindcss/scaffold/templates/new.html.erb.tt b/lib/generators/tailwindcss/scaffold/templates/new.html.erb.tt new file mode 100644 index 00000000..3eed7024 --- /dev/null +++ b/lib/generators/tailwindcss/scaffold/templates/new.html.erb.tt @@ -0,0 +1,7 @@ +
+

New <%= human_name.downcase %>

+ + <%%= 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" %> +
diff --git a/lib/generators/tailwindcss/scaffold/templates/partial.html.erb.tt b/lib/generators/tailwindcss/scaffold/templates/partial.html.erb.tt new file mode 100644 index 00000000..301d82f2 --- /dev/null +++ b/lib/generators/tailwindcss/scaffold/templates/partial.html.erb.tt @@ -0,0 +1,22 @@ +
+<% attributes.reject(&:password_digest?).each do |attribute| -%> +

+ <%= attribute.human_name %>: +<% 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 %>| %> +

<%%= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %> %>
+ <%% end %> +<% else -%> + <%%= <%= singular_table_name %>.<%= attribute.column_name %> %> +<% end -%> +

+ +<% 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" %> +
+ <%% end %> +
diff --git a/lib/generators/tailwindcss/scaffold/templates/show.html.erb.tt b/lib/generators/tailwindcss/scaffold/templates/show.html.erb.tt new file mode 100644 index 00000000..57ff9514 --- /dev/null +++ b/lib/generators/tailwindcss/scaffold/templates/show.html.erb.tt @@ -0,0 +1,15 @@ +
+
+ <%% if notice.present? %> +

<%%= notice %>

+ <%% 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" %> +
+ <%%= 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" %> +
+ <%%= 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" %> +
+
diff --git a/lib/install/tailwindcss.rb b/lib/install/tailwindcss.rb index c903fc54..97666284 100644 --- a/lib/install/tailwindcss.rb +++ b/lib/install/tailwindcss.rb @@ -1,11 +1,13 @@ APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb") if APPLICATION_LAYOUT_PATH.exist? - say "Add Tailwindcss include tags in application layout" + say "Add Tailwindcss include tags and container element in application layout" insert_into_file APPLICATION_LAYOUT_PATH.to_s, <<~ERB.indent(4), before: /^\s*<%= stylesheet_link_tag/ <%= stylesheet_link_tag "inter-font", "data-turbo-track": "reload" %> <%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %> ERB + insert_into_file APPLICATION_LAYOUT_PATH.to_s, %(
\n ), before: /^\s*<%= yield/ + insert_into_file APPLICATION_LAYOUT_PATH.to_s, %(\n
), after: /^\s*<%= yield %>/ else say "Default application.html.erb is missing!", :red say %( Add <%= stylesheet_link_tag "inter-font", "data-turbo-track": "reload" %> and <%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %> within the tag in your custom layout.) diff --git a/lib/tailwindcss/engine.rb b/lib/tailwindcss/engine.rb index d46f5850..9329dbbd 100644 --- a/lib/tailwindcss/engine.rb +++ b/lib/tailwindcss/engine.rb @@ -19,5 +19,9 @@ class Engine < ::Rails::Engine env.cache = ActiveSupport::Cache.lookup_store(:null_store) end if Rails.env.production? end + + config.app_generators do |g| + g.template_engine :tailwindcss + end end end diff --git a/test/lib/generators/tailwindcss/scaffold_generator_test.rb b/test/lib/generators/tailwindcss/scaffold_generator_test.rb new file mode 100644 index 00000000..fd28851a --- /dev/null +++ b/test/lib/generators/tailwindcss/scaffold_generator_test.rb @@ -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 \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index d7a01c0d..8e127805 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -7,5 +7,5 @@ require_relative "../lib/tailwindcss-rails" require "rails/test_unit/reporter" -Rails::TestUnitReporter.executable = 'bin/test' +Rails::TestUnitReporter.executable = "bin/test"