Skip to content

Commit

Permalink
[#492][CRUD] Add the index view templates to the generator (#505)
Browse files Browse the repository at this point in the history
* [#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 <[email protected]>
  • Loading branch information
khangbui22 and malparty authored Mar 13, 2024
1 parent cf33fad commit 688614e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .template/addons/crud/lib/template.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 2 additions & 0 deletions .template/addons/crud/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
@import 'layouts';
SCSS
end

apply 'lib/template.rb'
2 changes: 1 addition & 1 deletion .template/addons/slim/Gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<<~RUBY
# Templating
gem 'slim' # light weight template engine
gem 'slim-rails' # Slim generator for Rails
RUBY
end

Expand Down
14 changes: 14 additions & 0 deletions .template/lib/thor_utils.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions .template/spec/addons/variants/web/crud/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 688614e

Please sign in to comment.