hanami 2.3.2 → 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 (189) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +103 -19
  3. data/LICENSE +20 -0
  4. data/README.md +18 -35
  5. data/hanami.gemspec +36 -37
  6. data/lib/hanami/app.rb +12 -2
  7. data/lib/hanami/config/db.rb +2 -0
  8. data/lib/hanami/config/i18n.rb +138 -0
  9. data/lib/hanami/config/logger.rb +15 -7
  10. data/lib/hanami/config/null_config.rb +1 -1
  11. data/lib/hanami/config/views.rb +17 -0
  12. data/lib/hanami/config.rb +66 -22
  13. data/lib/hanami/errors.rb +6 -0
  14. data/lib/hanami/extensions/action/i18n_helper.rb +64 -0
  15. data/lib/hanami/extensions/action/name_inferrer.rb +34 -0
  16. data/lib/hanami/extensions/action/slice_configured_action.rb +31 -2
  17. data/lib/hanami/extensions/action.rb +20 -7
  18. data/lib/hanami/extensions/mailer/slice_configured_mailer.rb +134 -0
  19. data/lib/hanami/extensions/mailer.rb +28 -0
  20. data/lib/hanami/extensions/operation/slice_configured_db_operation.rb +2 -0
  21. data/lib/hanami/extensions/view/context.rb +26 -4
  22. data/lib/hanami/extensions/view/part.rb +2 -0
  23. data/lib/hanami/extensions/view/slice_configured_context.rb +7 -0
  24. data/lib/hanami/extensions/view/slice_configured_part.rb +2 -0
  25. data/lib/hanami/extensions/view/slice_configured_view.rb +8 -8
  26. data/lib/hanami/extensions/view/standard_helpers.rb +4 -0
  27. data/lib/hanami/extensions.rb +6 -1
  28. data/lib/hanami/helpers/assets_helper.rb +0 -4
  29. data/lib/hanami/helpers/form_helper.rb +1 -1
  30. data/lib/hanami/helpers/i18n_helper.rb +241 -0
  31. data/lib/hanami/logger/rack_formatter.rb +73 -0
  32. data/lib/hanami/logger/sql_formatter.rb +80 -0
  33. data/lib/hanami/logger/sql_logger.rb +48 -0
  34. data/lib/hanami/middleware/render_errors.rb +2 -2
  35. data/lib/hanami/providers/db.rb +7 -2
  36. data/lib/hanami/providers/db_logging.rb +4 -7
  37. data/lib/hanami/providers/i18n/backend.rb +373 -0
  38. data/lib/hanami/providers/i18n/locale/en.yml +57 -0
  39. data/lib/hanami/providers/i18n.rb +114 -0
  40. data/lib/hanami/providers/logger.rb +23 -1
  41. data/lib/hanami/providers/mailers.rb +101 -0
  42. data/lib/hanami/routes.rb +1 -0
  43. data/lib/hanami/settings/composite_store.rb +53 -0
  44. data/lib/hanami/settings.rb +4 -4
  45. data/lib/hanami/slice/router.rb +15 -10
  46. data/lib/hanami/slice.rb +102 -11
  47. data/lib/hanami/slice_configurable.rb +1 -3
  48. data/lib/hanami/slice_registrar.rb +2 -2
  49. data/lib/hanami/universal_logger.rb +250 -0
  50. data/lib/hanami/version.rb +1 -1
  51. data/lib/hanami/web/rack_logger.rb +2 -80
  52. data/lib/hanami/web/welcome.html.erb +443 -58
  53. data/lib/hanami.rb +4 -2
  54. metadata +34 -280
  55. data/CODE_OF_CONDUCT.md +0 -74
  56. data/FEATURES.md +0 -269
  57. data/LICENSE.md +0 -22
  58. data/spec/integration/action/cookies_spec.rb +0 -58
  59. data/spec/integration/action/csrf_protection_spec.rb +0 -54
  60. data/spec/integration/action/format_config_spec.rb +0 -129
  61. data/spec/integration/action/routes_spec.rb +0 -71
  62. data/spec/integration/action/sessions_spec.rb +0 -50
  63. data/spec/integration/action/slice_configuration_spec.rb +0 -284
  64. data/spec/integration/action/view_rendering/automatic_rendering_spec.rb +0 -247
  65. data/spec/integration/action/view_rendering/paired_view_inference_spec.rb +0 -115
  66. data/spec/integration/action/view_rendering/view_context_spec.rb +0 -221
  67. data/spec/integration/action/view_rendering_spec.rb +0 -89
  68. data/spec/integration/assets/assets_spec.rb +0 -155
  69. data/spec/integration/assets/cross_slice_assets_helpers_spec.rb +0 -129
  70. data/spec/integration/assets/serve_static_assets_spec.rb +0 -152
  71. data/spec/integration/code_loading/loading_from_app_spec.rb +0 -152
  72. data/spec/integration/code_loading/loading_from_lib_spec.rb +0 -242
  73. data/spec/integration/code_loading/loading_from_slice_spec.rb +0 -165
  74. data/spec/integration/container/application_routes_helper_spec.rb +0 -48
  75. data/spec/integration/container/auto_injection_spec.rb +0 -53
  76. data/spec/integration/container/auto_registration_spec.rb +0 -86
  77. data/spec/integration/container/autoloader_spec.rb +0 -82
  78. data/spec/integration/container/imports_spec.rb +0 -253
  79. data/spec/integration/container/prepare_container_spec.rb +0 -125
  80. data/spec/integration/container/provider_environment_spec.rb +0 -52
  81. data/spec/integration/container/provider_lifecycle_spec.rb +0 -61
  82. data/spec/integration/container/shutdown_spec.rb +0 -91
  83. data/spec/integration/container/standard_providers/rack_provider_spec.rb +0 -44
  84. data/spec/integration/container/standard_providers_spec.rb +0 -124
  85. data/spec/integration/db/auto_registration_spec.rb +0 -39
  86. data/spec/integration/db/commands_spec.rb +0 -80
  87. data/spec/integration/db/db_inflector_spec.rb +0 -57
  88. data/spec/integration/db/db_slices_spec.rb +0 -398
  89. data/spec/integration/db/db_spec.rb +0 -245
  90. data/spec/integration/db/gateways_spec.rb +0 -361
  91. data/spec/integration/db/logging_spec.rb +0 -301
  92. data/spec/integration/db/mappers_spec.rb +0 -84
  93. data/spec/integration/db/provider_config_spec.rb +0 -88
  94. data/spec/integration/db/provider_spec.rb +0 -35
  95. data/spec/integration/db/relations_spec.rb +0 -60
  96. data/spec/integration/db/repo_spec.rb +0 -300
  97. data/spec/integration/db/slices_importing_from_parent.rb +0 -130
  98. data/spec/integration/dotenv_loading_spec.rb +0 -138
  99. data/spec/integration/logging/exception_logging_spec.rb +0 -120
  100. data/spec/integration/logging/notifications_spec.rb +0 -68
  101. data/spec/integration/logging/request_logging_spec.rb +0 -202
  102. data/spec/integration/operations/extension_spec.rb +0 -122
  103. data/spec/integration/rack_app/body_parser_spec.rb +0 -108
  104. data/spec/integration/rack_app/method_override_spec.rb +0 -97
  105. data/spec/integration/rack_app/middleware_spec.rb +0 -720
  106. data/spec/integration/rack_app/non_booted_rack_app_spec.rb +0 -104
  107. data/spec/integration/rack_app/rack_app_spec.rb +0 -442
  108. data/spec/integration/rake_tasks_spec.rb +0 -107
  109. data/spec/integration/router/resource_routes_spec.rb +0 -281
  110. data/spec/integration/settings/access_in_slice_class_body_spec.rb +0 -83
  111. data/spec/integration/settings/access_to_constants_spec.rb +0 -46
  112. data/spec/integration/settings/loading_from_env_spec.rb +0 -188
  113. data/spec/integration/settings/settings_component_loading_spec.rb +0 -113
  114. data/spec/integration/settings/slice_registration_spec.rb +0 -145
  115. data/spec/integration/settings/using_types_spec.rb +0 -80
  116. data/spec/integration/setup_spec.rb +0 -165
  117. data/spec/integration/slices/external_slice_spec.rb +0 -91
  118. data/spec/integration/slices/slice_configuration_spec.rb +0 -42
  119. data/spec/integration/slices/slice_loading_spec.rb +0 -171
  120. data/spec/integration/slices/slice_registrations_spec.rb +0 -80
  121. data/spec/integration/slices/slice_routing_spec.rb +0 -219
  122. data/spec/integration/slices_spec.rb +0 -471
  123. data/spec/integration/view/config/default_context_spec.rb +0 -149
  124. data/spec/integration/view/config/inflector_spec.rb +0 -57
  125. data/spec/integration/view/config/part_class_spec.rb +0 -147
  126. data/spec/integration/view/config/part_namespace_spec.rb +0 -103
  127. data/spec/integration/view/config/paths_spec.rb +0 -119
  128. data/spec/integration/view/config/scope_class_spec.rb +0 -147
  129. data/spec/integration/view/config/scope_namespace_spec.rb +0 -103
  130. data/spec/integration/view/config/template_spec.rb +0 -38
  131. data/spec/integration/view/context/assets_spec.rb +0 -79
  132. data/spec/integration/view/context/inflector_spec.rb +0 -40
  133. data/spec/integration/view/context/request_spec.rb +0 -57
  134. data/spec/integration/view/context/routes_spec.rb +0 -84
  135. data/spec/integration/view/helpers/form_helper_spec.rb +0 -174
  136. data/spec/integration/view/helpers/part_helpers_spec.rb +0 -124
  137. data/spec/integration/view/helpers/scope_helpers_spec.rb +0 -84
  138. data/spec/integration/view/helpers/user_defined_helpers/part_helpers_spec.rb +0 -162
  139. data/spec/integration/view/helpers/user_defined_helpers/scope_helpers_spec.rb +0 -119
  140. data/spec/integration/view/parts/default_rendering_spec.rb +0 -138
  141. data/spec/integration/view/slice_configuration_spec.rb +0 -289
  142. data/spec/integration/view/views_spec.rb +0 -103
  143. data/spec/integration/web/content_security_policy_nonce_spec.rb +0 -251
  144. data/spec/integration/web/render_detailed_errors_spec.rb +0 -107
  145. data/spec/integration/web/render_errors_spec.rb +0 -242
  146. data/spec/integration/web/welcome_view_spec.rb +0 -84
  147. data/spec/spec_helper.rb +0 -28
  148. data/spec/support/app_integration.rb +0 -157
  149. data/spec/support/coverage.rb +0 -1
  150. data/spec/support/matchers.rb +0 -32
  151. data/spec/support/rspec.rb +0 -27
  152. data/spec/unit/hanami/config/actions/content_security_policy_spec.rb +0 -96
  153. data/spec/unit/hanami/config/actions/cookies_spec.rb +0 -46
  154. data/spec/unit/hanami/config/actions/csrf_protection_spec.rb +0 -58
  155. data/spec/unit/hanami/config/actions/default_values_spec.rb +0 -43
  156. data/spec/unit/hanami/config/actions/sessions_spec.rb +0 -48
  157. data/spec/unit/hanami/config/actions_spec.rb +0 -52
  158. data/spec/unit/hanami/config/base_url_spec.rb +0 -25
  159. data/spec/unit/hanami/config/console_spec.rb +0 -22
  160. data/spec/unit/hanami/config/db_spec.rb +0 -38
  161. data/spec/unit/hanami/config/inflector_spec.rb +0 -35
  162. data/spec/unit/hanami/config/logger_spec.rb +0 -195
  163. data/spec/unit/hanami/config/render_detailed_errors_spec.rb +0 -25
  164. data/spec/unit/hanami/config/render_errors_spec.rb +0 -25
  165. data/spec/unit/hanami/config/router_spec.rb +0 -44
  166. data/spec/unit/hanami/config/slices_spec.rb +0 -34
  167. data/spec/unit/hanami/config/views_spec.rb +0 -80
  168. data/spec/unit/hanami/env_spec.rb +0 -37
  169. data/spec/unit/hanami/extensions/view/context_spec.rb +0 -59
  170. data/spec/unit/hanami/helpers/assets_helper/asset_url_spec.rb +0 -120
  171. data/spec/unit/hanami/helpers/assets_helper/audio_tag_spec.rb +0 -132
  172. data/spec/unit/hanami/helpers/assets_helper/favicon_tag_spec.rb +0 -87
  173. data/spec/unit/hanami/helpers/assets_helper/image_tag_spec.rb +0 -92
  174. data/spec/unit/hanami/helpers/assets_helper/javascript_tag_spec.rb +0 -143
  175. data/spec/unit/hanami/helpers/assets_helper/stylesheet_tag_spec.rb +0 -126
  176. data/spec/unit/hanami/helpers/assets_helper/video_tag_spec.rb +0 -136
  177. data/spec/unit/hanami/helpers/form_helper_spec.rb +0 -2857
  178. data/spec/unit/hanami/port_spec.rb +0 -117
  179. data/spec/unit/hanami/providers/db/config/default_config_spec.rb +0 -100
  180. data/spec/unit/hanami/providers/db/config/gateway_spec.rb +0 -73
  181. data/spec/unit/hanami/providers/db/config_spec.rb +0 -143
  182. data/spec/unit/hanami/router/errors/not_allowed_error_spec.rb +0 -27
  183. data/spec/unit/hanami/router/errors/not_found_error_spec.rb +0 -22
  184. data/spec/unit/hanami/settings/env_store_spec.rb +0 -52
  185. data/spec/unit/hanami/settings_spec.rb +0 -111
  186. data/spec/unit/hanami/slice_configurable_spec.rb +0 -141
  187. data/spec/unit/hanami/slice_name_spec.rb +0 -47
  188. data/spec/unit/hanami/slice_spec.rb +0 -99
  189. data/spec/unit/hanami/web/rack_logger_spec.rb +0 -99
@@ -1,221 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe "App action / View rendering / View context", :app_integration do
4
- subject(:context) {
5
- # We capture the context during rendering via our view spies; see the view classes below
6
- action.call("REQUEST_METHOD" => "GET", "QUERY_STRING" => "/mock_request")
7
- action.view.called_with[:context]
8
- }
9
-
10
- before do
11
- with_directory(make_tmp_directory) do
12
- write "config/app.rb", <<~RUBY
13
- module TestApp
14
- class App < Hanami::App
15
- end
16
- end
17
- RUBY
18
-
19
- write "app/action.rb", <<~RUBY
20
- # auto_register: false
21
-
22
- require "hanami/view"
23
-
24
- module TestApp
25
- class Action < Hanami::Action
26
- end
27
- end
28
- RUBY
29
-
30
- before_prepare if respond_to?(:before_prepare)
31
- require "hanami/prepare"
32
- end
33
- end
34
-
35
- describe "app action" do
36
- let(:action) { TestApp::App["actions.posts.show"] }
37
-
38
- def before_prepare
39
- write "app/actions/posts/show.rb", <<~RUBY
40
- module TestApp
41
- module Actions
42
- module Posts
43
- class Show < TestApp::Action
44
- end
45
- end
46
- end
47
- end
48
- RUBY
49
-
50
- # Custom view class as a spy for `#call` args
51
- write "app/views/posts/show.rb", <<~RUBY
52
- module TestApp
53
- module Views
54
- module Posts
55
- class Show
56
- attr_reader :called_with
57
-
58
- def call(**args)
59
- @called_with = args
60
- ""
61
- end
62
- end
63
- end
64
- end
65
- end
66
- RUBY
67
- end
68
-
69
- context "no context class defined" do
70
- it "defines and uses a context class" do
71
- expect(context).to be_an_instance_of TestApp::Views::Context
72
- expect(context.class).to be < Hanami::View::Context
73
- end
74
-
75
- it "includes the request" do
76
- expect(context.request).to be_an_instance_of Hanami::Action::Request
77
- expect(context.request.env["QUERY_STRING"]).to eq "/mock_request"
78
- end
79
- end
80
-
81
- context "context class defined" do
82
- def before_prepare
83
- super()
84
-
85
- write "app/views/context.rb", <<~RUBY
86
- # auto_register: false
87
-
88
- module TestApp
89
- module Views
90
- class Context < Hanami::View::Context
91
- def concrete_app_context?
92
- true
93
- end
94
- end
95
- end
96
- end
97
- RUBY
98
- end
99
-
100
- it "uses the defined context class" do
101
- expect(context).to be_an_instance_of TestApp::Views::Context
102
- expect(context).to be_a_concrete_app_context
103
- end
104
- end
105
-
106
- context "hanami-view not bundled" do
107
- before do
108
- allow(Hanami).to receive(:bundled?).and_call_original
109
- expect(Hanami).to receive(:bundled?).with("hanami-view").and_return false
110
- end
111
-
112
- it "does not provide a context" do
113
- expect(context).to be nil
114
- end
115
-
116
- context "context class defined" do
117
- def before_prepare
118
- super()
119
-
120
- write "app/views/context.rb", <<~RUBY
121
- module TestApp
122
- module Views
123
- class Context
124
- def initialize(**)
125
- end
126
-
127
- def with(**)
128
- self
129
- end
130
-
131
- def concrete_app_context?
132
- true
133
- end
134
- end
135
- end
136
- end
137
- RUBY
138
- end
139
-
140
- it "uses the defined context class" do
141
- expect(context).to be_an_instance_of TestApp::Views::Context
142
- expect(context).to be_a_concrete_app_context
143
- end
144
- end
145
- end
146
- end
147
-
148
- describe "slice action" do
149
- let(:action) { Main::Slice["actions.posts.show"] }
150
-
151
- def before_prepare
152
- write "slices/main/action.rb", <<~RUBY
153
- module Main
154
- class Action < TestApp::Action
155
- end
156
- end
157
- RUBY
158
-
159
- write "slices/main/actions/posts/show.rb", <<~RUBY
160
- module Main
161
- module Actions
162
- module Posts
163
- class Show < Main::Action
164
- end
165
- end
166
- end
167
- end
168
- RUBY
169
-
170
- # Custom view class as a spy for `#call` args
171
- write "slices/main/views/posts/show.rb", <<~RUBY
172
- module Main
173
- module Views
174
- module Posts
175
- class Show
176
- attr_reader :called_with
177
-
178
- def call(**args)
179
- @called_with = args
180
- ""
181
- end
182
- end
183
- end
184
- end
185
- end
186
- RUBY
187
- end
188
-
189
- context "no context class defined" do
190
- it "defines and uses a context class" do
191
- expect(context).to be_an_instance_of Main::Views::Context
192
- expect(context.class).to be < Hanami::View::Context
193
- end
194
- end
195
-
196
- context "context class defined" do
197
- def before_prepare
198
- super()
199
-
200
- write "slices/main/views/context.rb", <<~RUBY
201
- # auto_register: false
202
-
203
- module Main
204
- module Views
205
- class Context < Hanami::View::Context
206
- def concrete_slice_context?
207
- true
208
- end
209
- end
210
- end
211
- end
212
- RUBY
213
- end
214
-
215
- it "uses the defined context class" do
216
- expect(context).to be_an_instance_of Main::Views::Context
217
- expect(context).to be_a_concrete_slice_context
218
- end
219
- end
220
- end
221
- end
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe "App action / View rendering", :app_integration do
4
- specify "Views render with a request-specific context object" do
5
- with_tmp_directory(Dir.mktmpdir) do
6
- write "config/app.rb", <<~RUBY
7
- require "hanami"
8
-
9
- module TestApp
10
- class App < Hanami::App
11
- end
12
- end
13
- RUBY
14
-
15
- write "app/action.rb", <<~RUBY
16
- # auto_register: false
17
-
18
- module TestApp
19
- class Action < Hanami::Action
20
- end
21
- end
22
- RUBY
23
-
24
- write "app/view.rb", <<~RUBY
25
- # auto_register: false
26
-
27
- require "hanami/view"
28
-
29
- module TestApp
30
- class View < Hanami::View
31
- end
32
- end
33
- RUBY
34
-
35
- write "app/actions/users/show.rb", <<~RUBY
36
- module TestApp
37
- module Actions
38
- module Users
39
- class Show < TestApp::Action
40
- include Deps[view: "views.users.show"]
41
-
42
- def handle(req, res)
43
- res[:job] = "Singer"
44
- res[:age] = 0
45
-
46
- res.render view, name: req.params[:name], age: 51
47
- end
48
- end
49
- end
50
- end
51
- end
52
- RUBY
53
-
54
- write "app/views/users/show.rb", <<~RUBY
55
- module TestApp
56
- module Views
57
- module Users
58
- class Show < TestApp::View
59
- expose :name, :job, :age
60
- end
61
- end
62
- end
63
- end
64
- RUBY
65
-
66
- write "app/templates/layouts/app.html.slim", <<~SLIM
67
- html
68
- body
69
- == yield
70
- SLIM
71
-
72
- write "app/templates/users/show.html.slim", <<~'SLIM'
73
- h1 Hello, #{name}
74
- - request.params.to_h.values.sort.each do |value|
75
- p = value
76
- p = job
77
- p = age
78
- SLIM
79
-
80
- require "hanami/prepare"
81
-
82
- action = TestApp::App["actions.users.show"]
83
- response = action.(name: "Jennifer", last_name: "Lopez")
84
- rendered = response.body[0]
85
-
86
- expect(rendered).to eq "<html><body><h1>Hello, Jennifer</h1><p>Jennifer</p><p>Lopez</p><p>Singer</p><p>51</p></body></html>"
87
- end
88
- end
89
- end
@@ -1,155 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rack/test"
4
- require "stringio"
5
-
6
- RSpec.describe "Assets", :app_integration do
7
- include Rack::Test::Methods
8
- let(:app) { Hanami.app }
9
- let(:root) { make_tmp_directory }
10
-
11
- before do
12
- with_directory(root) do
13
- write "config/app.rb", <<~RUBY
14
- module TestApp
15
- class App < Hanami::App
16
- config.logger.stream = StringIO.new
17
- end
18
- end
19
- RUBY
20
-
21
- write "config/assets.js", <<~JS
22
- import * as assets from "hanami-assets";
23
- await assets.run();
24
- JS
25
-
26
- write "package.json", <<~JSON
27
- {
28
- "type": "module"
29
- }
30
- JSON
31
-
32
- write "app/view.rb", <<~RUBY
33
- # auto_register: false
34
-
35
- require "hanami/view"
36
-
37
- module TestApp
38
- class View < Hanami::View
39
- config.layout = nil
40
- end
41
- end
42
- RUBY
43
-
44
- write "app/views/posts/show.rb", <<~RUBY
45
- module TestApp
46
- module Views
47
- module Posts
48
- class Show < TestApp::View
49
- end
50
- end
51
- end
52
- end
53
- RUBY
54
-
55
- write "app/templates/posts/show.html.erb", <<~ERB
56
- <%= stylesheet_tag("app") %>
57
- <%= javascript_tag("app") %>
58
- ERB
59
-
60
- write "app/assets/js/app.ts", <<~TS
61
- import "../css/app.css";
62
-
63
- console.log("Hello from index.ts");
64
- TS
65
-
66
- write "app/assets/css/app.css", <<~CSS
67
- .btn {
68
- background: #f00;
69
- }
70
- CSS
71
-
72
- before_prepare if respond_to?(:before_prepare)
73
- require "hanami/prepare"
74
- end
75
- end
76
-
77
- specify "assets are available in helpers and in `assets` component" do
78
- compile_assets!
79
-
80
- output = Hanami.app["views.posts.show"].call.to_s
81
-
82
- expect(output).to match(%r{<link href="/assets/app-[A-Z0-9]{8}.css" type="text/css" rel="stylesheet">})
83
- expect(output).to match(%r{<script src="/assets/app-[A-Z0-9]{8}.js" type="text/javascript"></script>})
84
-
85
- assets = Hanami.app["assets"]
86
-
87
- expect(assets["app.css"].to_s).to match(%r{/assets/app-[A-Z0-9]{8}.css})
88
- expect(assets["app.js"].to_s).to match(%r{/assets/app-[A-Z0-9]{8}.js})
89
- end
90
-
91
- describe "slice with assets" do
92
- def before_prepare
93
- write "slices/main/view.rb", <<~RUBY
94
- # auto_register: false
95
-
96
- module Main
97
- class View < TestApp::View
98
- end
99
- end
100
- RUBY
101
-
102
- write "slices/main/views/posts/show.rb", <<~RUBY
103
- module Main
104
- module Views
105
- module Posts
106
- class Show < Main::View
107
- end
108
- end
109
- end
110
- end
111
- RUBY
112
-
113
- write "slices/main/templates/posts/show.html.erb", <<~ERB
114
- <%= stylesheet_tag("app") %>
115
- <%= javascript_tag("app") %>
116
- ERB
117
-
118
- write "slices/main/assets/js/app.ts", <<~TS
119
- import "../css/app.css";
120
-
121
- console.log("Hello from main slice index.ts");
122
- TS
123
-
124
- write "slices/main/assets/css/app.css", <<~CSS
125
- .btn {
126
- background: #f00;
127
- }
128
- CSS
129
- end
130
-
131
- specify "the slice's assets are available in its own and distinct `assets` component" do
132
- compile_assets!
133
-
134
- output = Main::Slice["views.posts.show"].call.to_s
135
-
136
- expect(output).to match(%r{<link href="/assets/_main/app-[A-Z0-9]{8}.css" type="text/css" rel="stylesheet">})
137
- expect(output).to match(%r{<script src="/assets/_main/app-[A-Z0-9]{8}.js" type="text/javascript"></script>})
138
-
139
- assets = Main::Slice["assets"]
140
-
141
- expect(assets["app.css"].to_s).to match(%r{/assets/_main/app-[A-Z0-9]{8}.css})
142
- expect(assets["app.js"].to_s).to match(%r{/assets/_main/app-[A-Z0-9]{8}.js})
143
- end
144
- end
145
-
146
- describe "slice without assets" do
147
- def before_prepare
148
- write "slices/main/.keep", ""
149
- end
150
-
151
- it "does not have an assets component" do
152
- expect(Main::Slice.key?("assets")).to be false
153
- end
154
- end
155
- end
@@ -1,129 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rack/test"
4
- require "stringio"
5
-
6
- RSpec.describe "Cross-slice assets via helpers", :app_integration do
7
- include Rack::Test::Methods
8
- let(:app) { Hanami.app }
9
- let(:root) { make_tmp_directory }
10
-
11
- before do
12
- with_directory(root) do
13
- write "config/app.rb", <<~RUBY
14
- module TestApp
15
- class App < Hanami::App
16
- config.logger.stream = StringIO.new
17
- end
18
- end
19
- RUBY
20
-
21
- write "config/slices/admin.rb", <<~RUBY
22
- module Admin
23
- class Slice < Hanami::Slice
24
- # TODO: we should update `import` to make importing from the app nicer
25
- # TODO: this test failed when I tried doing `as: "app"` (string instead of symbol); fix this in dry-system
26
- import keys: ["assets"], from: Hanami.app.container, as: :app
27
- end
28
- end
29
- RUBY
30
-
31
- write "config/assets.js", <<~JS
32
- import * as assets from "hanami-assets";
33
- await assets.run();
34
- JS
35
-
36
- write "package.json", <<~JSON
37
- {
38
- "type": "module"
39
- }
40
- JSON
41
-
42
- write "app/view.rb", <<~RUBY
43
- # auto_register: false
44
-
45
- require "hanami/view"
46
-
47
- module TestApp
48
- class View < Hanami::View
49
- config.layout = nil
50
- end
51
- end
52
- RUBY
53
-
54
- write "app/assets/js/app.ts", <<~TS
55
- import "../css/app.css";
56
-
57
- console.log("Hello from index.ts");
58
- TS
59
-
60
- write "app/assets/css/app.css", <<~CSS
61
- .btn {
62
- background: #f00;
63
- }
64
- CSS
65
-
66
- write "slices/admin/assets/js/app.ts", <<~TS
67
- import "../css/app.css";
68
-
69
- console.log("Hello from admin's index.ts");
70
- TS
71
-
72
- write "slices/admin/assets/css/app.css", <<~CSS
73
- .btn {
74
- background: #f00;
75
- }
76
- CSS
77
-
78
- write "slices/admin/view.rb", <<~RUBY
79
- # auto_register: false
80
-
81
- module Admin
82
- class View < TestApp::View
83
- end
84
- end
85
- RUBY
86
-
87
- write "slices/admin/views/posts/show.rb", <<~RUBY
88
- module Admin
89
- module Views
90
- module Posts
91
- class Show < Admin::View
92
- end
93
- end
94
- end
95
- end
96
- RUBY
97
-
98
- write "slices/admin/views/context.rb", <<~RUBY
99
- # auto_register: false
100
-
101
- require "hanami/view"
102
-
103
- module Admin
104
- module Views
105
- class Context < Hanami::View::Context
106
- include Deps[app_assets: "app.assets"]
107
- end
108
- end
109
- end
110
- RUBY
111
-
112
- write "slices/admin/templates/posts/show.html.erb", <<~ERB
113
- <%= stylesheet_tag(app_assets["app.css"]) %>
114
- <%= javascript_tag(app_assets["app.js"]) %>
115
- ERB
116
-
117
- before_prepare if respond_to?(:before_prepare)
118
- require "hanami/prepare"
119
- end
120
- end
121
-
122
- specify "assets are available in helpers and in `assets` component" do
123
- compile_assets!
124
-
125
- output = Admin::Slice["views.posts.show"].call.to_s
126
- expect(output).to match(%r{<link href="/assets/app-[A-Z0-9]{8}.css" type="text/css" rel="stylesheet">})
127
- expect(output).to match(%r{<script src="/assets/app-[A-Z0-9]{8}.js" type="text/javascript"></script>})
128
- end
129
- end