Skip to content
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

[#492][CRUD] Add the index view templates to the generator #505

Merged
merged 12 commits into from
Mar 13, 2024
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
12 changes: 12 additions & 0 deletions .template/lib/thor_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
khangbui22 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading