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