From 688614eb1639c9460571a14e875c006ebbfd5e47 Mon Sep 17 00:00:00 2001 From: khangbui22 <36690900+khangbui22@users.noreply.github.com> Date: Wed, 13 Mar 2024 17:01:08 +0700 Subject: [PATCH] [#492][CRUD] Add the index view templates to the generator (#505) * [#483] Add tests to the CRUD layout * [#483] Fix the check for slim and erb layout files * [#492] Update Gemfile to include slim-rails instead of slim * [#492] Update template code and slim template index file * [#492] Fix rubocop issue * [#492] Fix spec test * [#492] Improve copy_template_files function for easier readability * [#492] Fix rubocop error * [#492] Remove unused method * [#492] Move ThorUtils to a lib * [#492] Fix rubocop * [#492] Fix rubocop --------- Co-authored-by: Xavier MALPARTY --- .template/addons/crud/lib/template.rb | 13 ++++++++++++ .../slim/scaffold/index.html.slim.tt | 21 +++++++++++++++++++ .template/addons/crud/template.rb | 2 ++ .template/addons/slim/Gemfile.rb | 2 +- .template/lib/thor_utils.rb | 14 +++++++++++++ .../addons/variants/web/crud/template_spec.rb | 4 ++++ 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 .template/addons/crud/lib/template.rb create mode 100644 .template/addons/crud/lib/templates/slim/scaffold/index.html.slim.tt create mode 100644 .template/lib/thor_utils.rb diff --git a/.template/addons/crud/lib/template.rb b/.template/addons/crud/lib/template.rb new file mode 100644 index 000000000..117cd98d3 --- /dev/null +++ b/.template/addons/crud/lib/template.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require File.expand_path('../../../lib/thor_utils', __dir__) + +use_source_path __dir__ + +def copy_template_files + ThorUtils.ignore_tt do + directory 'lib/templates', renderTemplates: false + end +end + +copy_template_files diff --git a/.template/addons/crud/lib/templates/slim/scaffold/index.html.slim.tt b/.template/addons/crud/lib/templates/slim/scaffold/index.html.slim.tt new file mode 100644 index 000000000..e28b31cc4 --- /dev/null +++ b/.template/addons/crud/lib/templates/slim/scaffold/index.html.slim.tt @@ -0,0 +1,21 @@ +h1 <%= human_name.pluralize %> + +#<%= plural_table_name %> + table.table.table-hover.table-responsive + thead + tr +<% attributes.each do |attribute| -%> + th <%= attribute.human_name %> +<% end -%> + th colspan="3" + tbody + - @<%= plural_table_name %>.each do |<%= singular_table_name %>| + tr +<% attributes.each do |attribute| -%> + td = <%= singular_table_name %>.<%= attribute.name %> +<% end -%> + td = link_to 'Show', <%= singular_table_name %>, class: 'btn btn-info' + td = link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>), class: 'btn btn-primary' + td = link_to 'Destroy', <%= singular_table_name %>, data: { turbo_method: :delete, confirm: 'Are you sure' }, class: 'btn btn-danger' + += link_to "New <%= human_name.downcase %>", <%= new_helper(type: :path) %>, class: 'btn btn-success' diff --git a/.template/addons/crud/template.rb b/.template/addons/crud/template.rb index c00dfd69f..544ad2807 100644 --- a/.template/addons/crud/template.rb +++ b/.template/addons/crud/template.rb @@ -13,3 +13,5 @@ @import 'layouts'; SCSS end + +apply 'lib/template.rb' diff --git a/.template/addons/slim/Gemfile.rb b/.template/addons/slim/Gemfile.rb index 879265d51..1d9ed2372 100644 --- a/.template/addons/slim/Gemfile.rb +++ b/.template/addons/slim/Gemfile.rb @@ -4,7 +4,7 @@ <<~RUBY # Templating - gem 'slim' # light weight template engine + gem 'slim-rails' # Slim generator for Rails RUBY end diff --git a/.template/lib/thor_utils.rb b/.template/lib/thor_utils.rb new file mode 100644 index 000000000..15fd52815 --- /dev/null +++ b/.template/lib/thor_utils.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class ThorUtils + def self.ignore_tt + # NOTE: change template extension so it would skip + # `when /#{TEMPLATE_EXTNAME}$/` condition and + # fallback to default `copy_file` + Thor::TEMPLATE_EXTNAME.concat '_no_match' # => .tt_no_match + yield + ensure + # NOTE: make sure to undo our dirty work after the block + Thor::TEMPLATE_EXTNAME.chomp! '_no_match' # => .tt + end +end diff --git a/.template/spec/addons/variants/web/crud/template_spec.rb b/.template/spec/addons/variants/web/crud/template_spec.rb index 1a51d098c..1fd376469 100644 --- a/.template/spec/addons/variants/web/crud/template_spec.rb +++ b/.template/spec/addons/variants/web/crud/template_spec.rb @@ -16,4 +16,8 @@ it 'creates app/views/layouts/application.html.slim' do expect(file('app/views/layouts/application.html.slim')).to exist end + + it 'creates lib/templates/slim/scaffold/index.html.slim' do + expect(file('lib/templates/slim/scaffold/index.html.slim.tt')).to exist + end end