haml-rails 2.0.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +83 -0
  3. data/.gitignore +1 -0
  4. data/Gemfile +17 -2
  5. data/README.md +14 -5
  6. data/haml-rails.gemspec +8 -8
  7. data/lib/generators/haml/controller/controller_generator.rb +3 -2
  8. data/lib/generators/haml/mailer/mailer_generator.rb +7 -33
  9. data/lib/generators/haml/scaffold/scaffold_generator.rb +25 -11
  10. data/lib/generators/haml/scaffold/templates/5_1/_form.html.haml +26 -0
  11. data/lib/generators/haml/scaffold/templates/5_1/edit.html.haml +7 -0
  12. data/lib/generators/haml/scaffold/templates/5_1/index.html.haml +25 -0
  13. data/lib/generators/haml/scaffold/templates/5_1/new.html.haml +5 -0
  14. data/lib/generators/haml/scaffold/templates/5_1/show.html.haml +11 -0
  15. data/lib/generators/haml/scaffold/templates/5_2/_form.html.haml +26 -0
  16. data/lib/generators/haml/scaffold/templates/5_2/edit.html.haml +7 -0
  17. data/lib/generators/haml/scaffold/templates/5_2/index.html.haml +25 -0
  18. data/lib/generators/haml/scaffold/templates/5_2/new.html.haml +5 -0
  19. data/lib/generators/haml/scaffold/templates/5_2/show.html.haml +11 -0
  20. data/lib/generators/haml/scaffold/templates/6_0/_form.html.haml +29 -0
  21. data/lib/generators/haml/scaffold/templates/6_0/edit.html.haml +7 -0
  22. data/lib/generators/haml/scaffold/templates/6_0/index.html.haml +25 -0
  23. data/lib/generators/haml/scaffold/templates/6_0/new.html.haml +5 -0
  24. data/lib/generators/haml/scaffold/templates/6_0/show.html.haml +18 -0
  25. data/lib/generators/haml/scaffold/templates/6_1/_form.html.haml +29 -0
  26. data/lib/generators/haml/scaffold/templates/6_1/edit.html.haml +7 -0
  27. data/lib/generators/haml/scaffold/templates/6_1/index.html.haml +25 -0
  28. data/lib/generators/haml/scaffold/templates/6_1/new.html.haml +5 -0
  29. data/lib/generators/haml/scaffold/templates/6_1/show.html.haml +18 -0
  30. data/lib/generators/haml/scaffold/templates/7_0/_form.html.haml +29 -0
  31. data/lib/generators/haml/scaffold/templates/7_0/edit.html.haml +10 -0
  32. data/lib/generators/haml/scaffold/templates/7_0/index.html.haml +11 -0
  33. data/lib/generators/haml/scaffold/templates/7_0/new.html.haml +8 -0
  34. data/lib/generators/haml/scaffold/templates/7_0/partial.html.haml +14 -0
  35. data/lib/generators/haml/scaffold/templates/7_0/show.html.haml +10 -0
  36. data/lib/generators/haml/scaffold/templates/7_1 +1 -0
  37. data/lib/generators/haml/scaffold/templates/7_2/_form.html.haml +29 -0
  38. data/lib/generators/haml/scaffold/templates/7_2/edit.html.haml +12 -0
  39. data/lib/generators/haml/scaffold/templates/7_2/index.html.haml +13 -0
  40. data/lib/generators/haml/scaffold/templates/7_2/new.html.haml +10 -0
  41. data/lib/generators/haml/scaffold/templates/7_2/partial.html.haml +14 -0
  42. data/lib/generators/haml/scaffold/templates/7_2/show.html.haml +10 -0
  43. data/lib/generators/haml/scaffold/templates/8_0 +1 -0
  44. data/lib/generators/haml/scaffold/templates/_form.html.haml +1 -0
  45. data/lib/generators/haml/scaffold/templates/edit.html.haml +1 -0
  46. data/lib/generators/haml/scaffold/templates/index.html.haml +1 -0
  47. data/lib/generators/haml/scaffold/templates/new.html.haml +1 -0
  48. data/lib/generators/haml/scaffold/templates/partial.html.haml +1 -0
  49. data/lib/generators/haml/scaffold/templates/show.html.haml +1 -0
  50. data/lib/haml-rails/version.rb +3 -1
  51. data/lib/haml-rails.rb +2 -2
  52. data/lib/rails/generators/haml/application_layout/application_layout_generator.rb +8 -6
  53. data/lib/tasks/erb2haml.rake +22 -18
  54. data/test/fixtures/routes.rb +2 -0
  55. data/test/lib/generators/haml/controller_generator_test.rb +6 -3
  56. data/test/lib/generators/haml/mailer_generator_test.rb +23 -1
  57. data/test/lib/generators/haml/scaffold_generator_test.rb +4 -1
  58. data/test/lib/generators/haml/scaffold_template_contents_test.rb +76 -0
  59. data/test/test_helper.rb +44 -53
  60. metadata +52 -43
  61. data/.travis.yml +0 -23
  62. data/Appraisals +0 -27
  63. data/gemfiles/rails_5_1.gemfile +0 -10
  64. data/gemfiles/rails_5_2.gemfile +0 -10
  65. data/gemfiles/rails_6.beta2.gemfile +0 -10
  66. data/lib/generators/haml/scaffold/templates/_form.html.haml +0 -15
  67. data/lib/generators/haml/scaffold/templates/edit.html.haml +0 -7
  68. data/lib/generators/haml/scaffold/templates/index.html.haml +0 -25
  69. data/lib/generators/haml/scaffold/templates/new.html.haml +0 -5
  70. data/lib/generators/haml/scaffold/templates/show.html.haml +0 -11
  71. data/test/lib/generators/haml/testing_helper.rb +0 -1
@@ -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,0 +1,18 @@
1
+ %p{id: "notice"}= notice
2
+
3
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
4
+ %p
5
+ %strong <%= attribute.human_name %>:
6
+ <% if attribute.attachment? -%>
7
+ = link_to @<%= singular_table_name %>.<%= attribute.column_name %>.filename, @<%= singular_table_name %>.<%= attribute.column_name %> if @<%= singular_table_name %>.<%= attribute.column_name %>.attached?
8
+ <% elsif attribute.attachments? -%>
9
+ - @<%= singular_table_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>|
10
+ %div= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %>
11
+ <% else -%>
12
+ = @<%= singular_table_name %>.<%= attribute.column_name %>
13
+ <% end -%>
14
+
15
+ <% end -%>
16
+ = link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>)
17
+ |
18
+ = link_to 'Back', <%= index_helper %>_path
@@ -0,0 +1,29 @@
1
+ = form_with(model: <%= model_resource_name %>) do |form|
2
+ - if <%= singular_table_name %>.errors.any?
3
+ %div{style: "color: red"}
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.each do |error|
8
+ %li= error.full_message
9
+
10
+ <% attributes.each do |attribute| -%>
11
+ %div
12
+ <% if attribute.password_digest? -%>
13
+ = form.label :password, style: "display: block"
14
+ = form.password_field :password
15
+
16
+ %div
17
+ = form.label :password_confirmation, style: "display: block"
18
+ = form.password_field :password_confirmation
19
+ <% elsif attribute.attachments? -%>
20
+ = form.label :<%= attribute.column_name %>, style: "display: block"
21
+ = form.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true
22
+ <% else -%>
23
+ = form.label :<%= attribute.column_name %>, style: "display: block"
24
+ = form.<%= attribute.field_type %> :<%= attribute.column_name %>
25
+ <% end -%>
26
+
27
+ <% end -%>
28
+ %div
29
+ = form.submit
@@ -0,0 +1,10 @@
1
+ %h1 Editing <%= human_name.downcase %>
2
+
3
+ = render "form", <%= singular_table_name %>: @<%= singular_table_name %>
4
+
5
+ %br
6
+
7
+ %div
8
+ = link_to "Show this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %>
9
+ |
10
+ = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %>
@@ -0,0 +1,11 @@
1
+ %p{style: "color: green"}= notice
2
+
3
+ %h1 <%= human_name.pluralize %>
4
+
5
+ %div{id: "<%= plural_table_name %>"}
6
+ - @<%= plural_table_name %>.each do |<%= singular_table_name %>|
7
+ = render <%= singular_table_name %>
8
+ %p
9
+ = link_to "Show this <%= human_name.downcase %>", <%= model_resource_name(singular_table_name) %>
10
+
11
+ = link_to "New <%= human_name.downcase %>", <%= new_helper(type: :path) %>
@@ -0,0 +1,8 @@
1
+ %h1 New <%= human_name.downcase %>
2
+
3
+ = render "form", <%= singular_table_name %>: @<%= singular_table_name %>
4
+
5
+ %br
6
+
7
+ %div
8
+ = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %>
@@ -0,0 +1,14 @@
1
+ %div{id: dom_id(<%= singular_name %>)}
2
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
3
+ %p
4
+ %strong <%= attribute.human_name %>:
5
+ <% if attribute.attachment? -%>
6
+ = link_to <%= singular_name %>.<%= attribute.column_name %>.filename, <%= singular_name %>.<%= attribute.column_name %> if <%= singular_name %>.<%= attribute.column_name %>.attached?
7
+ <% elsif attribute.attachments? -%>
8
+ - <%= singular_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>|
9
+ %div= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %>
10
+ <% else -%>
11
+ = <%= singular_name %>.<%= attribute.column_name %>
12
+ <% end -%>
13
+
14
+ <% end -%>
@@ -0,0 +1,10 @@
1
+ %p{style: "color: green"}= notice
2
+
3
+ = render @<%= singular_table_name %>
4
+
5
+ %div
6
+ = link_to "Edit this <%= human_name.downcase %>", <%= edit_helper(type: :path) %>
7
+ |
8
+ = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %>
9
+
10
+ = button_to "Destroy this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %>, method: :delete
@@ -0,0 +1 @@
1
+ 7_0
@@ -0,0 +1,29 @@
1
+ = form_with(model: <%= model_resource_name %>) do |form|
2
+ - if <%= singular_table_name %>.errors.any?
3
+ %div{style: "color: red"}
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.each do |error|
8
+ %li= error.full_message
9
+
10
+ <% attributes.each do |attribute| -%>
11
+ %div
12
+ <% if attribute.password_digest? -%>
13
+ = form.label :password, style: "display: block"
14
+ = form.password_field :password
15
+
16
+ %div
17
+ = form.label :password_confirmation, style: "display: block"
18
+ = form.password_field :password_confirmation
19
+ <% elsif attribute.attachments? -%>
20
+ = form.label :<%= attribute.column_name %>, style: "display: block"
21
+ = form.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true
22
+ <% else -%>
23
+ = form.label :<%= attribute.column_name %>, style: "display: block"
24
+ = form.<%= attribute.field_type %> :<%= attribute.column_name %>
25
+ <% end -%>
26
+
27
+ <% end -%>
28
+ %div
29
+ = form.submit
@@ -0,0 +1,12 @@
1
+ - content_for :title, "Editing <%= human_name.downcase %>"
2
+
3
+ %h1 Editing <%= human_name.downcase %>
4
+
5
+ = render "form", <%= singular_table_name %>: @<%= singular_table_name %>
6
+
7
+ %br
8
+
9
+ %div
10
+ = link_to "Show this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %>
11
+ |
12
+ = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %>
@@ -0,0 +1,13 @@
1
+ %p{style: "color: green"}= notice
2
+
3
+ - content_for :title, "<%= human_name.pluralize %>"
4
+
5
+ %h1 <%= human_name.pluralize %>
6
+
7
+ %div{id: "<%= plural_table_name %>"}
8
+ - @<%= plural_table_name %>.each do |<%= singular_table_name %>|
9
+ = render <%= singular_table_name %>
10
+ %p
11
+ = link_to "Show this <%= human_name.downcase %>", <%= model_resource_name(singular_table_name) %>
12
+
13
+ = link_to "New <%= human_name.downcase %>", <%= new_helper(type: :path) %>
@@ -0,0 +1,10 @@
1
+ - content_for :title, "New <%= human_name.downcase %>"
2
+
3
+ %h1 New <%= human_name.downcase %>
4
+
5
+ = render "form", <%= singular_table_name %>: @<%= singular_table_name %>
6
+
7
+ %br
8
+
9
+ %div
10
+ = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %>
@@ -0,0 +1,14 @@
1
+ %div{id: dom_id(<%= singular_name %>)}
2
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
3
+ %p
4
+ %strong <%= attribute.human_name %>:
5
+ <% if attribute.attachment? -%>
6
+ = link_to <%= singular_name %>.<%= attribute.column_name %>.filename, <%= singular_name %>.<%= attribute.column_name %> if <%= singular_name %>.<%= attribute.column_name %>.attached?
7
+ <% elsif attribute.attachments? -%>
8
+ - <%= singular_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>|
9
+ %div= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %>
10
+ <% else -%>
11
+ = <%= singular_name %>.<%= attribute.column_name %>
12
+ <% end -%>
13
+
14
+ <% end -%>
@@ -0,0 +1,10 @@
1
+ %p{style: "color: green"}= notice
2
+
3
+ = render @<%= singular_table_name %>
4
+
5
+ %div
6
+ = link_to "Edit this <%= human_name.downcase %>", <%= edit_helper(type: :path) %>
7
+ |
8
+ = link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %>
9
+
10
+ = button_to "Destroy this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %>, method: :delete
@@ -0,0 +1 @@
1
+ 7_2
@@ -0,0 +1 @@
1
+ 8_0/_form.html.haml
@@ -0,0 +1 @@
1
+ 8_0/edit.html.haml
@@ -0,0 +1 @@
1
+ 8_0/index.html.haml
@@ -0,0 +1 @@
1
+ 8_0/new.html.haml
@@ -0,0 +1 @@
1
+ 8_0/partial.html.haml
@@ -0,0 +1 @@
1
+ 8_0/show.html.haml
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Haml
2
4
  module Rails
3
- VERSION = "2.0.1"
5
+ VERSION = "3.0.0"
4
6
  end
5
7
  end
data/lib/haml-rails.rb CHANGED
@@ -1,11 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'haml'
2
4
  require 'rails'
3
5
  require 'haml/railtie'
4
6
 
5
7
  module Haml
6
8
  module Rails
7
- class Engine < ::Rails::Engine
8
- end
9
9
  class Railtie < ::Rails::Railtie
10
10
  config.app_generators.template_engine :haml
11
11
 
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails'
2
4
  require 'shellwords'
3
5
 
4
6
  module Haml
5
7
  module Generators
6
8
  class ApplicationLayoutGenerator < ::Rails::Generators::Base
7
-
8
9
  HTML_LAYOUT_PATH = 'app/views/layouts/application.html.erb'
9
10
  HAML_LAYOUT_PATH = 'app/views/layouts/application.html.haml'
10
11
 
@@ -16,20 +17,21 @@ module Haml
16
17
  app_layout_to = ::Rails.root.join(HAML_LAYOUT_PATH).to_s
17
18
 
18
19
  if File.exist?(app_layout_from)
19
-
20
20
  if !File.exist?(app_layout_to)
21
21
  `html2haml #{app_layout_from.shellescape} #{app_layout_to.shellescape}`
22
- puts "Success! app/views/layouts/application.html.haml is created.\n" \
23
- "Please remove the erb file: app/views/layouts/application.html.erb"
22
+ if $?.success?
23
+ puts "Success! app/views/layouts/application.html.haml is created.\n" \
24
+ "Please remove the erb file: app/views/layouts/application.html.erb"
25
+ else
26
+ puts "Error! Failed to execute html2haml command."
27
+ end
24
28
  else
25
29
  puts "Error! There is a file named app/views/layouts/application.html.haml already."
26
30
  end
27
31
  else
28
32
  puts "Error! There is no file named app/views/layouts/application.html.erb."
29
33
  end
30
-
31
34
  end
32
-
33
35
  end
34
36
  end
35
37
  end
@@ -10,6 +10,8 @@ namespace :haml do
10
10
  exit
11
11
  end
12
12
 
13
+ erb_files_to_convert = erb_files.dup
14
+
13
15
  haml_files_w_out_ext = haml_files.map { |f| f.gsub(/\.haml\z/, '') }
14
16
 
15
17
  # Get a list of all those erb files that already seem to have .haml equivalents
@@ -22,21 +24,24 @@ namespace :haml do
22
24
  puts "Some of your .html.erb files seem to already have .haml equivalents:"
23
25
  already_existing.map { |f| puts "\t#{f}" }
24
26
 
25
- # Ask the user whether he/she would like to overwrite them.
26
- begin
27
- puts "Would you like to overwrite these .haml files? (y/n)"
28
- should_overwrite = STDIN.gets.chomp.downcase[0]
29
- end until ['y', 'n'].include?(should_overwrite)
27
+ if ENV.has_key?("HAML_RAILS_OVERWRITE_HAML") && (ENV["HAML_RAILS_OVERWRITE_HAML"] == "false")
28
+ should_overwrite = 'n'
29
+ else
30
+ # Ask the user whether he/she would like to overwrite them.
31
+ begin
32
+ puts "Would you like to overwrite these .haml files? (y/n)"
33
+ should_overwrite = STDIN.gets.chomp.downcase[0]
34
+ end until ['y', 'n'].include?(should_overwrite)
35
+ end
30
36
  puts '-'*80
31
37
 
32
38
  # If we are not overwriting, remove each already_existing from our erb_files list
33
39
  if should_overwrite == 'n'
34
- erb_files = erb_files - already_existing
40
+ erb_files_to_convert = erb_files - already_existing
35
41
 
36
- if erb_files.empty?
42
+ if erb_files_to_convert.empty?
37
43
  # It is possible no .erb files remain, after we remove already_existing
38
- puts "No .erb files remain. Task will now exit."
39
- return
44
+ puts "No .erb files to convert"
40
45
  end
41
46
  else
42
47
  # Delete the current .haml
@@ -44,7 +49,7 @@ namespace :haml do
44
49
  end
45
50
  end
46
51
 
47
- erb_files.each do |file|
52
+ erb_files_to_convert.each do |file|
48
53
  puts "Generating HAML for #{file}..."
49
54
  `html2haml #{file} #{file.gsub(/\.erb\z/, '.haml')}`
50
55
  end
@@ -52,20 +57,19 @@ namespace :haml do
52
57
  puts '-'*80
53
58
 
54
59
  puts "HAML generated for the following files:"
55
- erb_files.each do |file|
60
+ erb_files_to_convert.each do |file|
56
61
  puts "\t#{file}"
57
62
  end
58
63
 
59
64
  puts '-'*80
60
- begin
61
- if ENV.has_key?("HAML_RAILS_DELETE_ERB")
62
- should_delete = ENV["HAML_RAILS_DELETE_ERB"] == "true" ? "y" : "n"
63
- else
65
+ if ENV.has_key?("HAML_RAILS_DELETE_ERB") && (ENV["HAML_RAILS_DELETE_ERB"] == "true")
66
+ should_delete = 'y'
67
+ else
68
+ begin
64
69
  puts 'Would you like to delete the original .erb files? (This is not recommended unless you are under version control.) (y/n)'
65
70
  should_delete = STDIN.gets.chomp.downcase[0]
66
- end
67
- end until ['y', 'n'].include?(should_delete)
68
-
71
+ end until ['y', 'n'].include?(should_delete)
72
+ end
69
73
  if should_delete == 'y'
70
74
  puts "Deleting original .erb files."
71
75
  File.delete(*erb_files)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  TestApp.routes.draw do |map|
2
4
  # The priority is based upon order of creation:
3
5
  # first created -> highest priority.
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
- require 'lib/generators/haml/testing_helper'
4
+ require 'rails/generators/rails/controller/controller_generator'
5
+ require 'generators/haml/controller/controller_generator'
3
6
 
4
7
  class Haml::Generators::ControllerGeneratorTest < Rails::Generators::TestCase
5
8
  destination Rails.root
@@ -11,8 +14,8 @@ class Haml::Generators::ControllerGeneratorTest < Rails::Generators::TestCase
11
14
  arguments %w(Account foo bar --template-engine haml)
12
15
 
13
16
  test "should invoke haml engine" do
14
- run_generator
17
+ run_generator
15
18
  assert_file "app/views/account/foo.html.haml"
16
19
  assert_file "app/views/account/bar.html.haml"
17
- end
20
+ end
18
21
  end
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
- require 'lib/generators/haml/testing_helper'
4
+ require 'rails/generators/mailer/mailer_generator'
5
+ require 'generators/haml/mailer/mailer_generator'
3
6
 
4
7
  class Haml::Generators::MailerGeneratorTest < Rails::Generators::TestCase
5
8
  destination File.join(Rails.root)
@@ -64,4 +67,23 @@ class Haml::Generators::MailerGeneratorTest < Rails::Generators::TestCase
64
67
  end
65
68
  end
66
69
  end
70
+
71
+ test 'suffix is not duplicated' do
72
+ if ::Rails.version.to_s >= '4.2'
73
+ run_generator ['notifier_mailer', 'foo', 'bar', '--template-engine', 'haml']
74
+
75
+ assert_no_file 'app/views/notifier_mailer_mailer/'
76
+ assert_file 'app/views/notifier_mailer/'
77
+
78
+ assert_no_file 'app/views/notifier_mailer_mailer/foo.text.haml'
79
+ assert_file 'app/views/notifier_mailer/foo.text.haml'
80
+ assert_no_file 'app/views/notifier_mailer_mailer/foo.html.haml'
81
+ assert_file 'app/views/notifier_mailer/foo.html.haml'
82
+
83
+ assert_no_file 'app/views/notifier_mailer_mailer/bar.text.haml'
84
+ assert_file 'app/views/notifier_mailer/bar.text.haml'
85
+ assert_no_file 'app/views/notifier_mailer_mailer/bar.html.haml'
86
+ assert_file 'app/views/notifier_mailer/bar.html.haml'
87
+ end
88
+ end
67
89
  end
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
- require 'lib/generators/haml/testing_helper'
4
+ require 'rails/generators/rails/scaffold/scaffold_generator'
5
+ require 'generators/haml/scaffold/scaffold_generator'
3
6
 
4
7
  class Haml::Generators::ScaffoldGeneratorTest < Rails::Generators::TestCase
5
8
  destination File.join(Rails.root)
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'rails/generators/rails/scaffold/scaffold_generator'
5
+ require 'generators/haml/scaffold/scaffold_generator'
6
+ require 'action_controller/test_case'
7
+
8
+ class Haml::Generators::ScaffoldTemplateContentsTest < Rails::Generators::TestCase
9
+ destination File.join(Rails.root)
10
+ tests Rails::Generators::ScaffoldGenerator
11
+ arguments %w(person name)
12
+
13
+ setup :prepare_destination
14
+ setup :copy_routes
15
+
16
+ def view_context
17
+ @view_context ||= begin
18
+ require Rails.root.join('app/controllers/people_controller')
19
+
20
+ controller = PeopleController.new
21
+ controller.set_request!(ActionDispatch::Request.empty)
22
+ controller.request.session = ActionController::TestSession.new
23
+
24
+ if controller.lookup_context.respond_to? :append_view_paths
25
+ controller.lookup_context.append_view_paths([Rails.root.join('app/views')])
26
+ else
27
+ controller.lookup_context.view_paths << Rails.root.join('app/views')
28
+ end
29
+ controller.instance_variable_set :@people, []
30
+ controller.instance_variable_set :@person, Person.new(1, 'Person 1')
31
+
32
+ controller.view_context
33
+ end
34
+ end
35
+
36
+ def assert_html_content_with_erb_version(view, html)
37
+ view_context.lookup_context.handlers = [:erb]
38
+ erb_html = view_context.render(template: "people/#{view}")
39
+
40
+ assert_equal scrub(erb_html), scrub(html), "#{view} template result differs from the ERB version"
41
+ end
42
+
43
+ def scrub(html)
44
+ html = strip_whitespace html
45
+ strip_authenticity_token! html
46
+ html
47
+ end
48
+
49
+ def strip_whitespace(html)
50
+ html_doc = Nokogiri::HTML(html)
51
+ html_doc.xpath('//text()').each do |text_node|
52
+ text_node.remove if text_node.text.strip.empty?
53
+ text_node.content = text_node.text.strip
54
+ end
55
+ html_doc.to_html
56
+ end
57
+
58
+ def strip_authenticity_token!(html)
59
+ html.sub!(/(<input type="hidden" name="authenticity_token" value=").*?"/, '\1"')
60
+ html.sub!(/(<input autocomplete="off" name="authenticity_token" type="hidden" value=").*?"/, '\1"')
61
+ end
62
+
63
+ test 'should generate a set of templates that are 100% compatible with the original ERB templates' do
64
+ run_generator
65
+ run_generator default_arguments + %w(--template-engine haml)
66
+
67
+ %w(index edit new show).each do |view|
68
+ assert_file "app/views/people/#{view}.html.haml"
69
+
70
+ view_context.lookup_context.handlers = [:haml]
71
+ haml_html = view_context.render(template: "people/#{view}")
72
+
73
+ assert_html_content_with_erb_version view, haml_html
74
+ end
75
+ end
76
+ end