Skip to content

Commit 2c51fd5

Browse files
authored
Merge pull request #197 from amatsuda/rails_versions
Let the templates generator generate 100% compatible Haml templates with the Rails' original ERB templates
2 parents 99a89bc + 9f4703d commit 2c51fd5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+616
-73
lines changed

lib/generators/haml/scaffold/scaffold_generator.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,44 @@ module Generators
55
class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
66
source_root File.expand_path("../templates", __FILE__)
77

8+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
9+
10+
hook_for :form_builder, :as => :scaffold
11+
812
def copy_view_files
913
available_views.each do |view|
10-
filename = filename_with_extensions(view)
11-
template "#{view}.html.haml", File.join("app/views", controller_file_path, filename)
12-
end
13-
end
14+
next if (view == '_form') && !options[:form_builder].nil?
1415

15-
hook_for :form_builder, :as => :scaffold
16+
template_filename = find_template_for_rails_version view
17+
filename = filename_with_extensions view
1618

17-
def copy_form_file
18-
if options[:form_builder].nil?
19-
filename = filename_with_extensions("_form")
20-
template "_form.html.haml", File.join("app/views", controller_file_path, filename)
19+
template template_filename, File.join('app/views', controller_file_path, filename)
20+
end
21+
22+
if ::Rails::VERSION::MAJOR >= 7
23+
template find_template_for_rails_version('partial'), File.join('app/views', controller_file_path, "_#{singular_name}.html.haml")
2124
end
2225
end
2326

2427
private
2528

2629
def available_views
27-
%w(index edit show new)
30+
%w(index edit show new _form)
2831
end
2932

3033
def handler
3134
:haml
3235
end
36+
37+
def find_template_for_rails_version(view)
38+
find_in_source_paths "#{rails_version}/#{view}.html.haml"
39+
rescue Thor::Error
40+
"#{view}.html.haml"
41+
end
42+
43+
def rails_version
44+
@rails_version ||= "#{::Rails::VERSION::MAJOR}_#{::Rails::VERSION::MINOR}"
45+
end
3346
end
3447
end
3548
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
= form_with(model: <%= singular_table_name %>, local: true) do |form|
2+
- if <%= singular_table_name %>.errors.any?
3+
%div{id: "error_explanation"}
4+
%h2= "#{pluralize(<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:"
5+
6+
%ul
7+
- <%= singular_table_name %>.errors.full_messages.each do |message|
8+
%li= message
9+
10+
<% attributes.each do |attribute| -%>
11+
%div{class: "field"}
12+
<% if attribute.password_digest? -%>
13+
= form.label :password
14+
= form.password_field :password, id: :<%= field_id(:password) %>
15+
16+
%div{class: "field"}
17+
= form.label :password_confirmation
18+
= form.password_field :password_confirmation, id: :<%= field_id(:password_confirmation) %>
19+
<% else -%>
20+
= form.label :<%= attribute.column_name %>
21+
= form.<%= attribute.field_type %> :<%= attribute.column_name %>, id: :<%= field_id(attribute.column_name) %>
22+
<% end -%>
23+
24+
<% end -%>
25+
%div{class: "actions"}
26+
= form.submit
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%h1 Editing <%= singular_table_name.titleize %>
2+
3+
= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>
4+
5+
= link_to 'Show', @<%= singular_table_name %>
6+
|
7+
= link_to 'Back', <%= index_helper %>_path
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
%p{id: "notice"}= notice
2+
3+
%h1 <%= plural_table_name.titleize %>
4+
5+
%table
6+
%thead
7+
%tr
8+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
9+
%th <%= attribute.human_name %>
10+
<% end -%>
11+
%th{colspan: "3"}
12+
13+
%tbody
14+
- @<%= plural_table_name %>.each do |<%= singular_table_name %>|
15+
%tr
16+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
17+
%td= <%= singular_table_name %>.<%= attribute.name %>
18+
<% end -%>
19+
%td= link_to 'Show', <%= singular_table_name %>
20+
%td= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>)
21+
%td= link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' }
22+
23+
%br
24+
25+
= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_table_name %>_path
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%h1 New <%= singular_table_name.titleize %>
2+
3+
= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>
4+
5+
= link_to 'Back', <%= index_helper %>_path
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%p{id: "notice"}= notice
2+
3+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
4+
%p
5+
%strong <%= attribute.human_name %>:
6+
= @<%= singular_table_name %>.<%= attribute.name %>
7+
8+
<% end -%>
9+
= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>)
10+
|
11+
= link_to 'Back', <%= index_helper %>_path
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
= form_with(model: <%= model_resource_name %>, local: true) do |form|
2+
- if <%= singular_table_name %>.errors.any?
3+
%div{id: "error_explanation"}
4+
%h2= "#{pluralize(<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:"
5+
6+
%ul
7+
- <%= singular_table_name %>.errors.full_messages.each do |message|
8+
%li= message
9+
10+
<% attributes.each do |attribute| -%>
11+
%div{class: "field"}
12+
<% if attribute.password_digest? -%>
13+
= form.label :password
14+
= form.password_field :password
15+
16+
%div{class: "field"}
17+
= form.label :password_confirmation
18+
= form.password_field :password_confirmation
19+
<% else -%>
20+
= form.label :<%= attribute.column_name %>
21+
= form.<%= attribute.field_type %> :<%= attribute.column_name %>
22+
<% end -%>
23+
24+
<% end -%>
25+
%div{class: "actions"}
26+
= form.submit
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%h1 Editing <%= singular_table_name.titleize %>
2+
3+
= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>
4+
5+
= link_to 'Show', @<%= singular_table_name %>
6+
|
7+
= link_to 'Back', <%= index_helper %>_path
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
%p{id: "notice"}= notice
2+
3+
%h1 <%= plural_table_name.titleize %>
4+
5+
%table
6+
%thead
7+
%tr
8+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
9+
%th <%= attribute.human_name %>
10+
<% end -%>
11+
%th{colspan: "3"}
12+
13+
%tbody
14+
- @<%= plural_table_name %>.each do |<%= singular_table_name %>|
15+
%tr
16+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
17+
%td= <%= singular_table_name %>.<%= attribute.name %>
18+
<% end -%>
19+
%td= link_to 'Show', <%= model_resource_name %>
20+
%td= link_to 'Edit', edit_<%= singular_route_name %>_path(<%= singular_table_name %>)
21+
%td= link_to 'Destroy', <%= model_resource_name %>, method: :delete, data: { confirm: 'Are you sure?' }
22+
23+
%br
24+
25+
= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_route_name %>_path
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%h1 New <%= singular_table_name.titleize %>
2+
3+
= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>
4+
5+
= link_to 'Back', <%= index_helper %>_path

0 commit comments

Comments
 (0)