hanami 2.1.0.beta1 → 2.1.0.beta2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -4
  3. data/README.md +1 -1
  4. data/lib/hanami/app.rb +5 -0
  5. data/lib/hanami/config/actions.rb +4 -7
  6. data/lib/hanami/config/assets.rb +84 -0
  7. data/lib/hanami/config/null_config.rb +3 -0
  8. data/lib/hanami/config.rb +17 -5
  9. data/lib/hanami/extensions/action.rb +4 -2
  10. data/lib/hanami/extensions/view/standard_helpers.rb +4 -0
  11. data/lib/hanami/helpers/assets_helper.rb +772 -0
  12. data/lib/hanami/middleware/assets.rb +21 -0
  13. data/lib/hanami/middleware/render_errors.rb +4 -7
  14. data/lib/hanami/providers/assets.rb +44 -0
  15. data/lib/hanami/rake_tasks.rb +19 -18
  16. data/lib/hanami/settings.rb +1 -1
  17. data/lib/hanami/slice.rb +25 -4
  18. data/lib/hanami/version.rb +1 -1
  19. data/lib/hanami.rb +2 -2
  20. data/spec/integration/assets/assets_spec.rb +101 -0
  21. data/spec/integration/assets/serve_static_assets_spec.rb +152 -0
  22. data/spec/integration/logging/exception_logging_spec.rb +115 -0
  23. data/spec/integration/logging/notifications_spec.rb +68 -0
  24. data/spec/integration/logging/request_logging_spec.rb +128 -0
  25. data/spec/integration/rack_app/middleware_spec.rb +4 -4
  26. data/spec/integration/rack_app/rack_app_spec.rb +0 -221
  27. data/spec/integration/rake_tasks_spec.rb +107 -0
  28. data/spec/integration/view/context/assets_spec.rb +3 -9
  29. data/spec/integration/web/render_detailed_errors_spec.rb +17 -0
  30. data/spec/integration/web/render_errors_spec.rb +6 -4
  31. data/spec/support/app_integration.rb +46 -2
  32. data/spec/unit/hanami/config/actions/content_security_policy_spec.rb +24 -36
  33. data/spec/unit/hanami/config/actions/csrf_protection_spec.rb +4 -3
  34. data/spec/unit/hanami/config/actions/default_values_spec.rb +3 -2
  35. data/spec/unit/hanami/env_spec.rb +11 -25
  36. data/spec/unit/hanami/helpers/assets_helper/asset_url_spec.rb +109 -0
  37. data/spec/unit/hanami/helpers/assets_helper/audio_spec.rb +136 -0
  38. data/spec/unit/hanami/helpers/assets_helper/favicon_spec.rb +91 -0
  39. data/spec/unit/hanami/helpers/assets_helper/image_spec.rb +96 -0
  40. data/spec/unit/hanami/helpers/assets_helper/javascript_spec.rb +147 -0
  41. data/spec/unit/hanami/helpers/assets_helper/stylesheet_spec.rb +130 -0
  42. data/spec/unit/hanami/helpers/assets_helper/video_spec.rb +136 -0
  43. data/spec/unit/hanami/version_spec.rb +1 -1
  44. metadata +32 -4
  45. data/lib/hanami/assets/app_config.rb +0 -61
  46. data/lib/hanami/assets/config.rb +0 -53
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b7770e05814c2c09affa6bb627a563f56f64601e2587c474f5139c42a17d25c
4
- data.tar.gz: 67e7f5b426b6e50023f5c3baad3b91d422b1c82dcc25213f4d6fe4efab0ff681
3
+ metadata.gz: 219f0622f399b1e3f94c77c5d7556b6bdd26122181929b444eb8fca436e9d0d2
4
+ data.tar.gz: 3dc2468f5561eb8d70fa62d3f5b7f755f096ab764cfbfd43fca93d6001061908
5
5
  SHA512:
6
- metadata.gz: eeb3695b89718d12ca4d6376b997af52a21fe39e39e8b8e2e932422d291314a9b8bc1f11ade96279737847283234a783039e8e7e0364fc488def2ab18f8efaf5
7
- data.tar.gz: 63cf7e291a0b6789eb16430ce0c1945ce945787fab250c8e804869276428045404d0f98205fdf481c9a80e48cf89b38a243bff00b52a900349fcffef5cc62254
6
+ metadata.gz: f38c4b54000182e7ab4fb63be367131c6580df35519f97d5fd171de016b4ec96590c5f7c0822044421e0efec64813a492d4e106a803ea6028ba6d6d105db2ee6
7
+ data.tar.gz: 8648d6b907fbc4a600a4561184631b2868a577a2280c6bade34af1726d3bd6458045268c8517956e52bc4e6b2a6dfc78b0bee938e254563cac3fbbae91afc6d2
data/CHANGELOG.md CHANGED
@@ -2,6 +2,33 @@
2
2
 
3
3
  The web, with simplicity.
4
4
 
5
+ ## v2.1.0.beta2.1 - 2023-10-04
6
+
7
+ ### Added
8
+
9
+ - [Tim Riley, Luca Guidi] Added assets helpers aliases (#1319, #1339):
10
+ - Added `#js` and `#javascript_tag` as alias for `#javascript`
11
+ - Added `#css` and `#stylesheet_link_tag` as alias for `#stylesheet`
12
+ - Added `#image_tag` as alias for `#image`
13
+ - Added `#favicon_link_tag` as alias for `#favicon`
14
+ - Added `#video_tag` as alias for `#video`
15
+ - Added `#audio_tag` as alias for `#audio`
16
+
17
+ ## v2.1.0.beta2 - 2023-10-04
18
+
19
+ ### Added
20
+
21
+ - [Luca Guidi, Tim Riley] Reimplement assets integration (#1319, #1332, #1333, #1336)
22
+ - [Tim Riley] Introduce `Hanami::Helpers::AssetsHelper`, automatically included in view templates, scopes and parts when hanami-assets is bundled (#1319)
23
+
24
+ ### Fixed
25
+
26
+ - [Tim Riley] Return appropriate response statuses based on error type (#1330)
27
+
28
+ ### Changed
29
+
30
+ - [Tim Riley] Removed `#asset_path` in favour of `#asset_url` only (#1319)
31
+
5
32
  ## v2.1.0.beta1 - 2023-06-29
6
33
 
7
34
  ### Added
@@ -23,10 +50,6 @@ The web, with simplicity.
23
50
 
24
51
  ## v2.0.3 - 2023-02-01
25
52
 
26
- ### Added
27
-
28
- - [Luca Guidi] Official support for Ruby 3.2
29
-
30
53
  ### Fixed
31
54
 
32
55
  - [Adam Lassek] Ensure to setup a logger in a non-default Hanami env
data/README.md CHANGED
@@ -62,7 +62,7 @@ You can give back to Open Source, by supporting Hanami development via [GitHub S
62
62
  * Guides: https://guides.hanamirb.org
63
63
  * Snippets: https://snippets.hanamirb.org
64
64
  * Mailing List: http://hanamirb.org/mailing-list
65
- * API Doc: http://rdoc.info/gems/hanami
65
+ * API Doc: https://gemdocs.org/gems/hanami/latest
66
66
  * Bugs/Issues: https://github.com/hanami/hanami/issues
67
67
  * Stack Overflow: http://stackoverflow.com/questions/tagged/hanami
68
68
  * Forum: https://discourse.hanamirb.org
data/lib/hanami/app.rb CHANGED
@@ -161,6 +161,11 @@ module Hanami
161
161
  require_relative "providers/rack"
162
162
  register_provider(:rack, source: Hanami::Providers::Rack, namespace: true)
163
163
  end
164
+
165
+ if Hanami.bundled?("hanami-assets")
166
+ require_relative "providers/assets"
167
+ register_provider(:assets, source: Providers::Assets.for_slice(self))
168
+ end
164
169
  end
165
170
 
166
171
  def prepare_autoloader
@@ -99,12 +99,7 @@ module Hanami
99
99
  super()
100
100
 
101
101
  @base_config = Hanami::Action.config.dup
102
- @content_security_policy = ContentSecurityPolicy.new do |csp|
103
- if assets_server_url = options[:assets_server_url]
104
- csp[:script_src] += " #{assets_server_url}"
105
- csp[:style_src] += " #{assets_server_url}"
106
- end
107
- end
102
+ @content_security_policy = ContentSecurityPolicy.new
108
103
 
109
104
  configure_defaults
110
105
  end
@@ -118,7 +113,9 @@ module Hanami
118
113
  private :initialize_copy
119
114
 
120
115
  # @api private
121
- def finalize!
116
+ def finalize!(app_config)
117
+ @base_config.root_directory = app_config.root
118
+
122
119
  # A nil value for `csrf_protection` means it has not been explicitly configured
123
120
  # (neither true nor false), so we can default it to whether sessions are enabled
124
121
  self.csrf_protection = sessions.enabled? if csrf_protection.nil?
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/configurable"
4
+ require "hanami/assets"
5
+
6
+ module Hanami
7
+ class Config
8
+ # Hanami assets config
9
+ #
10
+ # This exposes all the settings from the standalone `Hanami::Assets` class, pre-configured with
11
+ # sensible defaults for actions within a full Hanami app. It also provides additional settings
12
+ # for further integration of actions with other full stack app components.
13
+ #
14
+ # @since 2.1.0
15
+ # @api public
16
+ class Assets
17
+ include Dry::Configurable
18
+
19
+ # @!attribute [rw] serve
20
+ # Serve static assets.
21
+ #
22
+ # When this is `true`, the app will serve static assets.
23
+ #
24
+ # If not set, this will:
25
+ #
26
+ # * Check if the `HANAMI_SERVE_ASSETS` environment variable is set to `"true"`.
27
+ # * If not, it will check if the app is running in the `development` or `test` environment.
28
+ #
29
+ # @example
30
+ # config.assets.serve = true
31
+ #
32
+ # @return [Hanami::Config::Actions::Cookies]
33
+ #
34
+ # @api public
35
+ # @since 2.1.0
36
+ setting :serve
37
+
38
+ # @api private
39
+ attr_reader :base_config
40
+ protected :base_config
41
+
42
+ # @api private
43
+ def initialize(*, **options)
44
+ super()
45
+
46
+ @base_config = Hanami::Assets::Config.new(**options)
47
+
48
+ configure_defaults
49
+ end
50
+
51
+ # @api private
52
+ def initialize_copy(source)
53
+ super
54
+ @base_config = source.base_config.dup
55
+ end
56
+ private :initialize_copy
57
+
58
+ private
59
+
60
+ def configure_defaults
61
+ self.serve =
62
+ if ENV.key?("HANAMI_SERVE_ASSETS")
63
+ ENV["HANAMI_SERVE_ASSETS"] == "true"
64
+ else
65
+ Hanami.env?(:development, :test)
66
+ end
67
+ end
68
+
69
+ def method_missing(name, *args, &block)
70
+ if config.respond_to?(name)
71
+ config.public_send(name, *args, &block)
72
+ elsif base_config.respond_to?(name)
73
+ base_config.public_send(name, *args, &block)
74
+ else
75
+ super
76
+ end
77
+ end
78
+
79
+ def respond_to_missing?(name, _incude_all = false)
80
+ config.respond_to?(name) || base_config.respond_to?(name) || super
81
+ end
82
+ end
83
+ end
84
+ end
@@ -9,6 +9,9 @@ module Hanami
9
9
  # loaded)
10
10
  class NullConfig
11
11
  include Dry::Configurable
12
+
13
+ def finalize!(*)
14
+ end
12
15
  end
13
16
  end
14
17
  end
data/lib/hanami/config.rb CHANGED
@@ -99,6 +99,7 @@ module Hanami
99
99
  rack.monitor
100
100
  routes
101
101
  settings
102
+ assets
102
103
  ]
103
104
 
104
105
  # @!attribute [rw] no_auto_register_paths
@@ -263,6 +264,7 @@ module Hanami
263
264
  attr_reader :assets
264
265
 
265
266
  # @api private
267
+ # rubocop:disable Metrics/AbcSize
266
268
  def initialize(app_name:, env:)
267
269
  @app_name = app_name
268
270
  @env = env
@@ -275,10 +277,6 @@ module Hanami
275
277
 
276
278
  @logger = Config::Logger.new(env: env, app_name: app_name)
277
279
 
278
- # TODO: Make assets config dependent
279
- require "hanami/assets/app_config"
280
- @assets = Hanami::Assets::AppConfig.new
281
-
282
280
  @actions = load_dependent_config("hanami-controller") {
283
281
  require_relative "config/actions"
284
282
  Actions.new
@@ -295,8 +293,22 @@ module Hanami
295
293
  Views.new
296
294
  }
297
295
 
296
+ @assets = load_dependent_config("hanami-assets") {
297
+ require_relative "config/assets"
298
+
299
+ public_dir = root.join("public")
300
+
301
+ Hanami::Config::Assets.new(
302
+ # TODO: check if `sources` are still needed
303
+ sources: root.join("app", "assets"),
304
+ destination: public_dir.join("assets"),
305
+ manifest_path: public_dir.join("assets.json")
306
+ )
307
+ }
308
+
298
309
  yield self if block_given?
299
310
  end
311
+ # rubocop:enable Metrics/AbcSize
300
312
 
301
313
  # @api private
302
314
  def initialize_copy(source)
@@ -323,7 +335,7 @@ module Hanami
323
335
  def finalize!
324
336
  # Finalize nested configs
325
337
  assets.finalize!
326
- actions.finalize!
338
+ actions.finalize!(self)
327
339
  views.finalize!
328
340
  logger.finalize!
329
341
  router.finalize!
@@ -94,10 +94,12 @@ module Hanami
94
94
  end
95
95
 
96
96
  # @api private
97
- def _handle_exception(request, _response, exception)
97
+ def _handle_exception(request, _response, _exception)
98
+ super
99
+ rescue StandardError => exception
98
100
  rack_monitor&.instrument(:error, exception: exception, env: request.env)
99
101
 
100
- super
102
+ raise
101
103
  end
102
104
 
103
105
  # @api private
@@ -8,6 +8,10 @@ module Hanami
8
8
  include Hanami::View::Helpers::NumberFormattingHelper
9
9
  include Hanami::View::Helpers::TagHelper
10
10
  include Helpers::FormHelper
11
+
12
+ if Hanami.bundled?("hanami-assets")
13
+ include Helpers::AssetsHelper
14
+ end
11
15
  end
12
16
  end
13
17
  end