Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions lib/generators/haml/scaffold/scaffold_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,44 @@ module Generators
class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
source_root File.expand_path("../templates", __FILE__)

argument :attributes, type: :array, default: [], banner: "field:type field:type"

hook_for :form_builder, :as => :scaffold

def copy_view_files
available_views.each do |view|
filename = filename_with_extensions(view)
template "#{view}.html.haml", File.join("app/views", controller_file_path, filename)
end
end
next if (view == '_form') && !options[:form_builder].nil?

hook_for :form_builder, :as => :scaffold
template_filename = find_template_for_rails_version view
filename = filename_with_extensions view

def copy_form_file
if options[:form_builder].nil?
filename = filename_with_extensions("_form")
template "_form.html.haml", File.join("app/views", controller_file_path, filename)
template template_filename, File.join('app/views', controller_file_path, filename)
end

if ::Rails::VERSION::MAJOR >= 7
template find_template_for_rails_version('partial'), File.join('app/views', controller_file_path, "_#{singular_name}.html.haml")
end
end

private

def available_views
%w(index edit show new)
%w(index edit show new _form)
end

def handler
:haml
end

def find_template_for_rails_version(view)
find_in_source_paths "#{rails_version}/#{view}.html.haml"
rescue Thor::Error
"#{view}.html.haml"
end

def rails_version
@rails_version ||= "#{::Rails::VERSION::MAJOR}_#{::Rails::VERSION::MINOR}"
end
end
end
end
26 changes: 26 additions & 0 deletions lib/generators/haml/scaffold/templates/5_1/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
= form_with(model: <%= singular_table_name %>, local: true) do |form|
- if <%= singular_table_name %>.errors.any?
%div{id: "error_explanation"}
%h2= "#{pluralize(<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:"

%ul
- <%= singular_table_name %>.errors.full_messages.each do |message|
%li= message

<% attributes.each do |attribute| -%>
%div{class: "field"}
<% if attribute.password_digest? -%>
= form.label :password
= form.password_field :password, id: :<%= field_id(:password) %>

%div{class: "field"}
= form.label :password_confirmation
= form.password_field :password_confirmation, id: :<%= field_id(:password_confirmation) %>
<% else -%>
= form.label :<%= attribute.column_name %>
= form.<%= attribute.field_type %> :<%= attribute.column_name %>, id: :<%= field_id(attribute.column_name) %>
<% end -%>

<% end -%>
%div{class: "actions"}
= form.submit
7 changes: 7 additions & 0 deletions lib/generators/haml/scaffold/templates/5_1/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%h1 Editing <%= singular_table_name.titleize %>

= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>

= link_to 'Show', @<%= singular_table_name %>
|
= link_to 'Back', <%= index_helper %>_path
25 changes: 25 additions & 0 deletions lib/generators/haml/scaffold/templates/5_1/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%p{id: "notice"}= notice

%h1 <%= plural_table_name.titleize %>

%table
%thead
%tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
%th <%= attribute.human_name %>
<% end -%>
%th{colspan: "3"}

%tbody
- @<%= plural_table_name %>.each do |<%= singular_table_name %>|
%tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
%td= <%= singular_table_name %>.<%= attribute.name %>
<% end -%>
%td= link_to 'Show', <%= singular_table_name %>
%td= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>)
%td= link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' }

%br

= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_table_name %>_path
5 changes: 5 additions & 0 deletions lib/generators/haml/scaffold/templates/5_1/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h1 New <%= singular_table_name.titleize %>

= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>

= link_to 'Back', <%= index_helper %>_path
11 changes: 11 additions & 0 deletions lib/generators/haml/scaffold/templates/5_1/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%p{id: "notice"}= notice

<% attributes.reject(&:password_digest?).each do |attribute| -%>
%p
%strong <%= attribute.human_name %>:
= @<%= singular_table_name %>.<%= attribute.name %>

<% end -%>
= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>)
|
= link_to 'Back', <%= index_helper %>_path
26 changes: 26 additions & 0 deletions lib/generators/haml/scaffold/templates/5_2/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
= form_with(model: <%= model_resource_name %>, local: true) do |form|
- if <%= singular_table_name %>.errors.any?
%div{id: "error_explanation"}
%h2= "#{pluralize(<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:"

%ul
- <%= singular_table_name %>.errors.full_messages.each do |message|
%li= message

<% attributes.each do |attribute| -%>
%div{class: "field"}
<% if attribute.password_digest? -%>
= form.label :password
= form.password_field :password

%div{class: "field"}
= form.label :password_confirmation
= form.password_field :password_confirmation
<% else -%>
= form.label :<%= attribute.column_name %>
= form.<%= attribute.field_type %> :<%= attribute.column_name %>
<% end -%>

<% end -%>
%div{class: "actions"}
= form.submit
7 changes: 7 additions & 0 deletions lib/generators/haml/scaffold/templates/5_2/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%h1 Editing <%= singular_table_name.titleize %>

= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>

= link_to 'Show', @<%= singular_table_name %>
|
= link_to 'Back', <%= index_helper %>_path
25 changes: 25 additions & 0 deletions lib/generators/haml/scaffold/templates/5_2/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%p{id: "notice"}= notice

%h1 <%= plural_table_name.titleize %>

%table
%thead
%tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
%th <%= attribute.human_name %>
<% end -%>
%th{colspan: "3"}

%tbody
- @<%= plural_table_name %>.each do |<%= singular_table_name %>|
%tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
%td= <%= singular_table_name %>.<%= attribute.name %>
<% end -%>
%td= link_to 'Show', <%= model_resource_name %>
%td= link_to 'Edit', edit_<%= singular_route_name %>_path(<%= singular_table_name %>)
%td= link_to 'Destroy', <%= model_resource_name %>, method: :delete, data: { confirm: 'Are you sure?' }

%br

= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_route_name %>_path
5 changes: 5 additions & 0 deletions lib/generators/haml/scaffold/templates/5_2/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h1 New <%= singular_table_name.titleize %>

= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>

= link_to 'Back', <%= index_helper %>_path
11 changes: 11 additions & 0 deletions lib/generators/haml/scaffold/templates/5_2/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%p{id: "notice"}= notice

<% attributes.reject(&:password_digest?).each do |attribute| -%>
%p
%strong <%= attribute.human_name %>:
= @<%= singular_table_name %>.<%= attribute.name %>

<% end -%>
= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>)
|
= link_to 'Back', <%= index_helper %>_path
29 changes: 29 additions & 0 deletions lib/generators/haml/scaffold/templates/6_0/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
= form_with(model: <%= model_resource_name %>, local: true) do |form|
- if <%= singular_table_name %>.errors.any?
%div{id: "error_explanation"}
%h2= "#{pluralize(<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:"

%ul
- <%= singular_table_name %>.errors.full_messages.each do |message|
%li= message

<% attributes.each do |attribute| -%>
%div{class: "field"}
<% if attribute.password_digest? -%>
= form.label :password
= form.password_field :password

%div{class: "field"}
= form.label :password_confirmation
= form.password_field :password_confirmation
<% elsif attribute.attachments? -%>
= form.label :<%= attribute.column_name %>
= form.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true
<% else -%>
= form.label :<%= attribute.column_name %>
= form.<%= attribute.field_type %> :<%= attribute.column_name %>
<% end -%>

<% end -%>
%div{class: "actions"}
= form.submit
7 changes: 7 additions & 0 deletions lib/generators/haml/scaffold/templates/6_0/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%h1 Editing <%= singular_table_name.titleize %>

= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>

= link_to 'Show', @<%= singular_table_name %>
|
= link_to 'Back', <%= index_helper %>_path
25 changes: 25 additions & 0 deletions lib/generators/haml/scaffold/templates/6_0/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%p{id: "notice"}= notice

%h1 <%= plural_table_name.titleize %>

%table
%thead
%tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
%th <%= attribute.human_name %>
<% end -%>
%th{colspan: "3"}

%tbody
- @<%= plural_table_name %>.each do |<%= singular_table_name %>|
%tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
%td= <%= singular_table_name %>.<%= attribute.column_name %>
<% end -%>
%td= link_to 'Show', <%= model_resource_name %>
%td= link_to 'Edit', edit_<%= singular_route_name %>_path(<%= singular_table_name %>)
%td= link_to 'Destroy', <%= model_resource_name %>, method: :delete, data: { confirm: 'Are you sure?' }

%br

= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_route_name %>_path
5 changes: 5 additions & 0 deletions lib/generators/haml/scaffold/templates/6_0/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h1 New <%= singular_table_name.titleize %>

= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>

= link_to 'Back', <%= index_helper %>_path
18 changes: 18 additions & 0 deletions lib/generators/haml/scaffold/templates/6_0/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%p{id: "notice"}= notice

<% attributes.reject(&:password_digest?).each do |attribute| -%>
%p
%strong <%= attribute.human_name %>:
<% if attribute.attachment? -%>
= link_to @<%= singular_table_name %>.<%= attribute.column_name %>.filename, @<%= singular_table_name %>.<%= attribute.column_name %> if @<%= singular_table_name %>.<%= attribute.column_name %>.attached?
<% elsif attribute.attachments? -%>
- @<%= singular_table_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>|
%div= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %>
<% else -%>
= @<%= singular_table_name %>.<%= attribute.column_name %>
<% end -%>

<% end -%>
= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>)
|
= link_to 'Back', <%= index_helper %>_path
29 changes: 29 additions & 0 deletions lib/generators/haml/scaffold/templates/6_1/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
= form_with(model: <%= model_resource_name %>) do |form|
- if <%= singular_table_name %>.errors.any?
%div{id: "error_explanation"}
%h2= "#{pluralize(<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:"

%ul
- <%= singular_table_name %>.errors.each do |error|
%li= error.full_message

<% attributes.each do |attribute| -%>
%div{class: "field"}
<% if attribute.password_digest? -%>
= form.label :password
= form.password_field :password

%div{class: "field"}
= form.label :password_confirmation
= form.password_field :password_confirmation
<% elsif attribute.attachments? -%>
= form.label :<%= attribute.column_name %>
= form.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true
<% else -%>
= form.label :<%= attribute.column_name %>
= form.<%= attribute.field_type %> :<%= attribute.column_name %>
<% end -%>

<% end -%>
%div{class: "actions"}
= form.submit
7 changes: 7 additions & 0 deletions lib/generators/haml/scaffold/templates/6_1/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%h1 Editing <%= singular_table_name.titleize %>

= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>

= link_to 'Show', @<%= singular_table_name %>
|
= link_to 'Back', <%= index_helper %>_path
25 changes: 25 additions & 0 deletions lib/generators/haml/scaffold/templates/6_1/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%p{id: "notice"}= notice

%h1 <%= plural_table_name.titleize %>

%table
%thead
%tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
%th <%= attribute.human_name %>
<% end -%>
%th{colspan: "3"}

%tbody
- @<%= plural_table_name %>.each do |<%= singular_table_name %>|
%tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
%td= <%= singular_table_name %>.<%= attribute.column_name %>
<% end -%>
%td= link_to 'Show', <%= model_resource_name %>
%td= link_to 'Edit', edit_<%= singular_route_name %>_path(<%= singular_table_name %>)
%td= link_to 'Destroy', <%= model_resource_name %>, method: :delete, data: { confirm: 'Are you sure?' }

%br

= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_route_name %>_path
5 changes: 5 additions & 0 deletions lib/generators/haml/scaffold/templates/6_1/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h1 New <%= singular_table_name.titleize %>

= render 'form', <%= singular_table_name %>: @<%= singular_table_name %>

= link_to 'Back', <%= index_helper %>_path
18 changes: 18 additions & 0 deletions lib/generators/haml/scaffold/templates/6_1/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%p{id: "notice"}= notice

<% attributes.reject(&:password_digest?).each do |attribute| -%>
%p
%strong <%= attribute.human_name %>:
<% if attribute.attachment? -%>
= link_to @<%= singular_table_name %>.<%= attribute.column_name %>.filename, @<%= singular_table_name %>.<%= attribute.column_name %> if @<%= singular_table_name %>.<%= attribute.column_name %>.attached?
<% elsif attribute.attachments? -%>
- @<%= singular_table_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>|
%div= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %>
<% else -%>
= @<%= singular_table_name %>.<%= attribute.column_name %>
<% end -%>

<% end -%>
= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>)
|
= link_to 'Back', <%= index_helper %>_path
Loading