phlex 0.3.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of phlex might be problematic. Click here for more details.

Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +8 -0
  3. data/.rubocop.yml +21 -5
  4. data/Gemfile +26 -12
  5. data/Procfile.dev +3 -0
  6. data/README.md +1 -1
  7. data/Rakefile +3 -5
  8. data/SECURITY.md +1 -1
  9. data/bench.rb +7 -0
  10. data/config/sus.rb +15 -0
  11. data/docs/assets/application.css +6 -0
  12. data/docs/build.rb +17 -10
  13. data/docs/components/callout.rb +1 -1
  14. data/docs/components/code_block.rb +2 -2
  15. data/docs/components/code_span.rb +9 -0
  16. data/docs/components/example.rb +5 -5
  17. data/docs/components/heading.rb +2 -2
  18. data/docs/components/layout.rb +62 -17
  19. data/docs/components/markdown.rb +14 -15
  20. data/docs/components/nav/item.rb +33 -0
  21. data/docs/components/nav.rb +6 -0
  22. data/docs/components/tabs/tab.rb +4 -2
  23. data/docs/components/tabs.rb +1 -1
  24. data/docs/components/title.rb +2 -2
  25. data/docs/page_builder.rb +3 -0
  26. data/docs/pages/application_page.rb +1 -1
  27. data/docs/pages/helpers.rb +97 -0
  28. data/docs/pages/index.rb +6 -17
  29. data/docs/pages/library/collections.rb +83 -0
  30. data/docs/pages/rails/getting_started.rb +53 -0
  31. data/docs/pages/rails/helpers.rb +55 -0
  32. data/docs/pages/rails/layouts.rb +61 -0
  33. data/docs/pages/rails/migrating.rb +37 -0
  34. data/docs/pages/rails/rendering_views.rb +35 -0
  35. data/docs/pages/templates.rb +53 -151
  36. data/docs/pages/testing/capybara.rb +48 -0
  37. data/docs/pages/testing/getting_started.rb +44 -0
  38. data/docs/pages/testing/nokogiri.rb +83 -0
  39. data/docs/pages/testing/rails.rb +17 -0
  40. data/docs/pages/translations.rb +81 -0
  41. data/docs/pages/views.rb +87 -78
  42. data/fixtures/compiler_test_helpers.rb +19 -0
  43. data/fixtures/content.rb +60 -0
  44. data/fixtures/dummy/app/components/comment_component.html.erb +14 -0
  45. data/fixtures/dummy/app/components/comment_component.rb +8 -0
  46. data/fixtures/dummy/app/components/reaction_component.html.erb +3 -0
  47. data/fixtures/dummy/app/components/reaction_component.rb +7 -0
  48. data/fixtures/dummy/app/controllers/comments_controller.rb +4 -0
  49. data/fixtures/dummy/app/views/application_view.rb +8 -0
  50. data/fixtures/dummy/app/views/articles/form.rb +3 -1
  51. data/fixtures/dummy/app/views/card.rb +4 -2
  52. data/fixtures/dummy/app/views/comments/comment.rb +25 -0
  53. data/fixtures/dummy/app/views/comments/index.html.erb +3 -0
  54. data/fixtures/dummy/app/views/comments/reaction.rb +17 -0
  55. data/fixtures/dummy/app/views/comments/show.html.erb +3 -0
  56. data/fixtures/dummy/app/views/heading.rb +1 -1
  57. data/fixtures/layout.rb +5 -5
  58. data/fixtures/page.rb +18 -24
  59. data/fixtures/{test_helper.rb → rails_helper.rb} +3 -7
  60. data/fixtures/standard_element.rb +87 -0
  61. data/fixtures/view_helper.rb +1 -1
  62. data/fixtures/void_element.rb +31 -0
  63. data/lib/generators/phlex/collection/USAGE +8 -0
  64. data/lib/generators/phlex/collection/collection_generator.rb +13 -0
  65. data/lib/generators/phlex/collection/templates/collection.rb.erb +16 -0
  66. data/lib/generators/phlex/controller/USAGE +10 -0
  67. data/lib/generators/phlex/controller/controller_generator.rb +54 -0
  68. data/lib/generators/phlex/controller/templates/controller.rb.erb +10 -0
  69. data/lib/generators/phlex/controller/templates/view.rb.erb +14 -0
  70. data/lib/generators/phlex/layout/USAGE +8 -0
  71. data/lib/generators/phlex/layout/layout_generator.rb +13 -0
  72. data/lib/generators/phlex/layout/templates/layout.rb.erb +31 -0
  73. data/lib/generators/phlex/page/USAGE +8 -0
  74. data/lib/generators/phlex/page/page_generator.rb +13 -0
  75. data/lib/generators/phlex/page/templates/page.rb.erb +13 -0
  76. data/lib/generators/phlex/table/USAGE +8 -0
  77. data/lib/generators/phlex/table/table_generator.rb +14 -0
  78. data/lib/generators/phlex/table/templates/table.rb.erb +11 -0
  79. data/lib/generators/phlex/view/templates/view.rb.erb +7 -1
  80. data/lib/generators/phlex/view/view_generator.rb +9 -1
  81. data/lib/install/phlex.rb +10 -1
  82. data/lib/phlex/block.rb +2 -4
  83. data/lib/phlex/buffered.rb +6 -8
  84. data/lib/phlex/callable.rb +9 -0
  85. data/lib/phlex/collection.rb +33 -0
  86. data/lib/phlex/compiler/elements.rb +49 -0
  87. data/lib/phlex/compiler/generators/content.rb +103 -0
  88. data/lib/phlex/compiler/generators/element.rb +61 -0
  89. data/lib/phlex/compiler/nodes/base.rb +19 -0
  90. data/lib/phlex/compiler/nodes/call.rb +9 -0
  91. data/lib/phlex/compiler/nodes/command.rb +13 -0
  92. data/lib/phlex/compiler/nodes/fcall.rb +18 -0
  93. data/lib/phlex/compiler/nodes/method_add_block.rb +33 -0
  94. data/lib/phlex/compiler/nodes/vcall.rb +9 -0
  95. data/lib/phlex/compiler/optimizer.rb +66 -0
  96. data/lib/phlex/compiler/visitors/base.rb +15 -0
  97. data/lib/phlex/compiler/visitors/file.rb +23 -11
  98. data/lib/phlex/compiler/visitors/stable_scope.rb +28 -0
  99. data/lib/phlex/compiler/visitors/statements.rb +36 -0
  100. data/lib/phlex/compiler/visitors/view.rb +19 -0
  101. data/lib/phlex/compiler/visitors/view_method.rb +59 -0
  102. data/lib/phlex/compiler.rb +23 -3
  103. data/lib/phlex/elements.rb +57 -0
  104. data/lib/phlex/engine.rb +0 -3
  105. data/lib/phlex/helpers.rb +59 -0
  106. data/lib/phlex/html/callbacks.rb +11 -0
  107. data/lib/phlex/html.rb +209 -54
  108. data/lib/phlex/markdown.rb +76 -0
  109. data/lib/phlex/rails/form.rb +67 -0
  110. data/lib/phlex/rails/helpers.rb +118 -0
  111. data/lib/phlex/rails/layout.rb +15 -0
  112. data/lib/phlex/rails.rb +10 -0
  113. data/lib/phlex/renderable.rb +9 -3
  114. data/lib/phlex/table.rb +104 -0
  115. data/lib/phlex/testing/capybara.rb +25 -0
  116. data/lib/phlex/testing/nokogiri.rb +24 -0
  117. data/lib/phlex/testing/rails.rb +19 -0
  118. data/lib/phlex/testing/view_helper.rb +15 -0
  119. data/lib/phlex/translation.rb +23 -0
  120. data/lib/phlex/turbo/frame.rb +21 -0
  121. data/lib/phlex/turbo/stream.rb +18 -0
  122. data/lib/phlex/version.rb +1 -1
  123. data/lib/phlex.rb +22 -24
  124. metadata +112 -15
  125. data/.rspec +0 -1
  126. data/fixtures/compilation/vcall.rb +0 -38
  127. data/lib/phlex/compiler/generators/standard_element.rb +0 -30
  128. data/lib/phlex/compiler/generators/void_element.rb +0 -29
  129. data/lib/phlex/compiler/optimizers/base_optimizer.rb +0 -34
  130. data/lib/phlex/compiler/optimizers/vcall.rb +0 -29
  131. data/lib/phlex/compiler/visitors/base_visitor.rb +0 -19
  132. data/lib/phlex/compiler/visitors/component.rb +0 -28
  133. data/lib/phlex/compiler/visitors/component_method.rb +0 -28
  134. data/lib/phlex/rails/tag_helpers.rb +0 -29
  135. data/lib/phlex/view.rb +0 -223
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Views
4
- class Card < Phlex::View
4
+ class Card < ApplicationView
5
5
  def template(&block)
6
6
  article class: "drop-shadow p-5 rounded", &block
7
7
  end
8
8
 
9
9
  def title(text)
10
- h3 text, class: "font-bold"
10
+ h3 class: "font-bold" do
11
+ text
12
+ end
11
13
  end
12
14
  end
13
15
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Views
4
+ module Comments
5
+ class Comment < ApplicationView
6
+ def initialize(name:, body:)
7
+ @name = name
8
+ @body = body
9
+ end
10
+
11
+ def template(&block)
12
+ div {
13
+ span { @name }
14
+ span { @body }
15
+
16
+ yield_content(&block)
17
+
18
+ render(::ReactionComponent.new(emoji: "hamburger")) do
19
+ p { "Emoji reaction for a comment from #{@name} with body #{@body}" }
20
+ end
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ <%= render(CommentComponent.new(name: "Matz", body: "hey, folks")) do %>
2
+ Hello, World from a ViewComponent!
3
+ <% end %>
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Views
4
+ module Comments
5
+ class Reaction < ApplicationView
6
+ def initialize(emoji:)
7
+ @emoji = emoji
8
+ end
9
+
10
+ def template(&block)
11
+ p { @emoji }
12
+
13
+ yield_content(&block)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ <%= render Views::Comments::Comment.new(name: "_why", body: "I'm back") do %>
2
+ Hello, World from a Phlex Component!
3
+ <% end %>
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Views
4
- class Heading < Phlex::View
4
+ class Heading < ApplicationView
5
5
  def template(&block)
6
6
  h1(&block)
7
7
  end
data/fixtures/layout.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Example
4
- class LayoutComponent < Phlex::View
4
+ class LayoutComponent < Phlex::HTML
5
5
  def initialize(title: "Example")
6
6
  @title = title
7
7
  end
@@ -9,7 +9,7 @@ module Example
9
9
  def template(&block)
10
10
  html do
11
11
  head do
12
- title @title
12
+ title { @title }
13
13
  meta name: "viewport", content: "width=device-width,initial-scale=1"
14
14
  link href: "/assets/tailwind.css", rel: "stylesheet"
15
15
  end
@@ -17,9 +17,9 @@ module Example
17
17
  body class: "bg-zinc-100" do
18
18
  nav class: "p-5", id: "main_nav" do
19
19
  ul do
20
- li(class: "p-5") { a "Home", href: "/" }
21
- li(class: "p-5") { a "About", href: "/about" }
22
- li(class: "p-5") { a "Contact", href: "/contact" }
20
+ li(class: "p-5") { a(href: "/") { "Home" } }
21
+ li(class: "p-5") { a(href: "/about") { "About" } }
22
+ li(class: "p-5") { a(href: "/contact") { "Contact" } }
23
23
  end
24
24
  end
25
25
 
data/fixtures/page.rb CHANGED
@@ -1,37 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Example
4
- class Page < Phlex::View
4
+ class Page < Phlex::HTML
5
5
  def template
6
6
  render LayoutComponent.new do
7
- h1 "Hi"
7
+ h1 { "Hi" }
8
8
 
9
- 5.times do
10
- div do
11
- 10.times do
12
- a "Test", href: "something", unique: SecureRandom.uuid, data: { value: 1 }
9
+ table id: "test", class: "a b c d e f g" do
10
+ tr do
11
+ td id: "test", class: "a b c d e f g" do
12
+ span { "Hi" }
13
13
  end
14
- end
15
- end
16
14
 
17
- table do
18
- thead do
19
- 10.times do
20
- tr do
21
- th "Hi"
22
- end
15
+ td id: "test", class: "a b c d e f g" do
16
+ span { "Hi" }
17
+ end
18
+
19
+ td id: "test", class: "a b c d e f g" do
20
+ span { "Hi" }
21
+ end
22
+
23
+ td id: "test", class: "a b c d e f g" do
24
+ span { "Hi" }
23
25
  end
24
- end
25
26
 
26
- tbody do
27
- 100.times do
28
- tr class: "a b c d e f g", id: "something" do
29
- 10.times do
30
- td class: "f g h i j k l" do
31
- span "Test"
32
- end
33
- end
34
- end
27
+ td id: "test", class: "a b c d e f g" do
28
+ span { "Hi" }
35
29
  end
36
30
  end
37
31
  end
@@ -1,15 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "phlex"
4
- require "bundler"
3
+ require "phlex/rails"
5
4
 
6
- Bundler.require :test
5
+ Bundler.require :rails
7
6
 
8
7
  Combustion.path = "fixtures/dummy"
8
+
9
9
  Combustion.initialize! :action_controller do
10
10
  config.autoload_paths << "#{root}/app"
11
11
  end
12
-
13
- require "view_helper"
14
-
15
- Zeitwerk::Loader.eager_load_all
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fixtures
4
+ module StandardElement
5
+ class WithParens < Phlex::HTML
6
+ def template
7
+ h1()
8
+ end
9
+ end
10
+
11
+ class WithoutParens < Phlex::HTML
12
+ def template
13
+ h1
14
+ end
15
+ end
16
+
17
+ module WithAttributes
18
+ class WithParens < Phlex::HTML
19
+ def template
20
+ h1(class: "font-bold")
21
+ end
22
+ end
23
+
24
+ class WithoutParens < Phlex::HTML
25
+ def template
26
+ h1 class: "font-bold"
27
+ end
28
+ end
29
+ end
30
+
31
+ module WithBraceBlock
32
+ class WithParens < Phlex::HTML
33
+ def template
34
+ h1() { "Hi" }
35
+ end
36
+ end
37
+
38
+ class WithoutParens < Phlex::HTML
39
+ def template
40
+ h1 { "Hi" }
41
+ end
42
+ end
43
+
44
+ class WithAttributes < Phlex::HTML
45
+ def template
46
+ h1(class: "font-bold") { "Hi" }
47
+ end
48
+ end
49
+ end
50
+
51
+ module WithDoBlock
52
+ class WithParens < Phlex::HTML
53
+ def template
54
+ h1() do
55
+ "Hi"
56
+ end
57
+ end
58
+ end
59
+
60
+ class WithoutParens < Phlex::HTML
61
+ def template
62
+ h1 do
63
+ "Hi"
64
+ end
65
+ end
66
+ end
67
+
68
+ module WithAttributes
69
+ class WithParens < Phlex::HTML
70
+ def template
71
+ h1(class: "font-bold") do
72
+ "Hi"
73
+ end
74
+ end
75
+ end
76
+
77
+ class WithoutParens < Phlex::HTML
78
+ def template
79
+ h1 class: "font-bold" do
80
+ "Hi"
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -10,7 +10,7 @@ module ViewHelper
10
10
 
11
11
  def view(&block)
12
12
  let :view do
13
- Class.new(Phlex::View, &block)
13
+ Class.new(Phlex::HTML, &block)
14
14
  end
15
15
  end
16
16
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fixtures
4
+ module VoidElement
5
+ class WithParens < Phlex::HTML
6
+ def template
7
+ img()
8
+ end
9
+ end
10
+
11
+ class WithoutParens < Phlex::HTML
12
+ def template
13
+ img
14
+ end
15
+ end
16
+
17
+ module WithAttributes
18
+ class WithParens < Phlex::HTML
19
+ def template
20
+ img(class: "a b c")
21
+ end
22
+ end
23
+
24
+ class WithoutParens < Phlex::HTML
25
+ def template
26
+ img class: "a b c"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates a Phlex collection view with the given name
3
+
4
+ Example:
5
+ rails generate phlex:collection Articles::List
6
+
7
+ This will create:
8
+ app/views/articles/list.rb
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Generators
5
+ class CollectionGenerator < ::Rails::Generators::NamedBase
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ def create_view
9
+ template "collection.rb.erb", File.join("app/views", class_path, "#{file_name}.rb")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ <% module_namespacing do -%>
2
+ module Views
3
+ class <%= class_name %> < Phlex::HTML
4
+ include ApplicationView
5
+ include Phlex::Collection
6
+
7
+ def collection_template(&)
8
+ ul(&)
9
+ end
10
+
11
+ def item_template
12
+ li { @item }
13
+ end
14
+ end
15
+ end
16
+ <% end %>
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Generates a Rails controller with Phlex views for the supplied actions
3
+
4
+ Example:
5
+ rails generate phlex:controller Articles index show
6
+
7
+ This will create:
8
+ app/controllers/articles_controller.rb
9
+ app/views/articles/index.rb
10
+ app/views/articles/show.rb
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Generators
5
+ class ControllerGenerator < ::Rails::Generators::NamedBase # :nodoc:
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ argument :actions, type: :array, default: [], banner: "action action"
9
+ class_option :skip_routes, type: :boolean, desc: "Don't add routes to config/routes.rb."
10
+ class_option :parent, type: :string, default: "ApplicationController", desc: "The parent class for the generated controller"
11
+
12
+ check_class_collision suffix: "Controller"
13
+
14
+ def create_controller_files
15
+ template "controller.rb.erb", File.join("app/controllers", class_path, "#{file_name}_controller.rb")
16
+ end
17
+
18
+ def copy_view_files
19
+ base_path = File.join("app/views", class_path, file_name)
20
+ empty_directory base_path
21
+
22
+ actions.each do |action|
23
+ Rails::Generators.invoke("phlex:view", [remove_possible_suffix(name) + "/" + action])
24
+ end
25
+ end
26
+
27
+ def add_routes
28
+ return if options[:skip_routes]
29
+ return if actions.empty?
30
+
31
+ routing_code = actions.map { |action| "get '#{file_name}/#{action}'" }.join("\n")
32
+ route routing_code, namespace: regular_class_path
33
+ end
34
+
35
+ hook_for :test_framework, as: :controller do |generator|
36
+ invoke generator, [remove_possible_suffix(name), actions]
37
+ end
38
+
39
+ private
40
+
41
+ def parent_class_name
42
+ options[:parent]
43
+ end
44
+
45
+ def file_name
46
+ @_file_name ||= remove_possible_suffix(super)
47
+ end
48
+
49
+ def remove_possible_suffix(name)
50
+ name.sub(/_?controller$/i, "")
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,10 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Controller < <%= parent_class_name.classify %>
3
+ <% actions.each do |action| -%>
4
+ def <%= action %>
5
+ render Views::<%= class_name %>::<%= action.camelize %>.new
6
+ end
7
+ <%= "\n" unless action == actions.last -%>
8
+ <% end -%>
9
+ end
10
+ <% end -%>
@@ -0,0 +1,14 @@
1
+ <% module_namespacing do -%>
2
+ module Views
3
+ class <%= class_name %>::<%= @action.camelize %> < Phlex::HTML
4
+ include ApplicationView
5
+
6
+ def template
7
+ <%= "# " unless @has_layout %>render Layout.new(title: "<%= class_name.gsub("::", " ").titlecase %> - <%= @action.titlecase %>") do
8
+ h1 { "<%= class_name %>#<%= @action %>" }
9
+ p { "Find me in <%= @path %>" }
10
+ <%= "# " unless @has_layout %>end
11
+ end
12
+ end
13
+ end
14
+ <% end %>
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates a Phlex layout view with the given name
3
+
4
+ Example:
5
+ rails generate phlex:layout Layout
6
+
7
+ This will create:
8
+ app/views/layout.rb
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Generators
5
+ class LayoutGenerator < ::Rails::Generators::NamedBase
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ def create_view
9
+ template "layout.rb.erb", File.join("app/views", class_path, "#{file_name}.rb")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ <% module_namespacing do -%>
2
+ module Views
3
+ class <%= class_name %> < Phlex::HTML
4
+ include ApplicationView
5
+ include Phlex::Rails::Layout
6
+
7
+ def initialize(title:)
8
+ @title = title
9
+ end
10
+
11
+ def template(&)
12
+ doctype
13
+
14
+ html do
15
+ head do
16
+ meta charset: "utf-8"
17
+ csp_meta_tag
18
+ csrf_meta_tags
19
+ meta name: "viewport", content: "width=device-width,initial-scale=1"
20
+ title { @title }
21
+ stylesheet_link_tag "application"
22
+ end
23
+
24
+ body do
25
+ main(&)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ <% end %>
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates a Phlex page view with the given name
3
+
4
+ Example:
5
+ rails generate phlex:page Articles::Index
6
+
7
+ This will create:
8
+ app/views/articles/index.rb
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Generators
5
+ class PageGenerator < ::Rails::Generators::NamedBase
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ def create_view
9
+ template "page.rb.erb", File.join("app/views", class_path, "#{file_name}.rb")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ <% module_namespacing do -%>
2
+ module Views
3
+ class <%= class_name %> < Phlex::HTML
4
+ include ApplicationView
5
+
6
+ def template
7
+ render Layout.new(title: "<%= class_name.gsub("::", " ") %>") do
8
+ h1 { "👋 Hello World!" }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ <% end %>
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates a Phlex table collection view with the given name
3
+
4
+ Example:
5
+ rails generate phlex:collection Articles::Table --properties title author created_at
6
+
7
+ This will create:
8
+ app/views/articles/table.rb
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Generators
5
+ class TableGenerator < ::Rails::Generators::NamedBase
6
+ source_root File.expand_path("templates", __dir__)
7
+ class_option :properties, type: :array, default: []
8
+
9
+ def create_view
10
+ template "table.rb.erb", File.join("app/views", class_path, "#{file_name}.rb")
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ <% module_namespacing do -%>
2
+ module Views
3
+ class <%= class_name %> < Phlex::HTML
4
+ include ApplicationView
5
+ include Phlex::Table
6
+
7
+ <% options["properties"].each do |property| %>
8
+ property "<%= property.humanize %>", &:<%= property.underscore %><% end %>
9
+ end
10
+ end
11
+ <% end %>
@@ -1,7 +1,13 @@
1
1
  <% module_namespacing do -%>
2
2
  module Views
3
- class <%= class_name %> < ApplicationView
3
+ class <%= class_name %> < Phlex::HTML
4
+ include ApplicationView
5
+
4
6
  def template
7
+ <%= "# " unless @layout %>render Layout.new(title: "<%= class_name.gsub("::", " ").titlecase %>") do
8
+ h1 { "<%= class_name %>" }
9
+ p { "Find me in <%= @path %>" }
10
+ <%= "# " unless @layout %>end
5
11
  end
6
12
  end
7
13
  end
@@ -6,7 +6,15 @@ module Phlex
6
6
  source_root File.expand_path("templates", __dir__)
7
7
 
8
8
  def create_view
9
- template "view.rb.erb", File.join("app/views", class_path, "#{file_name}.rb")
9
+ @layout = layout
10
+ @path = File.join("app/views", class_path, "#{file_name}.rb")
11
+ template "view.rb.erb", @path
12
+ end
13
+
14
+ private
15
+
16
+ def layout
17
+ Rails.root.join("app/views/layout.rb").exist?
10
18
  end
11
19
  end
12
20
  end
data/lib/install/phlex.rb CHANGED
@@ -20,11 +20,20 @@ unless Rails.root.join("app/views/application_view.rb").exist?
20
20
  # frozen_string_literal: true
21
21
 
22
22
  module Views
23
- class ApplicationView < Phlex::View
23
+ module ApplicationView
24
24
  include Rails.application.routes.url_helpers
25
+ include Phlex::Translation
25
26
  end
26
27
  end
27
28
  RUBY
28
29
  end
29
30
 
31
+ tailwind_config_path = Rails.root.join("config/tailwind.config.js")
32
+
33
+ if tailwind_config_path.exist?
34
+ insert_into_file tailwind_config_path, after: "content: [" do
35
+ "\n './app/views/**/*.rb',"
36
+ end
37
+ end
38
+
30
39
  say "Phlex successfully installed!"
data/lib/phlex/block.rb CHANGED
@@ -2,15 +2,13 @@
2
2
 
3
3
  module Phlex
4
4
  class Block
5
+ include Callable
6
+
5
7
  def initialize(context, &block)
6
8
  @context = context
7
9
  @block = block
8
10
  end
9
11
 
10
- def to_proc
11
- method(:call).to_proc
12
- end
13
-
14
12
  def call(*args, **kwargs)
15
13
  @context.instance_exec(*args, **kwargs, &@block)
16
14
  end