diff --git a/lib/generators/haml/scaffold/scaffold_generator.rb b/lib/generators/haml/scaffold/scaffold_generator.rb index c4c3fe8..5abfcc9 100644 --- a/lib/generators/haml/scaffold/scaffold_generator.rb +++ b/lib/generators/haml/scaffold/scaffold_generator.rb @@ -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 diff --git a/lib/generators/haml/scaffold/templates/5_1/_form.html.haml b/lib/generators/haml/scaffold/templates/5_1/_form.html.haml new file mode 100644 index 0000000..a3fbd2e --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_1/_form.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/5_1/edit.html.haml b/lib/generators/haml/scaffold/templates/5_1/edit.html.haml new file mode 100644 index 0000000..600b39b --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_1/edit.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/5_1/index.html.haml b/lib/generators/haml/scaffold/templates/5_1/index.html.haml new file mode 100644 index 0000000..0d3ca3c --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_1/index.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/5_1/new.html.haml b/lib/generators/haml/scaffold/templates/5_1/new.html.haml new file mode 100644 index 0000000..d32ddef --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_1/new.html.haml @@ -0,0 +1,5 @@ +%h1 New <%= singular_table_name.titleize %> + += render 'form', <%= singular_table_name %>: @<%= singular_table_name %> + += link_to 'Back', <%= index_helper %>_path diff --git a/lib/generators/haml/scaffold/templates/5_1/show.html.haml b/lib/generators/haml/scaffold/templates/5_1/show.html.haml new file mode 100644 index 0000000..6fed0d2 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_1/show.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/5_2/_form.html.haml b/lib/generators/haml/scaffold/templates/5_2/_form.html.haml new file mode 100644 index 0000000..b5ef886 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_2/_form.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/5_2/edit.html.haml b/lib/generators/haml/scaffold/templates/5_2/edit.html.haml new file mode 100644 index 0000000..600b39b --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_2/edit.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/5_2/index.html.haml b/lib/generators/haml/scaffold/templates/5_2/index.html.haml new file mode 100644 index 0000000..c7c79a7 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_2/index.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/5_2/new.html.haml b/lib/generators/haml/scaffold/templates/5_2/new.html.haml new file mode 100644 index 0000000..d32ddef --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_2/new.html.haml @@ -0,0 +1,5 @@ +%h1 New <%= singular_table_name.titleize %> + += render 'form', <%= singular_table_name %>: @<%= singular_table_name %> + += link_to 'Back', <%= index_helper %>_path diff --git a/lib/generators/haml/scaffold/templates/5_2/show.html.haml b/lib/generators/haml/scaffold/templates/5_2/show.html.haml new file mode 100644 index 0000000..6fed0d2 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/5_2/show.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/6_0/_form.html.haml b/lib/generators/haml/scaffold/templates/6_0/_form.html.haml new file mode 100644 index 0000000..3dd06e1 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_0/_form.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/6_0/edit.html.haml b/lib/generators/haml/scaffold/templates/6_0/edit.html.haml new file mode 100644 index 0000000..600b39b --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_0/edit.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/6_0/index.html.haml b/lib/generators/haml/scaffold/templates/6_0/index.html.haml new file mode 100644 index 0000000..b8e77a2 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_0/index.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/6_0/new.html.haml b/lib/generators/haml/scaffold/templates/6_0/new.html.haml new file mode 100644 index 0000000..d32ddef --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_0/new.html.haml @@ -0,0 +1,5 @@ +%h1 New <%= singular_table_name.titleize %> + += render 'form', <%= singular_table_name %>: @<%= singular_table_name %> + += link_to 'Back', <%= index_helper %>_path diff --git a/lib/generators/haml/scaffold/templates/6_0/show.html.haml b/lib/generators/haml/scaffold/templates/6_0/show.html.haml new file mode 100644 index 0000000..51fce01 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_0/show.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/6_1/_form.html.haml b/lib/generators/haml/scaffold/templates/6_1/_form.html.haml new file mode 100644 index 0000000..54ec7aa --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_1/_form.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/6_1/edit.html.haml b/lib/generators/haml/scaffold/templates/6_1/edit.html.haml new file mode 100644 index 0000000..600b39b --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_1/edit.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/6_1/index.html.haml b/lib/generators/haml/scaffold/templates/6_1/index.html.haml new file mode 100644 index 0000000..b8e77a2 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_1/index.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/6_1/new.html.haml b/lib/generators/haml/scaffold/templates/6_1/new.html.haml new file mode 100644 index 0000000..d32ddef --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_1/new.html.haml @@ -0,0 +1,5 @@ +%h1 New <%= singular_table_name.titleize %> + += render 'form', <%= singular_table_name %>: @<%= singular_table_name %> + += link_to 'Back', <%= index_helper %>_path diff --git a/lib/generators/haml/scaffold/templates/6_1/show.html.haml b/lib/generators/haml/scaffold/templates/6_1/show.html.haml new file mode 100644 index 0000000..51fce01 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/6_1/show.html.haml @@ -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 diff --git a/lib/generators/haml/scaffold/templates/7_0/_form.html.haml b/lib/generators/haml/scaffold/templates/7_0/_form.html.haml new file mode 100644 index 0000000..e48ce78 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_0/_form.html.haml @@ -0,0 +1,29 @@ += form_with(model: <%= model_resource_name %>) do |form| + - if <%= singular_table_name %>.errors.any? + %div{style: "color: red"} + %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 +<% if attribute.password_digest? -%> + = form.label :password, style: "display: block" + = form.password_field :password + + %div + = form.label :password_confirmation, style: "display: block" + = form.password_field :password_confirmation +<% elsif attribute.attachments? -%> + = form.label :<%= attribute.column_name %>, style: "display: block" + = form.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true +<% else -%> + = form.label :<%= attribute.column_name %>, style: "display: block" + = form.<%= attribute.field_type %> :<%= attribute.column_name %> +<% end -%> + +<% end -%> + %div + = form.submit diff --git a/lib/generators/haml/scaffold/templates/7_0/edit.html.haml b/lib/generators/haml/scaffold/templates/7_0/edit.html.haml new file mode 100644 index 0000000..c2f35d5 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_0/edit.html.haml @@ -0,0 +1,10 @@ +%h1 Editing <%= human_name.downcase %> + += render "form", <%= singular_table_name %>: @<%= singular_table_name %> + +%br + +%div + = link_to "Show this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %> + | + = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %> diff --git a/lib/generators/haml/scaffold/templates/7_0/index.html.haml b/lib/generators/haml/scaffold/templates/7_0/index.html.haml new file mode 100644 index 0000000..ead8458 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_0/index.html.haml @@ -0,0 +1,11 @@ +%p{style: "color: green"}= notice + +%h1 <%= human_name.pluralize %> + +%div{id: "<%= plural_table_name %>"} + - @<%= plural_table_name %>.each do |<%= singular_table_name %>| + = render <%= singular_table_name %> + %p + = link_to "Show this <%= human_name.downcase %>", <%= model_resource_name(singular_table_name) %> + += link_to "New <%= human_name.downcase %>", <%= new_helper(type: :path) %> diff --git a/lib/generators/haml/scaffold/templates/7_0/new.html.haml b/lib/generators/haml/scaffold/templates/7_0/new.html.haml new file mode 100644 index 0000000..22d7aeb --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_0/new.html.haml @@ -0,0 +1,8 @@ +%h1 New <%= human_name.downcase %> + += render "form", <%= singular_table_name %>: @<%= singular_table_name %> + +%br + +%div + = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %> diff --git a/lib/generators/haml/scaffold/templates/7_0/partial.html.haml b/lib/generators/haml/scaffold/templates/7_0/partial.html.haml new file mode 100644 index 0000000..5afa8d9 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_0/partial.html.haml @@ -0,0 +1,14 @@ +%div{id: dom_id(<%= singular_name %>)} +<% attributes.reject(&:password_digest?).each do |attribute| -%> + %p + %strong <%= attribute.human_name %>: +<% if attribute.attachment? -%> + = link_to <%= singular_name %>.<%= attribute.column_name %>.filename, <%= singular_name %>.<%= attribute.column_name %> if <%= singular_name %>.<%= attribute.column_name %>.attached? +<% elsif attribute.attachments? -%> + - <%= singular_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>| + %div= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %> +<% else -%> + = <%= singular_name %>.<%= attribute.column_name %> +<% end -%> + +<% end -%> diff --git a/lib/generators/haml/scaffold/templates/7_0/show.html.haml b/lib/generators/haml/scaffold/templates/7_0/show.html.haml new file mode 100644 index 0000000..c81ab50 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_0/show.html.haml @@ -0,0 +1,10 @@ +%p{style: "color: green"}= notice + += render @<%= singular_table_name %> + +%div + = link_to "Edit this <%= human_name.downcase %>", <%= edit_helper(type: :path) %> + | + = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %> + + = button_to "Destroy this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %>, method: :delete diff --git a/lib/generators/haml/scaffold/templates/7_1 b/lib/generators/haml/scaffold/templates/7_1 new file mode 120000 index 0000000..b9992fa --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_1 @@ -0,0 +1 @@ +7_0 \ No newline at end of file diff --git a/lib/generators/haml/scaffold/templates/7_2/_form.html.haml b/lib/generators/haml/scaffold/templates/7_2/_form.html.haml new file mode 100644 index 0000000..e48ce78 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_2/_form.html.haml @@ -0,0 +1,29 @@ += form_with(model: <%= model_resource_name %>) do |form| + - if <%= singular_table_name %>.errors.any? + %div{style: "color: red"} + %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 +<% if attribute.password_digest? -%> + = form.label :password, style: "display: block" + = form.password_field :password + + %div + = form.label :password_confirmation, style: "display: block" + = form.password_field :password_confirmation +<% elsif attribute.attachments? -%> + = form.label :<%= attribute.column_name %>, style: "display: block" + = form.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true +<% else -%> + = form.label :<%= attribute.column_name %>, style: "display: block" + = form.<%= attribute.field_type %> :<%= attribute.column_name %> +<% end -%> + +<% end -%> + %div + = form.submit diff --git a/lib/generators/haml/scaffold/templates/7_2/edit.html.haml b/lib/generators/haml/scaffold/templates/7_2/edit.html.haml new file mode 100644 index 0000000..40d6e76 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_2/edit.html.haml @@ -0,0 +1,12 @@ +- content_for :title, "Editing <%= human_name.downcase %>" + +%h1 Editing <%= human_name.downcase %> + += render "form", <%= singular_table_name %>: @<%= singular_table_name %> + +%br + +%div + = link_to "Show this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %> + | + = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %> diff --git a/lib/generators/haml/scaffold/templates/7_2/index.html.haml b/lib/generators/haml/scaffold/templates/7_2/index.html.haml new file mode 100644 index 0000000..0edae0f --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_2/index.html.haml @@ -0,0 +1,13 @@ +%p{style: "color: green"}= notice + +- content_for :title, "<%= human_name.pluralize %>" + +%h1 <%= human_name.pluralize %> + +%div{id: "<%= plural_table_name %>"} + - @<%= plural_table_name %>.each do |<%= singular_table_name %>| + = render <%= singular_table_name %> + %p + = link_to "Show this <%= human_name.downcase %>", <%= model_resource_name(singular_table_name) %> + += link_to "New <%= human_name.downcase %>", <%= new_helper(type: :path) %> diff --git a/lib/generators/haml/scaffold/templates/7_2/new.html.haml b/lib/generators/haml/scaffold/templates/7_2/new.html.haml new file mode 100644 index 0000000..2aea9bb --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_2/new.html.haml @@ -0,0 +1,10 @@ +- content_for :title, "New <%= human_name.downcase %>" + +%h1 New <%= human_name.downcase %> + += render "form", <%= singular_table_name %>: @<%= singular_table_name %> + +%br + +%div + = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %> diff --git a/lib/generators/haml/scaffold/templates/7_2/partial.html.haml b/lib/generators/haml/scaffold/templates/7_2/partial.html.haml new file mode 100644 index 0000000..5afa8d9 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_2/partial.html.haml @@ -0,0 +1,14 @@ +%div{id: dom_id(<%= singular_name %>)} +<% attributes.reject(&:password_digest?).each do |attribute| -%> + %p + %strong <%= attribute.human_name %>: +<% if attribute.attachment? -%> + = link_to <%= singular_name %>.<%= attribute.column_name %>.filename, <%= singular_name %>.<%= attribute.column_name %> if <%= singular_name %>.<%= attribute.column_name %>.attached? +<% elsif attribute.attachments? -%> + - <%= singular_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>| + %div= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %> +<% else -%> + = <%= singular_name %>.<%= attribute.column_name %> +<% end -%> + +<% end -%> diff --git a/lib/generators/haml/scaffold/templates/7_2/show.html.haml b/lib/generators/haml/scaffold/templates/7_2/show.html.haml new file mode 100644 index 0000000..c81ab50 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/7_2/show.html.haml @@ -0,0 +1,10 @@ +%p{style: "color: green"}= notice + += render @<%= singular_table_name %> + +%div + = link_to "Edit this <%= human_name.downcase %>", <%= edit_helper(type: :path) %> + | + = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %> + + = button_to "Destroy this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %>, method: :delete diff --git a/lib/generators/haml/scaffold/templates/8_0 b/lib/generators/haml/scaffold/templates/8_0 new file mode 120000 index 0000000..69ea72a --- /dev/null +++ b/lib/generators/haml/scaffold/templates/8_0 @@ -0,0 +1 @@ +7_2 \ No newline at end of file diff --git a/lib/generators/haml/scaffold/templates/_form.html.haml b/lib/generators/haml/scaffold/templates/_form.html.haml deleted file mode 100644 index 993cd03..0000000 --- a/lib/generators/haml/scaffold/templates/_form.html.haml +++ /dev/null @@ -1,15 +0,0 @@ -= form_for @<%= singular_table_name %> do |f| - - if @<%= singular_table_name %>.errors.any? - #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 - -<% for attribute in attributes -%> - .field - = f.label :<%= attribute.name %> - = f.<%= attribute.field_type %> :<%= attribute.name %> -<% end -%> - .actions - = f.submit 'Save' diff --git a/lib/generators/haml/scaffold/templates/_form.html.haml b/lib/generators/haml/scaffold/templates/_form.html.haml new file mode 120000 index 0000000..1af6748 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/_form.html.haml @@ -0,0 +1 @@ +8_0/_form.html.haml \ No newline at end of file diff --git a/lib/generators/haml/scaffold/templates/edit.html.haml b/lib/generators/haml/scaffold/templates/edit.html.haml deleted file mode 100644 index 6fb96b8..0000000 --- a/lib/generators/haml/scaffold/templates/edit.html.haml +++ /dev/null @@ -1,7 +0,0 @@ -%h1 Editing <%= singular_table_name %> - -= render 'form' - -= link_to 'Show', @<%= singular_table_name %> -\| -= link_to 'Back', <%= index_helper %>_path diff --git a/lib/generators/haml/scaffold/templates/edit.html.haml b/lib/generators/haml/scaffold/templates/edit.html.haml new file mode 120000 index 0000000..464480b --- /dev/null +++ b/lib/generators/haml/scaffold/templates/edit.html.haml @@ -0,0 +1 @@ +8_0/edit.html.haml \ No newline at end of file diff --git a/lib/generators/haml/scaffold/templates/index.html.haml b/lib/generators/haml/scaffold/templates/index.html.haml deleted file mode 100644 index d3d21bb..0000000 --- a/lib/generators/haml/scaffold/templates/index.html.haml +++ /dev/null @@ -1,25 +0,0 @@ -%h1 Listing <%= plural_table_name %> - -%table - %thead - %tr -<% for attribute in attributes -%> - %th <%= attribute.human_name %> -<% end -%> - %th - %th - %th - - %tbody - - @<%= plural_table_name %>.each do |<%= singular_table_name %>| - %tr -<% for attribute in attributes -%> - %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 <%= human_name %>', new_<%= singular_table_name %>_path diff --git a/lib/generators/haml/scaffold/templates/index.html.haml b/lib/generators/haml/scaffold/templates/index.html.haml new file mode 120000 index 0000000..c5d79da --- /dev/null +++ b/lib/generators/haml/scaffold/templates/index.html.haml @@ -0,0 +1 @@ +8_0/index.html.haml \ No newline at end of file diff --git a/lib/generators/haml/scaffold/templates/new.html.haml b/lib/generators/haml/scaffold/templates/new.html.haml deleted file mode 100644 index 5467854..0000000 --- a/lib/generators/haml/scaffold/templates/new.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -%h1 New <%= singular_table_name %> - -= render 'form' - -= link_to 'Back', <%= index_helper %>_path diff --git a/lib/generators/haml/scaffold/templates/new.html.haml b/lib/generators/haml/scaffold/templates/new.html.haml new file mode 120000 index 0000000..12436a2 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/new.html.haml @@ -0,0 +1 @@ +8_0/new.html.haml \ No newline at end of file diff --git a/lib/generators/haml/scaffold/templates/partial.html.haml b/lib/generators/haml/scaffold/templates/partial.html.haml new file mode 120000 index 0000000..90996c5 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/partial.html.haml @@ -0,0 +1 @@ +8_0/partial.html.haml \ No newline at end of file diff --git a/lib/generators/haml/scaffold/templates/show.html.haml b/lib/generators/haml/scaffold/templates/show.html.haml deleted file mode 100644 index 14b6280..0000000 --- a/lib/generators/haml/scaffold/templates/show.html.haml +++ /dev/null @@ -1,11 +0,0 @@ -%p#notice= notice - -<% for attribute in attributes -%> -%p - %b <%= 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 diff --git a/lib/generators/haml/scaffold/templates/show.html.haml b/lib/generators/haml/scaffold/templates/show.html.haml new file mode 120000 index 0000000..39855f3 --- /dev/null +++ b/lib/generators/haml/scaffold/templates/show.html.haml @@ -0,0 +1 @@ +8_0/show.html.haml \ No newline at end of file diff --git a/test/lib/generators/haml/scaffold_template_contents_test.rb b/test/lib/generators/haml/scaffold_template_contents_test.rb new file mode 100644 index 0000000..0259912 --- /dev/null +++ b/test/lib/generators/haml/scaffold_template_contents_test.rb @@ -0,0 +1,74 @@ +require 'test_helper' +require 'rails/generators/rails/scaffold/scaffold_generator' +require 'generators/haml/scaffold/scaffold_generator' +require 'action_controller/test_case' + +class Haml::Generators::ScaffoldTemplateContentsTest < Rails::Generators::TestCase + destination File.join(Rails.root) + tests Rails::Generators::ScaffoldGenerator + arguments %w(person name) + + setup :prepare_destination + setup :copy_routes + + def view_context + @view_context ||= begin + require Rails.root.join('app/controllers/people_controller') + + controller = PeopleController.new + controller.set_request!(ActionDispatch::Request.empty) + controller.request.session = ActionController::TestSession.new + + if controller.lookup_context.respond_to? :append_view_paths + controller.lookup_context.append_view_paths([Rails.root.join('app/views')]) + else + controller.lookup_context.view_paths << Rails.root.join('app/views') + end + controller.instance_variable_set :@people, [] + controller.instance_variable_set :@person, Person.new(1, 'Person 1') + + controller.view_context + end + end + + def assert_html_content_with_erb_version(view, html) + view_context.lookup_context.handlers = [:erb] + erb_html = view_context.render(template: "people/#{view}") + + assert_equal scrub(erb_html), scrub(html), "#{view} template result differs from the ERB version" + end + + def scrub(html) + html = strip_whitespace html + strip_authenticity_token! html + html + end + + def strip_whitespace(html) + html_doc = Nokogiri::HTML(html) + html_doc.xpath('//text()').each do |text_node| + text_node.remove if text_node.text.strip.empty? + text_node.content = text_node.text.strip + end + html_doc.to_html + end + + def strip_authenticity_token!(html) + html.sub!(/(