brut 0.0.20 → 0.0.21

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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/brut/back_end/seed_data.rb +19 -2
  4. data/lib/brut/back_end/sidekiq/middlewares/server.rb +2 -1
  5. data/lib/brut/back_end/sidekiq/middlewares.rb +2 -1
  6. data/lib/brut/back_end/sidekiq.rb +2 -1
  7. data/lib/brut/back_end/validator.rb +5 -1
  8. data/lib/brut/back_end.rb +4 -2
  9. data/lib/brut/cli.rb +4 -3
  10. data/lib/brut/factory_bot.rb +0 -5
  11. data/lib/brut/framework/app.rb +70 -5
  12. data/lib/brut/framework/config.rb +5 -3
  13. data/lib/brut/framework/container.rb +3 -2
  14. data/lib/brut/framework/errors.rb +12 -4
  15. data/lib/brut/framework/mcp.rb +62 -1
  16. data/lib/brut/framework/project_environment.rb +6 -2
  17. data/lib/brut/framework.rb +1 -1
  18. data/lib/brut/front_end/component.rb +35 -12
  19. data/lib/brut/front_end/components/constraint_violations.rb +1 -1
  20. data/lib/brut/front_end/components/form_tag.rb +1 -1
  21. data/lib/brut/front_end/components/inputs/csrf_token.rb +1 -1
  22. data/lib/brut/front_end/components/inputs/text_field.rb +1 -1
  23. data/lib/brut/front_end/components/time_tag.rb +1 -1
  24. data/lib/brut/front_end/layout.rb +16 -0
  25. data/lib/brut/front_end/page.rb +51 -26
  26. data/lib/brut/front_end/routing.rb +5 -1
  27. data/lib/brut/front_end.rb +4 -13
  28. data/lib/brut/i18n/base_methods.rb +37 -3
  29. data/lib/brut/i18n/for_back_end.rb +3 -0
  30. data/lib/brut/i18n/for_cli.rb +3 -0
  31. data/lib/brut/i18n/http_accept_language.rb +47 -0
  32. data/lib/brut/instrumentation/open_telemetry.rb +25 -0
  33. data/lib/brut/instrumentation.rb +3 -5
  34. data/lib/brut/sinatra_helpers.rb +1 -0
  35. data/lib/brut/spec_support/component_support.rb +18 -4
  36. data/lib/brut/spec_support/e2e_support.rb +1 -1
  37. data/lib/brut/spec_support/general_support.rb +3 -0
  38. data/lib/brut/spec_support/handler_support.rb +6 -1
  39. data/lib/brut/spec_support/matcher.rb +1 -0
  40. data/lib/brut/spec_support/matchers/be_page_for.rb +1 -0
  41. data/lib/brut/spec_support/matchers/have_html_attribute.rb +1 -0
  42. data/lib/brut/spec_support/matchers/have_i18n_string.rb +2 -5
  43. data/lib/brut/spec_support/matchers/have_link_to.rb +1 -0
  44. data/lib/brut/spec_support/matchers/have_redirected_to.rb +1 -0
  45. data/lib/brut/spec_support/matchers/have_rendered.rb +1 -0
  46. data/lib/brut/spec_support/matchers/have_returned_rack_response.rb +44 -0
  47. data/lib/brut/spec_support.rb +1 -1
  48. data/lib/brut/version.rb +1 -1
  49. data/lib/brut.rb +5 -4
  50. metadata +2 -9
  51. data/doc-src/architecture.md +0 -102
  52. data/doc-src/assets.md +0 -98
  53. data/doc-src/forms.md +0 -214
  54. data/doc-src/handlers.md +0 -83
  55. data/doc-src/javascript.md +0 -265
  56. data/doc-src/keyword-injection.md +0 -183
  57. data/doc-src/pages.md +0 -210
  58. data/doc-src/route-hooks.md +0 -59
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a6e2324291c72fa3b3012b38b604104ce6d00af875326c20df00cfc42d16a9c
4
- data.tar.gz: c16981e60940c676e21590f9a3db5845c8cc77ee2c7dd86eba9cfb8777ac7a32
3
+ metadata.gz: 13ef62dae2d2a40f20fce1aa2f5df7809fc41ae99b75b2a9aefd25423ef51fb3
4
+ data.tar.gz: 65b9e0b2b77a441210d0047da5c6c8f77d01d97a954d64626683d11fa7f2f1d1
5
5
  SHA512:
6
- metadata.gz: b41d3231c6e589b06deb3f668ff42983bf1021757961e1edf5c0b040f8e64dc4c4d4e481bf9feb34a3104371eecd11e5e0a63414a221f8748eca4982cebccec9
7
- data.tar.gz: 5896d743754d4e9538a85268a3b72856b3b07ca940798e3732db577d67691fc819b7e927aa2a15a309c4440c7ea10beca45df5473d9ca7247b75e6f83678e4e8
6
+ metadata.gz: 4a524485b78b02dd2fee75c540b8cef83fc1c79e8ffa6ec8b214995000c42c6e41db5dbfe8ab65fe28b3e9f08eb9edb63f9156e4208618750f3165bf548995ee
7
+ data.tar.gz: af71268d9598c889731db3cbff39c47d14173ae4112b4eb9e723ff917bf036da93ee4efb3bdffc7f0fc1136abc71b089ab64fe0f8e503226f61e1b0511ffa9f1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- brut (0.0.20)
4
+ brut (0.0.21)
5
5
  concurrent-ruby
6
6
  i18n
7
7
  irb
@@ -1,10 +1,19 @@
1
1
  require_relative "../factory_bot"
2
2
  module Brut
3
3
  module BackEnd
4
- # Base class and manager of Seed Data for the app. Seed Data is data used for development. It is not for populating e.g.
5
- # reference data or other stuff in production.
4
+ # Base class and manager of Seed Data for the app. Seed Data is data used for development.
5
+ # It is not for populating e.g. reference data or other stuff in production, nor is it for
6
+ # managing test data.
6
7
  #
7
8
  # Seed Data uses FactoryBot.
9
+ #
10
+ # To create your own seed data:
11
+ #
12
+ # 1. Inherit from this class. Doing so will register your class with an internal data structure
13
+ # Brut will use to create all seed data.
14
+ # 2. Provide a no-arg initializer (although you are unlikely to need any initializer at all).
15
+ # 3. Implement {#seed!} to use Factory Bot to create all the seed data. This method should be self-contained
16
+ # and not rely on other seed data classes. It need not be idempotent.
8
17
  class SeedData
9
18
  def self.inherited(seed_data_klass)
10
19
  @classes ||= []
@@ -12,10 +21,13 @@ module Brut
12
21
  end
13
22
  def self.classes = @classes || []
14
23
 
24
+ # Sets up anything needed before seed data can be created. Do not override this method.
15
25
  def setup!
16
26
  Brut::FactoryBot.new.setup!
17
27
  end
18
28
 
29
+ # Loads all seed data registered with this class. Seed data is registered when a class
30
+ # extends this one. Do not override this method.
19
31
  def load_seeds!
20
32
  DB.transaction do
21
33
  self.class.classes.each do |klass|
@@ -23,6 +35,11 @@ module Brut
23
35
  end
24
36
  end
25
37
  end
38
+
39
+ # Implement this to create your seed data.
40
+ def seed!
41
+ raise Brut::Framework::Errors::AbstractMethod
42
+ end
26
43
  end
27
44
  end
28
45
  end
@@ -1,3 +1,4 @@
1
- class Brut::BackEnd::Sidekiq::Middlewares::Server
1
+ # Useful server middlewares for Sidekiq
2
+ module Brut::BackEnd::Sidekiq::Middlewares::Server
2
3
  autoload(:FlushSpans, "brut/back_end/sidekiq/middlewares/server/flush_spans")
3
4
  end
@@ -1,3 +1,4 @@
1
- class Brut::BackEnd::Sidekiq::Middlewares
1
+ # Useful middlewares for Sidekiq jobs
2
+ module Brut::BackEnd::Sidekiq::Middlewares
2
3
  autoload(:Server, "brut/back_end/sidekiq/middlewares/server")
3
4
  end
@@ -1,3 +1,4 @@
1
- class Brut::BackEnd::Sidekiq
1
+ # Namespace for Sidekiq-related support.
2
+ module Brut::BackEnd::Sidekiq
2
3
  autoload(:Middlewares, "brut/back_end/sidekiq/middlewares")
3
4
  end
@@ -1,3 +1,7 @@
1
- class Brut::BackEnd::Validators
1
+ # Namespace for back-end validation support. Note that in Brut, validators
2
+ # are not a mechanism for ensuring data integrity. Validators are for helping
3
+ # a website visitor or app user to understand data entry mistakes. To ensure
4
+ # data integrity, use your databases constraints and other features.
5
+ module Brut::BackEnd::Validators
2
6
  autoload(:FormValidator, "brut/back_end/validators/form_validator")
3
7
  end
data/lib/brut/back_end.rb CHANGED
@@ -1,5 +1,7 @@
1
- # The _back end_ of a Brut app is where your app's business logic and database are managed. While the bulk of your Brut app's code
2
- # will be in the back end, Brut is far less prescriptive about how to manage that than it is the front end.
1
+ # The _back end_ of a Brut app is where your app's business logic and
2
+ # database are managed. While the bulk of your Brut app's code
3
+ # will be in the back end, Brut is far less prescriptive about how to manage
4
+ # that than it is the front end.
3
5
  module Brut::BackEnd
4
6
  autoload(:Validators, "brut/back_end/validator")
5
7
  autoload(:Sidekiq, "brut/back_end/sidekiq")
data/lib/brut/cli.rb CHANGED
@@ -6,9 +6,10 @@ module Brut
6
6
  # that your CLI will respond to. See {Brut::CLI::app}.
7
7
  module CLI
8
8
 
9
- # Execute your CLI based on its command line invocation. You would call this method inside the executable file placed in `bin/`
10
- # in your project. For example, if you have `YourApp::CLI::CleanOldFiles` and you wish to execute it via `bin/clean-files`, you'd
11
- # create `bin/clean-files` like so:
9
+ # Execute your CLI based on its command line invocation. You would call this method inside the
10
+ # executable file placed in `bin/` in your project.
11
+ # For example, if you have `YourApp::CLI::CleanOldFiles` and you wish to execute
12
+ # it via `bin/clean-files`, you'd create `bin/clean-files` like so:
12
13
  #
13
14
  # ```
14
15
  # #!/usr/bin/env ruby
@@ -1,7 +1,3 @@
1
- # Because FactoryBot 6.4.6 has a bug where it is not properly
2
- # requiring active support, active supporot must be required first,
3
- # then factory bot. When 6.4.7 is released, this can be removed. See Gemfile
4
- require "active_support"
5
1
  require "factory_bot"
6
2
  require "faker"
7
3
 
@@ -17,6 +13,5 @@ class Brut::FactoryBot
17
13
  to_create { |instance| instance.save }
18
14
  end
19
15
  FactoryBot.find_definitions
20
-
21
16
  end
22
17
  end
@@ -1,9 +1,10 @@
1
1
  # An "App" in Brut paralance is the collection of source code and configuration that is needed to operate
2
2
  # a website. This includes everything needed to serve HTTP requests, but also includes ancillary
3
- # tasks and any related files required for the app to exist and function. Your app will have an `App` class that subclasses this
4
- # class.
3
+ # tasks and any related files required for the app to exist and function.
4
+ # Your app will have an `App` class that subclasses this class.
5
5
  #
6
- # When your app is initialized, Brut will have been configured, but access to internal resources may not be available. It is here
6
+ # When your app is initialized, Brut will have been configured,
7
+ # but access to internal resources may not be available. It is here
7
8
  # that you can override configuration values or do any other setup before everything boots.
8
9
  class Brut::Framework::App
9
10
  include Brut::Framework::Errors
@@ -18,7 +19,8 @@ class Brut::Framework::App
18
19
  # actions where an app needs to exist inside some organizational context.
19
20
  def organization = id
20
21
 
21
- # Call this in your app's definition to define your app's routes. The contents of the block will be evaluated in the context of
22
+ # Call this in your app's definition to define your app's routes.
23
+ # The contents of the block will be evaluated in the context of
22
24
  # {Brut::SinatraHelpers::ClassMethods}, and the methods there are generally the ones you should be calling.
23
25
  #
24
26
  # You can call this multiple times and the routes will be concatenated together.
@@ -31,6 +33,69 @@ class Brut::Framework::App
31
33
  end
32
34
  end
33
35
 
36
+ # Call this to specify what happens when an unhandled exception occurs. You may call this mulitple times,
37
+ # however note that if an error is caught that matches more than one block's condition, the one that is called
38
+ # will be the first one declared.
39
+ #
40
+ # The only deviation from this rule is when you call this
41
+ # without any condition. Doing that establishes the behavior for a "catch all" handler, which is
42
+ # only called when no other configured block can handle the exception. You can declare
43
+ # this at any time. **Do note** the "catch all" handler is more of a best effort. Brut is currently
44
+ # based on Sinatra which provides no way to arbitrarily catch all exceptions. What Brut does here is to
45
+ # explicitly catch the range of http status codes from 400 to 999.
46
+ #
47
+ # Note that Brut will record the exception via OpenTelemetry so you should not do this in your handlers. It
48
+ # would be preferable to instead record an event if you want to have observability from your error handlers.
49
+ #
50
+ # @param [Class|Integer|Range<Integer>] condition if given this specifies the conditions under which the given
51
+ # block will handle the error. If omitted, this block will handle any error that doesn't have a more
52
+ # specific handler configured. Meaning of values:
53
+ # * A class - this is an exception class that, if caught, triggers the handler
54
+ # * An integer - this is an HTTP status code that, if returned, triggers the handler
55
+ # * A range of integers - this is a range of HTTP status codes that, if returned, triggers the handler
56
+ # @yield [Exception] the block is given two named parameters: `exception:` and `http_status_code:`. Your block
57
+ # can declare both, either, or none. Any that are declared will be given values. At least one
58
+ # will be non-`nil`, however are encouraged to code defensively inside this block.
59
+ # @yieldparam [Exception] exception: the exception that was raised. This will be `nil`
60
+ # if the error was caused by an HTTP status code.
61
+ # @yieldparam [Integer] http_status_code: the HTTP status code that was returned. If `exception:` is
62
+ # not `nil`, this value is highly likely to be 500.
63
+ # @yieldreturn The block should return a valid Rack response. For now.
64
+ def self.error(condition=:catch_all, &block)
65
+ @error_blocks ||= {}
66
+ if block.nil?
67
+ raise ArgumentError, "You must provide a block to error"
68
+ end
69
+ parameters = block.parameters.reject { |type,name|
70
+ type == :keyreq && [ :http_status_code, :exception ].include?(name)
71
+ }
72
+ if parameters.any?
73
+ messages = parameters.map { |type,name|
74
+ case type
75
+ when :keyreq
76
+ "required keyword parameter '#{name}:'"
77
+ when :key
78
+ "optional keyword parameter '#{name}:'"
79
+ when :rest
80
+ "rest parameter '#{name}'"
81
+ when :opt
82
+ "optional parameter '#{name}'"
83
+ when :req
84
+ "required parameter '#{name}'"
85
+ else
86
+ "unknown parameter '#{name}'"
87
+ end
88
+ }
89
+ raise ArgumentError, "Your error handler block may only accept exception: and http_status_code: as required keyword parameters. The following parameters were found:\n #{messages.join("\n ")}"
90
+ end
91
+ if @error_blocks[condition]
92
+ raise ArgumentError, "You have already configured error handling for condition '#{condition.to_s}'"
93
+ end
94
+ @error_blocks[condition] = block
95
+ end
96
+
97
+ def self.error_blocks = @error_blocks || {}
98
+
34
99
  # Add a Rack middleware to your app. Middlewares are configured in the order in which you call this method.
35
100
  #
36
101
  # @param [Class] middleware a class that implements [Rack Middleware](https://github.com/rack/rack/blob/main/SPEC.rdoc).
@@ -81,7 +146,7 @@ class Brut::Framework::App
81
146
  # code required *after* Brut has been set up and started. You can rely on the
82
147
  # database being available. Any attempts to override configuration values
83
148
  # may not succeed. This is called after the framework has booted, but before
84
- # your apps' routes are set up.
149
+ # your app's routes are set up.
85
150
  def boot!
86
151
  end
87
152
 
@@ -1,11 +1,13 @@
1
1
  require_relative "project_environment"
2
2
  require "pathname"
3
3
 
4
- # Holds configuration for the framework and your app. In general, you should not interact with this class, however it's source code
5
- # is a good reference for what is configured by default by Brut.
4
+ # Holds configuration for the framework and your app. In general, you
5
+ # should not interact with this class, however it's source code is a good
6
+ # reference for what is configured by default by Brut.
6
7
  class Brut::Framework::Config
7
8
 
8
- # Configures all defaults. In general, this attempts to be lazy in setting things up, so calling this should not attempt to make a
9
+ # Configures all defaults. In general, this attempts to be lazy in
10
+ # setting things up, so calling this should not attempt to make a
9
11
  # connection to your database.
10
12
  def configure!
11
13
  Brut.container do |c|
@@ -21,7 +21,8 @@ end
21
21
  #
22
22
  # There is no namespacing/hierarchy.
23
23
  #
24
- # In general, you should not create instances of this class, but you may need to access it via {Brut.container} in order to obtain
24
+ # In general, you should not create instances of this class, but you may
25
+ # need to access it via {Brut.container} in order to obtain
25
26
  # configuration values or set your own.
26
27
  class Brut::Framework::Container
27
28
  def initialize
@@ -92,7 +93,7 @@ class Brut::Framework::Container
92
93
  end
93
94
 
94
95
  # Called by your app to override an existing value. The value must be overridable (see {#store}). Generally, you should call this
95
- # in the initializer of your {Brut::Framework::App} subclass. Calling this after the fact may not have the affect you want.
96
+ # in the initializer of your {Brut::Framework::App} subclass. Calling this after the fact may not have the effect you want.
96
97
  #
97
98
  # @param [String|Symbol] name name of the value to override. Will be coerced to a String. This name must have been previously
98
99
  # configured.
@@ -1,7 +1,7 @@
1
1
  module Brut
2
2
  module Framework
3
- # Include this in your class to access some helpful methods that throw commonly-needed
4
- # errors
3
+ # Namespace for Brut-specific error classes, and a holder of several error-related convienience
4
+ # methods. Include this module to gain access to those methods.
5
5
  module Errors
6
6
  autoload(:Bug,"brut/framework/errors/bug")
7
7
  autoload(:NotImplemented,"brut/framework/errors/not_implemented")
@@ -10,7 +10,12 @@ module Brut
10
10
  autoload(:MissingConfiguration,"brut/framework/errors/missing_configuration")
11
11
  autoload(:AbstractMethod,"brut/framework/errors/abstract_method")
12
12
  autoload(:NoClassForPath,"brut/framework/errors/no_class_for_path")
13
- # Raises {Brut::Framework::Errors::Bug}
13
+ # Raises {Brut::Framework::Errors::Bug}, used to indicate a codepath is a bug. "But, why write a
14
+ # bug in the first place?" you may be asking. Sometimes, a code path exists, but external factors
15
+ # mean that it should never be executed. Or, sometimes an API can be mis-used, but the current
16
+ # state of the system is such that it would never be misused.
17
+ #
18
+ # This method allows you to indicate such situations and provide a meaningful explanation.
14
19
  #
15
20
  # @param message Message to include in the error
16
21
  # @raise [Brut::Framework::Errors::Bug]
@@ -18,7 +23,10 @@ module Brut
18
23
  raise Brut::Framework::Errors::Bug,message
19
24
  end
20
25
 
21
- # Raises {Brut::Framework::Errors::AbstractMethod}
26
+ # Raises {Brut::Framework::Errors::AbstractMethod}, which is useful if you need to document
27
+ # a method that a subclass must implement, but for which there is no useful default
28
+ # implementation.
29
+ #
22
30
  # @raise [Brut::Framework::Errors::AbstractMethod]
23
31
  def abstract_method!
24
32
  raise Brut::Framework::Errors::AbstractMethod
@@ -116,8 +116,69 @@ class Brut::Framework::MCP
116
116
  @sinatra_app = Class.new(Sinatra::Base)
117
117
  @sinatra_app.include(Brut::SinatraHelpers)
118
118
 
119
+ safely_record_exception = ->(exception,http_status_code) {
120
+ begin
121
+ if exception.nil?
122
+ Brut.container.instrumentation.add_event("error triggered without exception", http_status_code:)
123
+ else
124
+ Brut.container.instrumentation.record_exception(exception, http_status_code:)
125
+ end
126
+ rescue => ex
127
+ begin
128
+ SemanticLogger[self.class].error(
129
+ "Error recording exception",
130
+ original_excerption: exception,
131
+ exception: ex)
132
+ rescue => ex2
133
+ $stderr.puts "While handling an error recording an exception, we get another error from SemanticLogger." + [
134
+ [ :original_exception, exception, ].join(": "),
135
+ [ :exception_from_recording_exception, ex, ].join(": "),
136
+ [ :exception_from_semantic_logger, ex2 ].join(": "),
137
+
138
+ ].join(", ")
139
+ end
140
+ end
141
+ }
142
+
143
+ @app.class.error_blocks.each do |condition,block|
144
+ puts "Setting up error handling for #{condition}"
145
+ if condition != :catch_all
146
+ @sinatra_app.error(condition) do
147
+ puts "Error block triggered"
148
+ exception = request.env["sinatra.error"]
149
+ safely_record_exception.(exception, response.status)
150
+ block_args = if exception
151
+ puts "Exception: #{exception.class}"
152
+ { exception: }
153
+ else
154
+ puts "HTTP: #{response.status}"
155
+ { http_status_code: response.status }
156
+ end
157
+ block.(**block_args)
158
+ end
159
+ end
160
+ end
161
+ if @app.class.error_blocks[:catch_all]
162
+ block = @app.class.error_blocks[:catch_all]
163
+ block_args = block.parameters.map { |(type,name)|
164
+ [ name, nil ]
165
+ }.to_h
166
+ @sinatra_app.error(400..999) do
167
+ exception = request.env["sinatra.error"]
168
+ safely_record_exception.(exception, response.status)
169
+ if block_args.key?(:exception)
170
+ block_args[:exception] = exception
171
+ end
172
+ if block_args.key?(:http_status_code)
173
+ block_args[:http_status_code] = response.status
174
+ end
175
+ block.(**block_args)
176
+ end
177
+ else
178
+ end
179
+
119
180
  message = if Brut.container.project_env.development?
120
- "Form submission did not include an authenticity token. All forms must include one. To add one, use the `form_tag` helper, or include <%= component(Brut::FrontEnd::Components::Inputs::CsrfToken) %> somewhere inside your <form> tag"
181
+ "Form submission did not include an authenticity token. All forms must include one. To add one, use the `form_tag` helper, or include Brut::FrontEnd::Components::Inputs::CsrfToken somewhere inside your <form> tag"
121
182
  else
122
183
  "Forbidden"
123
184
  end
@@ -1,5 +1,7 @@
1
- # Manages the interpretation of dev/test/prod. The canonical instance is available via `Brut.container.project_env`. Generally, you
2
- # should avoid basing logic on this, or at least contain the conditional behavior to the configuration values. But, you do you.
1
+ # Manages the interpretation of dev/test/prod. The canonical instance is available
2
+ # via `Brut.container.project_env`. Generally, you
3
+ # should avoid basing logic on this, or at least contain the conditional behavior
4
+ # to the configuration values. But, you do you.
3
5
  class Brut::Framework::ProjectEnvironment
4
6
  # Create the project environment based on the string
5
7
  # @param [String] string_value value from e.g. `ENV["RACK_ENV"]` to use to set the environment
@@ -21,6 +23,8 @@ class Brut::Framework::ProjectEnvironment
21
23
  # @return [true|false] true is this is production
22
24
  def production? = @value == "production"
23
25
 
26
+ def staging? = raise "Staging is a lie, please consider feature flags or literally any other way to manage in-development features of your app. I promise you, you will regret ever having to do anything with a staging server"
27
+
24
28
  # @return [String] the string value (which should be suitable for the constructor)
25
29
  def to_s = @value
26
30
  end
@@ -1,5 +1,5 @@
1
1
  module Brut
2
- # The Framework module holds a lot of Brut's internals, or classes that cut across the back end and front end.
2
+ # Namespace for Brut's internals as well as classes that aren't strictly front or back end.
3
3
  module Framework
4
4
  autoload(:App,"brut/framework/app")
5
5
  autoload(:Config,"brut/framework/config")
@@ -16,21 +16,19 @@ module Brut::FrontEnd::Components
16
16
  end
17
17
 
18
18
  # A Component is the top level class for managing the rendering of
19
- # content. A component is essentially an ERB template and a class whose
20
- # instance servces as it's binding. It is very similar to a View Component, though
21
- # not quite as fancy.
19
+ # content. It is a Phlex component with additional features.
20
+ # Components are the primary mechanism for managing view complexity and managing
21
+ # markup re-use in Brut.
22
22
  #
23
- # When subclassing this to create a component, your initializer's signature will determine what data
24
- # is required for your component to work. It can be anything, just keep in mind that any page or component
25
- # that uses your component must be able to provide those values.
23
+ # To create a component, subclass this class (or, more likely, your app's `AppComponent`) and
24
+ # provide an initializer that accepts keyword arguments. The names of these arguments will be used to locate the
25
+ # values that Brut will pass in when creating your component object.
26
26
  #
27
- # If your component does not override {#render} (which, generally, it won't), an ERB file is expected to exist alongside it in the
28
- # app. For example, if you have a component named `Auth::LoginButtonComponent`, it would expected to be in
29
- # `app/src/front_end/components/auth/login_button_component.rb`. Thus, Brut will also expect
30
- # `app/src/front_end/components/auth/login_button_component.html.erb` to exist as well. That ERB file is used with an instance of your
31
- # component's class to render the component's HTML.
27
+ # Consult Brut's documentation on keyword injection to know what values you may use and how values are located.
28
+ #
29
+ # Becuase this is a Phlex component, you must implement `view_template` and make calls to Phlex's API to create
30
+ # the markup for your component.
32
31
  #
33
- # @see Brut::FrontEnd::Component::Helpers
34
32
  class Brut::FrontEnd::Component < Phlex::HTML
35
33
 
36
34
  include Brut::Framework::Errors
@@ -53,6 +51,13 @@ class Brut::FrontEnd::Component < Phlex::HTML
53
51
  register_element :brut_tabs
54
52
  register_element :brut_tracing
55
53
 
54
+ # Inline an SVG that is part of your app.
55
+ #
56
+ # @param [String] svg path to the SVG file, relative to where SVGs are
57
+ # stored, which is `app/src/front_end/svgs` or where `Brut.container.svg_locator` is
58
+ # looking
59
+ #
60
+ # @see Brut::FrontEnd::InlineSvgLocator
56
61
  def inline_svg(svg)
57
62
  Brut.container.svg_locator.locate(svg).then { |svg_file|
58
63
  File.read(svg_file)
@@ -61,19 +66,27 @@ class Brut::FrontEnd::Component < Phlex::HTML
61
66
  }
62
67
  end
63
68
 
69
+ # Include a {Brut::FrontEnd::Components::TimeTag} in your markup.
64
70
  def time_tag(timestamp:nil,**component_options, &contents)
65
71
  args = component_options.merge(timestamp:)
66
72
  render Brut::FrontEnd::Components::TimeTag.new(**args,&contents)
67
73
  end
68
74
 
75
+ # Include a {Brut::FrontEnd::Components::FormTag} in your markup.
69
76
  def form_tag(**args, &block)
70
77
  render Brut::FrontEnd::Components::FormTag.new(**args,&block)
71
78
  end
72
79
 
80
+ # Include a component in your markup that you would like Brut to instantiate.
81
+ # This will use keyword injection to create the component, which means that if the component
82
+ # doesn't require any data from this component, you do not need to pass through those values.
83
+ # For example, you may have a component that renders the flash message. To avoid requiring your component to
84
+ # be passed the flash, a global component can be injected with it from Brut.
73
85
  def global_component(component_klass)
74
86
  render Brut::FrontEnd::RequestContext.inject(component_klass)
75
87
  end
76
88
 
89
+ # include a {Brut::FrontEnd::Components::ConstraintViolations} in your markup.
77
90
  def constraint_violations(form:, input_name:, index: nil, message_html_attributes: {}, **html_attributes)
78
91
  render(
79
92
  Brut::FrontEnd::Components::ConstraintViolations.new(
@@ -98,9 +111,19 @@ class Brut::FrontEnd::Component < Phlex::HTML
98
111
  )
99
112
  end
100
113
 
114
+ # The name of this component, used for debugging and other purposes. Do not
115
+ # override this.
101
116
  def self.component_name = self.name
117
+
118
+ # Calls {.component_name} as a convienience. Do not override this.
102
119
  def component_name = self.class.component_name
103
120
 
121
+ # For page components (components that are private/nested to a page), this returns
122
+ # the name of the page in which they are nested. This is mostly useful for
123
+ # locating page-specific I18n translations.
124
+ #
125
+ # @raise If this component is not nested inside a page
126
+ # @see Brut::I18n::BaseMethods#t
104
127
  def page_name
105
128
  @page_name ||= begin
106
129
  page = self.class.name.split(/::/).reduce(Module) { |accumulator,class_path_part|
@@ -17,7 +17,7 @@
17
17
  # Note that if you are using `<brut-form>` then `<brut-cv>` elements will be inserted into the `<brut-cv-messages>` element, however
18
18
  # they will not have the `server-side` attribute.
19
19
  #
20
- # You will most commonly use this component via {Brut::FrontEnd::Component::Helpers#constraint_violations}.
20
+ # You will most commonly use this component via {Brut::FrontEnd::Component#constraint_violations}.
21
21
  class Brut::FrontEnd::Components::ConstraintViolations < Brut::FrontEnd::Component
22
22
  # Create a new ConstraintViolations component
23
23
  #
@@ -1,4 +1,4 @@
1
- # Represents a `<form>` HTML element that includes a CSRF token as needed. You likely want to use this class via the {Brut::FrontEnd::Component::Helpers#form_tag} method.
1
+ # Represents a `<form>` HTML element that includes a CSRF token as needed. You likely want to use this class via the {Brut::FrontEnd::Component#form_tag} method.
2
2
  class Brut::FrontEnd::Components::FormTag < Brut::FrontEnd::Component
3
3
  # Creates the form surrounding the contents of the block yielded to it. If the form's action is a POST, it will include a CSRF token.
4
4
  # If the form's action is GET, it will not.
@@ -1,5 +1,5 @@
1
1
  # Renders a hidden field for a form that contains the current CSRF token. You only need
2
- # to use this directly if you are building a form without {Brut::FrontEnd::Component::Helpers#form_tag}.
2
+ # to use this directly if you are building a form without {Brut::FrontEnd::Component#form_tag}.
3
3
  class Brut::FrontEnd::Components::Inputs::CsrfToken < Brut::FrontEnd::Components::Input
4
4
  def initialize(csrf_token:)
5
5
  @csrf_token = csrf_token
@@ -41,7 +41,7 @@ class Brut::FrontEnd::Components::Inputs::TextField < Brut::FrontEnd::Components
41
41
  default_html_attributes[:value] = (index || true).to_s
42
42
  default_html_attributes[:checked] = value == "true"
43
43
  else
44
- default_html_attributes[:value] = value.to_s
44
+ default_html_attributes[:value] = value.nil? ? nil : value.to_s
45
45
  end
46
46
  if !form.new? && !input.valid?
47
47
  default_html_attributes["data-invalid"] = true
@@ -1,4 +1,4 @@
1
- # Renders a date or timestamp accessibly, using the `<time>` element. Likely you will use this via the {Brut::FrontEnd::Component::Helpers#time_tag} method. This will account for the current request's time zone. See {Clock}.
1
+ # Renders a date or timestamp accessibly, using the `<time>` element. Likely you will use this via the {Brut::FrontEnd::Component#time_tag} method. This will account for the current request's time zone. See {Clock}.
2
2
  class Brut::FrontEnd::Components::TimeTag < Brut::FrontEnd::Component
3
3
  include Brut::I18n::ForHTML
4
4
  # Creates the component
@@ -1,3 +1,19 @@
1
+ # A layout is common HTML that surrounds different pages. For example, it would hold your
2
+ # DOCTYPE, `<head>`, and possibly any common `<body>` elements that every page needs.
3
+ #
4
+ # A layout is a Phlex component but it must contain a call to `yield` somewhere in the
5
+ # implementation of `view_template`.
6
+ #
7
+ # This base class contains helper methods needed for implementing a layout.
1
8
  class Brut::FrontEnd::Layout < Brut::FrontEnd::Component
9
+ # Get the actual path of an asset managed by Brut. This handles
10
+ # locating the asset's URL as well as ensuring the hash is properly
11
+ # inserted into the filename.
12
+ #
13
+ # @param [String] path the path to an asset, such as `/css/styles.css`.
14
+ #
15
+ # @return [String] the actual path to the current version of that asset.
16
+ #
17
+ # @see Brut::FrontEnd::AssetPathResolver
2
18
  def asset_path(path) = Brut.container.asset_path_resolver.resolve(path)
3
19
  end