phlex 0.5.3 → 1.0.0.rc1

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 (134) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -20
  3. data/config/sus.rb +0 -5
  4. data/lib/phlex/buffered.rb +2 -0
  5. data/lib/phlex/collection.rb +2 -0
  6. data/lib/phlex/elements.rb +1 -5
  7. data/lib/phlex/experimental.rb +10 -0
  8. data/lib/phlex/helpers.rb +2 -0
  9. data/lib/phlex/html.rb +73 -86
  10. data/lib/phlex/table.rb +1 -0
  11. data/lib/phlex/turbo/frame.rb +2 -0
  12. data/lib/phlex/turbo/stream.rb +2 -0
  13. data/lib/phlex/version.rb +1 -1
  14. data/lib/phlex.rb +2 -19
  15. metadata +6 -166
  16. data/Procfile.dev +0 -3
  17. data/Rakefile +0 -8
  18. data/config.ru +0 -9
  19. data/docs/assets/application.css +0 -32
  20. data/docs/assets/logo.png +0 -0
  21. data/docs/build.rb +0 -30
  22. data/docs/components/callout.rb +0 -9
  23. data/docs/components/code_block.rb +0 -26
  24. data/docs/components/code_span.rb +0 -9
  25. data/docs/components/example.rb +0 -32
  26. data/docs/components/heading.rb +0 -9
  27. data/docs/components/layout.rb +0 -91
  28. data/docs/components/markdown.rb +0 -25
  29. data/docs/components/nav/item.rb +0 -33
  30. data/docs/components/nav.rb +0 -6
  31. data/docs/components/tabs/tab.rb +0 -28
  32. data/docs/components/tabs.rb +0 -30
  33. data/docs/components/title.rb +0 -9
  34. data/docs/page_builder.rb +0 -39
  35. data/docs/pages/application_page.rb +0 -7
  36. data/docs/pages/helpers.rb +0 -97
  37. data/docs/pages/index.rb +0 -27
  38. data/docs/pages/library/collections.rb +0 -83
  39. data/docs/pages/rails/getting_started.rb +0 -57
  40. data/docs/pages/rails/helpers.rb +0 -55
  41. data/docs/pages/rails/layouts.rb +0 -61
  42. data/docs/pages/rails/migrating.rb +0 -37
  43. data/docs/pages/rails/rendering_views.rb +0 -35
  44. data/docs/pages/rails_integration.rb +0 -58
  45. data/docs/pages/templates.rb +0 -144
  46. data/docs/pages/testing/capybara.rb +0 -48
  47. data/docs/pages/testing/getting_started.rb +0 -44
  48. data/docs/pages/testing/nokogiri.rb +0 -83
  49. data/docs/pages/testing/rails.rb +0 -17
  50. data/docs/pages/translations.rb +0 -81
  51. data/docs/pages/views.rb +0 -184
  52. data/fixtures/compiler_test_helpers.rb +0 -19
  53. data/fixtures/content.rb +0 -60
  54. data/fixtures/dummy/app/assets/config/manifest.js +0 -0
  55. data/fixtures/dummy/app/components/comment_component.html.erb +0 -14
  56. data/fixtures/dummy/app/components/comment_component.rb +0 -8
  57. data/fixtures/dummy/app/components/reaction_component.html.erb +0 -3
  58. data/fixtures/dummy/app/components/reaction_component.rb +0 -7
  59. data/fixtures/dummy/app/controllers/articles_controller.rb +0 -4
  60. data/fixtures/dummy/app/controllers/comments_controller.rb +0 -4
  61. data/fixtures/dummy/app/views/application_view.rb +0 -8
  62. data/fixtures/dummy/app/views/articles/form.rb +0 -15
  63. data/fixtures/dummy/app/views/articles/index.html.erb +0 -14
  64. data/fixtures/dummy/app/views/articles/new.html.erb +0 -1
  65. data/fixtures/dummy/app/views/card.rb +0 -15
  66. data/fixtures/dummy/app/views/comments/comment.rb +0 -25
  67. data/fixtures/dummy/app/views/comments/index.html.erb +0 -3
  68. data/fixtures/dummy/app/views/comments/reaction.rb +0 -17
  69. data/fixtures/dummy/app/views/comments/show.html.erb +0 -3
  70. data/fixtures/dummy/app/views/heading.rb +0 -9
  71. data/fixtures/dummy/config/database.yml +0 -3
  72. data/fixtures/dummy/config/routes.rb +0 -5
  73. data/fixtures/dummy/config/storage.yml +0 -3
  74. data/fixtures/dummy/db/schema.rb +0 -6
  75. data/fixtures/dummy/log/.gitignore +0 -1
  76. data/fixtures/dummy/public/favicon.ico +0 -0
  77. data/fixtures/rails_helper.rb +0 -11
  78. data/fixtures/standard_element.rb +0 -87
  79. data/fixtures/void_element.rb +0 -31
  80. data/lib/generators/phlex/collection/USAGE +0 -8
  81. data/lib/generators/phlex/collection/collection_generator.rb +0 -13
  82. data/lib/generators/phlex/collection/templates/collection.rb.erb +0 -16
  83. data/lib/generators/phlex/controller/USAGE +0 -10
  84. data/lib/generators/phlex/controller/controller_generator.rb +0 -54
  85. data/lib/generators/phlex/controller/templates/controller.rb.erb +0 -10
  86. data/lib/generators/phlex/controller/templates/view.rb.erb +0 -14
  87. data/lib/generators/phlex/layout/USAGE +0 -8
  88. data/lib/generators/phlex/layout/layout_generator.rb +0 -13
  89. data/lib/generators/phlex/layout/templates/layout.rb.erb +0 -31
  90. data/lib/generators/phlex/page/USAGE +0 -8
  91. data/lib/generators/phlex/page/page_generator.rb +0 -13
  92. data/lib/generators/phlex/page/templates/page.rb.erb +0 -13
  93. data/lib/generators/phlex/table/USAGE +0 -8
  94. data/lib/generators/phlex/table/table_generator.rb +0 -14
  95. data/lib/generators/phlex/table/templates/table.rb.erb +0 -11
  96. data/lib/generators/phlex/view/USAGE +0 -8
  97. data/lib/generators/phlex/view/templates/view.rb.erb +0 -14
  98. data/lib/generators/phlex/view/view_generator.rb +0 -21
  99. data/lib/install/phlex.rb +0 -39
  100. data/lib/phlex/block.rb +0 -16
  101. data/lib/phlex/compiler/elements.rb +0 -49
  102. data/lib/phlex/compiler/formatter.rb +0 -91
  103. data/lib/phlex/compiler/generators/content.rb +0 -103
  104. data/lib/phlex/compiler/generators/element.rb +0 -61
  105. data/lib/phlex/compiler/nodes/base.rb +0 -19
  106. data/lib/phlex/compiler/nodes/call.rb +0 -9
  107. data/lib/phlex/compiler/nodes/command.rb +0 -13
  108. data/lib/phlex/compiler/nodes/fcall.rb +0 -18
  109. data/lib/phlex/compiler/nodes/method_add_block.rb +0 -33
  110. data/lib/phlex/compiler/nodes/vcall.rb +0 -9
  111. data/lib/phlex/compiler/optimizer.rb +0 -66
  112. data/lib/phlex/compiler/visitors/base.rb +0 -15
  113. data/lib/phlex/compiler/visitors/file.rb +0 -29
  114. data/lib/phlex/compiler/visitors/stable_scope.rb +0 -28
  115. data/lib/phlex/compiler/visitors/statements.rb +0 -36
  116. data/lib/phlex/compiler/visitors/view.rb +0 -19
  117. data/lib/phlex/compiler/visitors/view_method.rb +0 -59
  118. data/lib/phlex/compiler.rb +0 -70
  119. data/lib/phlex/html/callbacks.rb +0 -11
  120. data/lib/phlex/markdown.rb +0 -76
  121. data/lib/phlex/rails/engine.rb +0 -10
  122. data/lib/phlex/rails/form.rb +0 -67
  123. data/lib/phlex/rails/helpers.rb +0 -118
  124. data/lib/phlex/rails/layout.rb +0 -15
  125. data/lib/phlex/rails.rb +0 -11
  126. data/lib/phlex/renderable.rb +0 -47
  127. data/lib/phlex/testing/capybara.rb +0 -25
  128. data/lib/phlex/testing/nokogiri.rb +0 -24
  129. data/lib/phlex/testing/rails.rb +0 -19
  130. data/lib/phlex/translation.rb +0 -23
  131. data/lib/tasks/phlex_tasks.rake +0 -11
  132. data/package-lock.json +0 -1195
  133. data/package.json +0 -5
  134. data/tailwind.config.js +0 -7
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pages
4
- module Rails
5
- class GettingStarted < ApplicationPage
6
- def template
7
- render Layout.new(title: "Getting started with Rails") do
8
- render Markdown.new <<~MD
9
- # Getting started with Phlex on Rails
10
-
11
- While Phlex can be used in any Ruby project, it's especially great with [Rails](https://rubyonrails.org). But before we get into the details, it's important to understand that Phlex is very different from [ActionView](https://guides.rubyonrails.org/action_view_overview.html) and [ViewComponent](https://viewcomponent.org).
12
-
13
- In Phlex, _layouts_, _pages_ and _components_ (or "partials") are the same thing. Phlex Views are Ruby objects that represent every piece of your app's user interface, from pages and layouts and nav-bars, to headings and buttons and links. They're not templates like ERB files in ActionView / ViewComponent; they are just Ruby objects.
14
-
15
- It might feel a bit weird at first, but you'll soon realise how weird it was writing procedural templates in ERB while every other part of your app was object-oriented Ruby.
16
-
17
- ## Setup
18
-
19
- If you haven't installed Phlex already, you'll need to add it to your Gemfile. This is a good place to require `phlex/rails`.
20
-
21
- ```ruby
22
- gem "phlex", require: "phlex/rails"
23
- ```
24
-
25
- Now run `bundle install`. Once that's finished, you'll want to run the setup script: `bin/rails phlex:install`.
26
-
27
- This script will:
28
-
29
- 1. update `config/application.rb` to include `/app` in your auto-load paths;
30
- 2. generate `views/application_view.rb`.
31
-
32
- Like `ApplicationRecord`, `ApplicationView` is your base view which all your other views should inherit from.
33
-
34
- ## Naming conventions
35
-
36
- We recommend using the Zeitwerk conventions for naming. For example, your Articles index page, would be called `Views::Articles::Index` and live in `app/views/articles/index.rb`.
37
-
38
- ## View generator
39
-
40
- You can generate new views with the `rails g phlex:view` command.
41
-
42
- For example, running `rails g phlex:view Articles::Index` will create `app/views/articles/index.rb` with the following:
43
-
44
- ```ruby
45
- module Views
46
- class Articles::Index < ApplicationView
47
- def template
48
- end
49
- end
50
- end
51
- ```
52
- MD
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "phlex/rails"
4
-
5
- module Pages
6
- module Rails
7
- class Helpers < ApplicationPage
8
- def template
9
- render Layout.new(title: "Using Rails helpers in Phlex views") do
10
- render Markdown.new <<~MD
11
- # Using Rails helpers in Phlex views
12
-
13
- ## The `helpers` proxy
14
-
15
- You can use the `helpers` proxy to access any Rails helper from a Phlex view. For example, you can use the `#t` helper for translations:
16
-
17
- ```ruby
18
- module Views
19
- class Hello < ApplicationView
20
- delegate :t, to: :helpers
21
-
22
- def template
23
- h1 do
24
- t "hello"
25
- end
26
- end
27
- end
28
- end
29
- ```
30
-
31
- ## Layout helpers
32
-
33
- Rails tag helpers return strings which makes them less than ideal to use from Phlex. Including `Phlex::Rails::Layout` gives you access to the following Rails helper proxies which immediately output to the buffer:
34
-
35
- #{(Phlex::Rails::Layout.instance_methods - Module.methods).sort.map { "1. `#{_1}`" }.join("\n")}
36
-
37
- Using these is equvalent to passing the output of the original Rails helpers to `raw`, e.g:
38
-
39
- ```ruby
40
- unsafe_raw helpers.javascript_include_tag
41
- ```
42
-
43
- ## Including proxies
44
-
45
- The following modules can be included for direct access to these helpers:
46
-
47
- #{Phlex::Rails::Helpers.constants.sort.map { "1. `Phlex::Rails::Helpers::#{_1}`" }.join("\n")}
48
-
49
- Note, helpers that produce HTML are adapted to output to the buffer directly.
50
- MD
51
- end
52
- end
53
- end
54
- end
55
- end
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pages
4
- module Rails
5
- class Layouts < ApplicationPage
6
- def template
7
- render Layout.new(title: "Getting started with Rails") do
8
- render Markdown.new <<~MD
9
- # Layouts in Rails
10
-
11
- Rails has an implicit layouts feature that automatically wraps views in a layout template, usually `views/layouts/application.html.erb`. When using Phlex, you'll probably want to by-pass this feature completely. Here's why:
12
-
13
- Sometimes you need to pass some argument to the layout, such as a page title that needs to be rendered in a `<title>` tag in the HTML `<head>`. Rails lets you do this from another part of the view using the `content_for` feature, but this pattern precludes defining an explicit interface for your layout through its initializer.
14
-
15
- If layouts are Phlex views, they can be rendered just like any other view and can require that `title` argument from their initializer. The trick is the page view renders its content into the layout view.
16
- MD
17
-
18
- render Example.new do |e|
19
- e.tab "layout.rb", <<~RUBY
20
- module Views
21
- class Layout < Phlex::HTML
22
- def initialize(title:)
23
- @title = title
24
- end
25
-
26
- def template(&)
27
- html do
28
- head do
29
- title { @title }
30
- end
31
-
32
- body(&)
33
- end
34
- end
35
- end
36
- end
37
- RUBY
38
-
39
- e.tab "index.rb", <<~RUBY
40
- module Views
41
- class Index < Phlex::HTML
42
- def template
43
- render Layout.new(title: "Hello") do
44
- h1 { "👋" }
45
- end
46
- end
47
- end
48
- end
49
- RUBY
50
-
51
- e.execute "Views::Index.new.call"
52
- end
53
-
54
- render Markdown.new <<~MD
55
- If you're using a Phlex view as a layout, you'll want to disable layouts from your Rails controller. You can do this by adding `layout false` to your controller. In a new app, you'll probably want to add this to the `ApplicationController`.
56
- MD
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pages
4
- class Rails::Migrating < ApplicationPage
5
- def template
6
- render Layout.new(title: "Migrating to Phlex in Rails") do
7
- render Markdown.new <<~MD
8
- # Migrating an existing Rails app to Phlex
9
-
10
- Whether you currently use ActionView or ViewComponent with ERB, HAML or Slim, you can start using Phlex in your Rails app today without a big rewrite.
11
-
12
- ## You can render Phlex views into existing templates
13
-
14
- Phlex views implement the _renderable_ interface for Rails, which means they can be rendered from a controller or another view template — even ViewComponent templates. This means you can gradually migrate specific pages and components to Phlex without having to change anything else.
15
-
16
- ## You can render ActionView partials and ViewComponent components in Phlex views
17
-
18
- That's right, the `render` method in Phlex doesn't only work with Phlex views. You can use it to render ActionView partials and ViewComponent components too.
19
-
20
- Say you have an `articles/index.html.erb` template that renders a list of articles using the `articles/_article.html.erb` partial. You can convert the index template to an `Articles::Index` Phlex view while continuing to render the same ActionView partial inside it.
21
-
22
- ## Use an ERB → Phlex converter
23
-
24
- The ERB → Phlex converter, [Phlexing](https://www.phlexing.fun), can do the heavy-lifting but it won't help you architect your components / design system. Take your time, converting things piece-by-piece.
25
-
26
- If you're using ViewComponent, you might find you can convert components to Phlex views without even changing any call-sites.
27
-
28
- ## Save the layout 'til last
29
-
30
- After everything I said about layouts in the previous section, I'll let you in on a little secret: Phlex actually does support `content_for` in one direction. You can't yield a `content_for` block in Phlex, but you can assign one.
31
-
32
- If you're working on an established Rails app, the layout is probably the last thing you should convert. Just include `Phlex::Rails::Helpers::ContentFor` in your `ApplicationView` and you'll be able to render Phlex views into existing ActionView layouts and assign `content_for` blocks too.
33
- MD
34
- end
35
- end
36
- end
37
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pages
4
- class Rails::RenderingViews < ApplicationPage
5
- def template
6
- render Layout.new(title: "Getting started with Rails") do
7
- render Markdown.new <<~MD
8
- # Rendering Phlex views in Rails
9
-
10
- You can render a `Phlex::HTML` from your Rails controller actions as well as other views, and even from ActionView / ViewComponent templates.
11
-
12
- Instead of implicitly rendering an ERB template with automatic access to all your controller instance variables, you need to explicitly render Phlex views from your controller action methods.
13
-
14
- Doing this allows you to design views without implicit dependencies on controller instance variables, making them much easier to test and reuse and reason about.
15
-
16
- ```ruby
17
- class ArticlesController < ApplicationController
18
- def index
19
- render Views::Articles::Index.new(
20
- articles: Article.all.load_async
21
- )
22
- end
23
-
24
- def show
25
- render Views::Articles::Show.new(
26
- article: Article.find(params[:id])
27
- )
28
- end
29
- end
30
- ```
31
- MD
32
- end
33
- end
34
- end
35
- end
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pages
4
- class RailsIntegration < ApplicationPage
5
- def template
6
- render Layout.new(title: "Ruby on Rails integration") do
7
- render Markdown.new(<<~MD)
8
- # Ruby on Rails integration
9
-
10
- ## Installation
11
-
12
- To install Phlex into your Rails application, you can run the `bin/rails phlex:install` command.
13
-
14
- ## View generator
15
-
16
- You can generate new views with the `rails g phlex:view` command.
17
-
18
- For example, running `rails g phlex:view Card` will create the following file:
19
- MD
20
-
21
- render CodeBlock.new(<<~RUBY, syntax: :ruby)
22
- # app/views/card.rb
23
-
24
- module Views
25
- class Card < ApplicationView
26
- def template
27
- end
28
- end
29
- end
30
- RUBY
31
-
32
- render Markdown.new(<<~MD)
33
- ## Helpers
34
-
35
- You can use the `helpers` proxy to access helpers within your views.
36
-
37
- For example, you can use the `#t` helper for translations:
38
- MD
39
-
40
- render CodeBlock.new(<<~RUBY, syntax: :ruby)
41
- # app/views/hello.rb
42
-
43
- module Views
44
- class Hello < ApplicationView
45
- delegate :t, to: :helpers
46
-
47
- def template
48
- h1 do
49
- t "hello"
50
- end
51
- end
52
- end
53
- end
54
- RUBY
55
- end
56
- end
57
- end
58
- end
@@ -1,144 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pages
4
- class Templates < ApplicationPage
5
- def template
6
- render Layout.new(title: "Templates in Phlex") do
7
- render Markdown.new(<<~MD)
8
- # Working with templates
9
-
10
- In Phlex, templates are just methods that call other methods that add things to the output buffer. When you call the method `h1`, an `<h1>` tag is buffered for output.
11
-
12
- ## Tag attributes
13
-
14
- You can add attributes to HTML tags by passing keyword arguments to the tag methods.
15
- MD
16
-
17
- render Example.new do |e|
18
- e.tab "hello.rb", <<~RUBY
19
- class Hello < Phlex::HTML
20
- def template
21
- h1(class: "text-xl font-bold") { "👋 Hello World!" }
22
- end
23
- end
24
- RUBY
25
-
26
- e.execute "Hello.new.call"
27
- end
28
-
29
- render Markdown.new(<<~MD)
30
- ## Hash attributes
31
-
32
- If you pass a `Hash` as an attribute value, the hash will be flattened with a dash between each section.
33
- MD
34
-
35
- render Example.new do |e|
36
- e.tab "hello.rb", <<~RUBY
37
- class Hello < Phlex::HTML
38
- def template
39
- div(data: { controller: "hello" }) do
40
- # ...
41
- end
42
- end
43
- end
44
- RUBY
45
-
46
- e.execute "Hello.new.call"
47
- end
48
-
49
- render Markdown.new(<<~MD)
50
- ## Boolean attributes
51
-
52
- When `true`, the attribute will be rendered without a value; when _falsy_, the attribute isn’t rendered at all. You can still use the strings `"true"` and `"false"` as values for non-boolean attributes.
53
- MD
54
-
55
- render Example.new do |e|
56
- e.tab "channel_controls.rb", <<~RUBY
57
- class ChannelControls < Phlex::HTML
58
- def template
59
- input(
60
- value: "1",
61
- name: "channel",
62
- type: "radio",
63
- checked: true
64
- )
65
-
66
- input(
67
- value: "2",
68
- name: "channel",
69
- type: "radio",
70
- checked: false
71
- )
72
- end
73
- end
74
- RUBY
75
-
76
- e.execute "ChannelControls.new.call"
77
- end
78
-
79
- render Markdown.new(<<~MD)
80
- ## The template tag
81
-
82
- Because the `template` method is used to define the view template itself, you'll need to use the method `template_tag` if you want to to render an HTML `<template>` tag.
83
- MD
84
-
85
- render Example.new do |e|
86
- e.tab "example.rb", <<~RUBY
87
- class Example < Phlex::HTML
88
- def template
89
- template_tag do
90
- img src: "hidden.jpg", alt: "A hidden image."
91
- end
92
- end
93
- end
94
- RUBY
95
-
96
- e.execute "Example.new.call"
97
- end
98
-
99
- render Markdown.new(<<~MD)
100
- ## Stand-alone text
101
-
102
- You can output text content without wrapping it in an element by using the `text` helper method.
103
- MD
104
-
105
- render Example.new do |e|
106
- e.tab "heading.rb", <<~RUBY
107
- class Heading < Phlex::HTML
108
- def template
109
- h1 do
110
- strong { "Hello " }
111
- text "World!"
112
- end
113
- end
114
- end
115
- RUBY
116
-
117
- e.execute "Heading.new.call"
118
- end
119
-
120
- render Markdown.new(<<~MD)
121
- ## Whitespace
122
-
123
- The example output on this site is formatted for readability, but there is usually no whitespace between HTML tags in the output. If you need to add some whitespace, you can use the `whitespace` helper. This is useful for adding space between _inline_ elements to allow them to wrap.
124
- MD
125
-
126
- render Example.new do |e|
127
- e.tab "links.rb", <<~RUBY
128
- class Links < Phlex::HTML
129
- def template
130
- a(href: "/") { "Home" }
131
- whitespace
132
- a(href: "/about") { "About" }
133
- whitespace
134
- a(href: "/contact") { "Contact" }
135
- end
136
- end
137
- RUBY
138
-
139
- e.execute "Links.new.call"
140
- end
141
- end
142
- end
143
- end
144
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "minitest"
4
-
5
- module Pages
6
- module Testing
7
- class Capybara < ApplicationPage
8
- def template
9
- render Layout.new(title: "Testing with Capybara") do
10
- render Markdown.new(<<~MD)
11
- # Testing with Capybara
12
-
13
- Require `phlex/testing/capybara` and include `Phlex::Testing::Capybara::ViewHelper` to use [Capybara](http://teamcapybara.github.io/capybara/) matchers.
14
-
15
- The `render` method will return a `Capybara::Node::Simple` and set the `page` attribute to the result.
16
- MD
17
-
18
- render Example.new do |e|
19
- e.tab "test.rb", <<~RUBY
20
- require "phlex/testing/capybara"
21
-
22
- class TestExample < Minitest::Test
23
- include Phlex::Testing::Capybara::ViewHelper
24
-
25
- def test_example
26
- render Example.new("Joel")
27
- assert_select "h1", text: "Hello Joel"
28
- end
29
- end
30
- RUBY
31
-
32
- e.tab "hello.rb", <<~RUBY
33
- class Hello < Phlex::HTML
34
- def initialize(name)
35
- @name = name
36
- end
37
-
38
- def template
39
- h1 { "Hello \#{@name}" }
40
- end
41
- end
42
- RUBY
43
- end
44
- end
45
- end
46
- end
47
- end
48
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pages
4
- module Testing
5
- class GettingStarted < ApplicationPage
6
- def template
7
- render Layout.new(title: "Getting Started Testing Phlex Views") do
8
- render Markdown.new(<<~MD)
9
- # Getting Started
10
-
11
- The `Phlex::Testing::ViewHelper` module defines `render` allowing you to render Phlex views directly in your tests and make assertions against the output.
12
- MD
13
-
14
- render Example.new do |e|
15
- e.tab "test/test_hello.rb", <<~RUBY
16
- require "phlex/testing/view_helper"
17
-
18
- class TestHello < Minitest::Test
19
- include Phlex::Testing::ViewHelper
20
-
21
- def test_hello_output_includes_name
22
- output = render Hello.new("Joel")
23
- assert_equal "<h1>Hello Joel</h1>", output
24
- end
25
- end
26
- RUBY
27
-
28
- e.tab "hello.rb", <<~RUBY
29
- class Hello < Phlex::HTML
30
- def initialize(name)
31
- @name = name
32
- end
33
-
34
- def template
35
- h1 { "Hello \#{@name}" }
36
- end
37
- end
38
- RUBY
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pages
4
- module Testing
5
- class Nokogiri < ApplicationPage
6
- def template
7
- render Layout.new(title: "Testing with Nokogiri") do
8
- render Markdown.new(<<~MD)
9
- # Testing with Nokogiri
10
-
11
- Phlex includes test helpers for working with rendered views as [Nokogiri](https://nokogiri.org) documents and fragments.
12
-
13
- Nokogiri is not a dependency of Phlex, so you’ll need to install that separately to use this helper.
14
-
15
- ## Documents
16
-
17
- If your view represents a whole HTML document, you can require `phlex/testing/nokogiri` and include the `Phlex::Testing::Nokogiri::DocumentHelper` module to render your view as `Nokogiri::Document` using the `render` method.
18
- MD
19
-
20
- render Example.new do |e|
21
- e.tab "test.rb", <<~RUBY
22
- require "phlex/testing/nokogiri"
23
-
24
- class TestExample < Minitest::Test
25
- include Phlex::Testing::Nokogiri::DocumentHelper
26
-
27
- def test_example
28
- output = render Example.new
29
- assert_equal "Hello Joel", output.css("h1").text
30
- end
31
- end
32
- RUBY
33
-
34
- e.tab "hello.rb", <<~RUBY
35
- class Hello < Phlex::HTML
36
- def initialize(name)
37
- @name = name
38
- end
39
-
40
- def template
41
- h1 { "Hello \#{@name}" }
42
- end
43
- end
44
- RUBY
45
- end
46
-
47
- render Markdown.new(<<~MD)
48
- ## Fragments
49
-
50
- If your view represents a fragment (partial), you can require `phlex/testing/nokogiri` and include the `Phlex::Testing::Nokogiri::FragmentHelper` module to render your view as `Nokogiri::Fragment` with the `render` method.
51
- MD
52
-
53
- render Example.new do |e|
54
- e.tab "test.rb", <<~RUBY
55
- require "phlex/testing/nokogiri"
56
-
57
- class TestExample < Minitest::Test
58
- include Phlex::Testing::Nokogiri::FragmentHelper
59
-
60
- def test_example
61
- output = render Example.new("Joel")
62
- assert_equal "Hello Joel", output.css("h1").text
63
- end
64
- end
65
- RUBY
66
-
67
- e.tab "hello.rb", <<~RUBY
68
- class Hello < Phlex::HTML
69
- def initialize(name)
70
- @name = name
71
- end
72
-
73
- def template
74
- h1 { "Hello \#{@name}" }
75
- end
76
- end
77
- RUBY
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pages
4
- module Testing
5
- class Rails < ApplicationPage
6
- def template
7
- render Layout.new(title: "Testing Phlex Views in Rails") do
8
- render Markdown.new(<<~MD)
9
- # Testing Phlex views in Rails
10
-
11
- When you include `Phlex::Testing::Rails::ViewHelper`, views rendered in the test will have a view context, so they can use Rails helpers.
12
- MD
13
- end
14
- end
15
- end
16
- end
17
- end