Commit Graph

144 Commits

Author SHA1 Message Date
fatkodima f48bbff32c Expose `assert_queries_match` and `assert_no_queries_match` assertions 2023-12-21 01:30:16 +02:00
Jean Boussier 3881518c47
Merge pull request #50281 from p8/activerecord/assert-queries
Expose `assert_queries` and `assert_no_queries` assertions
2023-12-12 00:29:45 +01:00
Petrik 8392c54e73 Expose `assert_queries` and `assert_no_queries` assertions
To assert the expected number of queries are made, Rails internally uses
`assert_queries` and `assert_no_queries`. These assertions can be
useful in applications as well.

By extracting these assertions to a module, the assertions can be
included where required.
These assertions are added to `ActiveSupport::TestCase` when
ActiveRecord is defined.

ActiveStorage, ActionView and ActionText are using this module now as
well, instead of duplicating the implementation.
The internal ActiveRecord::TestCase, used for testing ActiveRecord,
implements these assertions as well. However, these are slighlty more
advanced/complex and use the SQLCounter class. To keep things simple,
for now this implementation isn't used.
2023-12-11 12:31:16 +01:00
Chedli Bourguiba c3b821e6d8 Take AR affixes into account for Action Text database models 2023-12-09 11:05:52 +01:00
Sean Doyle 16c28d0a09 Add test coverage for `rich_text_area` helper
Follow-up to [#50252][]

Similar to the reliance on a `FormBuilder` in the helper methods
documentation examples, the template test coverage for `#rich_text_area`
relied on invocations through a `FormBuilder` instance.

This commit adds explicit coverage for calling the `#rich_text_area`
helper method directly with both an `object_name` and `method_name`
positional arguments.

[#50252]: https://github.com/rails/rails/pull/50252
2023-12-03 14:53:44 -05:00
Max Notarangelo 185c19c5ae fix typo in production initializer generator
And put "info" in quotes.
2023-11-16 15:00:07 -08:00
Matias Grunberg 2dbcb3df33 fix using actiontext.js in sprocket
actiontext.js is compiled as ESM bundle instead of UMD bundle.
This leads to issues when trying to use ActionText with sprockets because the ESM bundle declares variables like they are scoped to the file but sprockets will see them as scoped globally.
This is a problem, in particular, if you want to mix actiontext with
turbo-rails.

The problem got introduced in https://github.com/rails/rails/pull/46447.
I traced valid compilation back to
https://github.com/rails/rails/pull/42895.

This commit mimic changes made in
https://github.com/rails/rails/pull/42895 to ActiveStorage:
   Retains app/assets/javascripts/actiontext.js as a UMD package for backwards compatibility with
   bundling in the asset pipeline, but also adds app/assets/javascripts/actiontext.esm.js for use
   with ESM via importmap in the browser.
2023-11-07 00:36:53 -03:00
Jonathan Hefner a93771cb0d Add JavascriptPackageTest for Action Text
This mirrors the existing `JavascriptPackageTest` tests for UJS, Action
Cable, and Active Storage.
2023-11-05 15:28:14 -06:00
Adrian Hirt f0a03bd899 Remove `config.public_file_server.enabled` from generators
Remove the option `config.public_file_server.enabled` from the generators for all environments, as the value is the same in all environments.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-11-03 11:34:37 -05:00
Jonathan Hefner dd428f1ef1 Present config.public_file_server.enabled as opt-out
Follow-up to #47137.

Since `config.public_file_server.enabled` is true by default, this
commit changes the `config/environments/production.rb` template to
present the setting as an opt-out.
2023-10-30 11:54:26 -05:00
Nikita Vasilevsky 19f8ab2e7d
[Tests only] Enable `Minitest/AssertPredicate` rule 2023-10-13 19:26:47 +00:00
Rafael Mendonça França fb6c6007d0
Development of Rails 7.2 starts now
🎉
2023-09-27 03:59:11 +00:00
a5-stable 7809146a03 fix ActionText::Attachable#as_json to allow options 2023-09-27 00:51:18 +09:00
Mike Dalessio e8137c527d
Fix ActionText::ContentHelper allowed tags and attrs
which were being set to the HTML4 defaults before the sanitizer
configuration could be applied.

Also, backfill some light tests for sanitization.

Related to #48644
2023-07-17 11:43:51 -04:00
Mike Dalessio 55bca6b8e8
Update Action Text to use HTML5 when available
The change from `#clone` to `#dup` is necessary to work around an
issue in Nokogiri where `#clone` is not defined properly for HTML5
fragment and the fragment does not have a parent Document. `#dup`
behaves the way we expect, so this should be fine.
2023-06-19 15:59:59 -04:00
Jonathan Hefner 23243eb283
Merge pull request #46476 from justinperkins/fix/i18n-reserved-words-comments
Update comments in all three en.yml files relating to booleans
2023-05-24 13:58:38 -05:00
Justin Perkins ad1d2bed4a Clarify YAML boolean caveat in en.yml template
This clarifies that the boolean interpretation (1) is due to YAML rather
than I18n, (2) is case insensitive, and (3) affects both keys and
values.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-05-24 13:35:53 -05:00
Jon Dufresne e28f147329 Make the test environment show rescuable exceptions in responses
Background
----------

During integration tests, it is desirable for the application to respond
as closely as possible to the way it would in production. This improves
confidence that the application behavior acts as it should.

In Rails tests, one major mismatch between the test and production
environments is that exceptions raised during an HTTP request (e.g.
`ActiveRecord::RecordNotFound`) are re-raised within the test rather
than rescued and then converted to a 404 response.

Setting `config.action_dispatch.show_exceptions` to `true` will make the
test environment act like production, however, when an unexpected
internal server error occurs, the test will be left with a opaque 500
response rather than presenting a useful stack trace. This makes
debugging more difficult.

This leaves the developer with choosing between higher quality
integration tests or an improved debugging experience on a failure.

I propose that we can achieve both.

Solution
--------

Change the configuration option `config.action_dispatch.show_exceptions`
from a boolean to one of 3 values: `:all`, `:rescuable`, `:none`. The
values `:all` and `:none` behaves the same as the previous `true` and
`false` respectively. What was previously `true` (now `:all`) continues
to be the default for non-test environments.

The new `:rescuable` value is the new default for the test environment.
It will show exceptions in the response only for rescuable exceptions as
defined by `ActionDispatch::ExceptionWrapper.rescue_responses`. In the
event of an unexpected internal server error, the exception that caused
the error will still be raised within the test so as to provide a useful
stack trace and a good debugging experience.
2023-05-17 06:30:28 -07:00
zzak f397f47212
Use double quotes for escaping yaml (re #47983) 2023-05-16 20:34:40 +09:00
Joe Dupuis fc839e980b Fix a race condition in Action Text's test suite
`assert_queries` in Action Text's test helpers can detect queries from
a previous test if background jobs are allowed to run during its
execution.

Changing the queue_adapter to the test adapter for all tests using the
helper ensure no jobs can run during its execution.
2023-05-07 16:44:08 -07:00
Josh Brito 43ec5737c7 Update comment to reference values with single quotes as opposed to double 2023-04-18 21:32:37 -07:00
Rafael Mendonça França 6aaa9db647
Merge pull request #46101 from swanson/swanson/missing-attachable-partial
Allow Attachables to override default template when attachment is missing
2023-03-25 12:49:54 -04:00
Andrew Novoselac 6902cbce1b Introducs `TestFixtures#fixture_paths`.
Multiple fixture paths can now be specified using the `#fixture_paths` accessor.
2023-03-14 19:02:56 -04:00
Rafael Mendonça França 20d87500a4
Merge pull request #46126 from alexandreruban/fix-as-json-action-text-attachables
Fix ActionText::Attachable#as_json
2023-03-06 17:32:27 -05:00
Alexandre Ruban 6c2132b332
Fix ActionText::Attachable#as_json
Before this commit, using ActionText and calling `#as_json` on a
non-persisted `ActiveStorage::Blob` raised an error.

This is because `ActionText::Attachable` is included in
`ActiveStorage::Blob` and overrides the `#as_json` method to
expose a global signed id. However, a global signed id can only
be generated on a persisted instance.

This commit fixes the issue by making sure the blob is persisted
before exposing the global signed id.
2023-03-06 13:55:01 +01:00
Rafael Mendonça França e78ed07e00
Merge pull request #47259 from skipkayhil/hm-regenerate-dummies
Full refresh of framework dummy applications
2023-03-01 18:08:58 -05:00
Hartley McGuire ba028a48f6
Fix Action Mailbox, Action Text strict warnings
These appear to have been missed when the change was made in
d917896f45
2023-02-23 17:34:55 -05:00
Hartley McGuire 2e5d060537
Remove Webpacker files in Action Text dummy app
These are leftover after Webpacker was replaced with importmaps in
89a24d6216
2023-02-18 17:51:27 -05:00
Hartley McGuire 9795eb9194
Regenerate Action Text dummy app
Generated using the following script and manually reviewed after:
skipkayhil/rails-bin@38e2aacb1b
2023-02-18 17:51:27 -05:00
Matt Swanson fbf0145f19 Allow Attachables to override default template when attachment is missing 2023-02-13 10:45:12 -05:00
David Heinemeier Hansson 2b1fa89e44
Log to stdout in production by default (#47138)
* Logging to a file doesn't make sense in production

You're going to run out of space, and it doesn't play well with containers. Either you log to STDOUT, and let your container setup aggregate the logs, or you'll be switching to syslogger or whatever. You won't be logging to a file in production any more.

* Remove from Dockerfile too

* Did not mean to change this default

But we should make it easy to see how to change it.

* Restore what we had
2023-01-25 21:10:22 +01:00
David Heinemeier Hansson e8f481b924
Turn on static file server by default (#47137)
Modern deployments don't need NGINX/Apache when using CDNs or caching LBs in front. This means file serving works out of the box too.
2023-01-25 18:19:05 +01:00
Alex Ghiculescu e18c23fc2b Make `raise_on_missing_translations` raise on any missing translation
Fixes https://github.com/rails/rails/issues/47057
Superseeds https://github.com/rails/rails/pull/45361
2023-01-23 10:17:11 -07:00
Rafael Mendonça França 5c835bd669
Make explicit this config only affects controllers are views 2023-01-19 20:21:29 +00:00
Haroon Ahmed 89a24d6216 Fix action text tests by updating dummy app to use import maps instead
of webpacker.

Include the js and css in the application.html.erb which now shows the
rich text editor on the page. This allows the integration tests to run.

Fixes https://github.com/rails/rails/issues/46804
2023-01-08 22:33:30 +00:00
Sean Doyle 0e9013081e Revert "Focus editor after calling `fill_in_rich_text_area`"
Closes [rails/rails#46803][].

This reverts commit 67a4ac6c56.

Once the system test helper test suite executes in CI, we can
re-introduce the changes as part of [rails/rails#46807][].

[rails/rails#46803]: https://github.com/rails/rails/issues/46803#issuecomment-1364112967
[rails/rails#46807]: https://github.com/rails/rails/pull/46807
2022-12-23 14:19:25 -05:00
Kaíque Kandy Koga 0dc71acf39 Apply field_error_proc to rich_text_area fields 2022-12-22 11:52:30 -06:00
Jorge Manrubia 14cd0c4dee
Avoid unnecessary replacements when the node doesn't change
This represents a +2x performance optimization when the replacement
logic is based on some condition, and it returns the same unchanged
node when it wants to skip it:

 ```ruby
 html = <<~HTML
       <div>
         #{'<p>ignore me</p>' * 1000}
         #{'<p>replace me</p>' * 1000}
       </div>
 HTML

 content = content_from_html(html)

 replacement_example = -> do
   content.fragment.replace("p") do |node|
     if node.text =~ /replace me/
       "<p>replace me</p>"
     else
       node
     end
   end
 end

 current_implementation = -> do
   class ActionText::Fragment
     def replace(selector)
       update do |source|
         source.css(selector).each do |node|
           replacement_node = yield(node)
           node.replace(replacement_node.to_s)
         end
       end
     end
   end

   replacement_example.call
 end

 new_implementation = -> do
   class ActionText::Fragment
     def replace(selector)
       update do |source|
         source.css(selector).each do |node|
           replacement_node = yield(node)
           node.replace(replacement_node.to_s) if node != replacement_node
         end
       end
     end
   end

   replacement_example.call
 end

 Benchmark.ips do |x|
   x.report "Current implementation", &current_implementation
   x.report "New implementation", &new_implementation

   x.compare!
 end
 ```

 Results:

 ```
 Warming up --------------------------------------
 Current implementation
                          2.000  i/100ms
   New implementation     5.000  i/100ms
 Calculating -------------------------------------
   Current implementation
                          32.484  (±30.8%) i/s -    134.000  in   5.036419s
   New implementation     74.878  (±38.7%) i/s -    250.000  in   5.052168s

 Comparison:
   New implementation:       74.9 i/s
   Current implementation:       32.5 i/s - 2.31x  (± 0.00) slower
 ```
2022-12-17 22:39:51 +01:00
Jonathan Hefner 93038baad7 Use routes.default_url_options in AC::Renderer env
When a host is not specified for an `ActionController::Renderer`'s env,
the host and related options will now be derived from the routes'
`default_url_options` and `ActionDispatch::Http::URL.secure_protocol`.

For example, with:

  ```ruby
  Rails.application.default_url_options = { host: "rubyonrails.org" }
  Rails.application.config.force_ssl = true
  ```

Before:

  ```ruby
  ApplicationController.renderer.render inline: "<%= blog_url %>"
  # => "http://example.org/blog"
  ```

After:

  ```ruby
  ApplicationController.renderer.render inline: "<%= blog_url %>"
  # => "https://rubyonrails.org/blog"
  ```

As a consequence, Action Text attachment URLs rendered in a background
job (a la Turbo Streams) will now use `Rails.application.default_url_options`.

Fixes #41795.
Fixes hotwired/turbo-rails#54.
Fixes hotwired/turbo-rails#155.
2022-12-15 16:22:07 -06:00
David Heinemeier Hansson d18fc32999
Use storage/ instead of db/ for sqlite3 db files (#46699)
* Use storage/ instead of db/ for sqlite3 db files

db/ should be for configuration only, not data. This will make it easier to mount a single volume into a container for testing, development, and even sqlite3 in production.
2022-12-12 08:32:12 +01:00
Sean Doyle 67a4ac6c56 Focus editor after calling `fill_in_rich_text_area`
When System Tests call `fill_in_rich_text_area`, they interact with
`<trix-editor>` elements by changing the contents programmatically.

This is unlike how end-users will interact with the element. Overhauling
the test helper to more accurately reflect Real World usage would
require a sizable effort.

With that being said, leaving the `<trix-editor>` with focus after
populating its contents is a minor change that makes it a more genuine
recreation.
2022-12-09 09:48:27 -05:00
Eileen M. Uchitelle f002536a5d
Merge pull request #46271 from seanpdoyle/has-rich-text-strict-loading
Support `has_rich_text` with `strict_loading:`
2022-12-09 08:44:07 -05:00
Evan Brooks 8d3408e41a Update mailer templates meta tag mailer.html.erb 2022-11-14 21:38:07 -04:00
Sean Doyle 4a743b9e0a Support `has_rich_text` with `strict_loading:`
Expand the `has_rich_text` signature to accept a `strict_loading:`
value. Forward that value along to the `has_one` declaration made under
the hood. When omitted, `strict_loading:` will be set to the value of
the `strict_loading_by_default` class attribute (false by default).
2022-10-18 17:06:26 -04:00
Eike Send 5ba8aa5854
Facilitate use of any regular ERB in database.yml
Commit 37d1429ab1 introduced the DummyERB to avoid loading the environment when
running `rake -T`.

The DummyCompiler simply replaced all output from `<%=` with a fixed string and
removed everything else. This worked okay when it was used for YAML values.
When using `<%=` within a YAML key, it caused an error in the YAML parser,
making it impossible to use ERB as you would expect. For example a
`database.yml` file containing the following should be possible:

  development:
    <% 5.times do |i| %>
    shard_<%= i %>:
      database: db/development_shard_<%= i %>.sqlite3
      adapter: sqlite3
    <% end %>

Instead of using a broken ERB compiler we can temporarily use a
`Rails.application.config` that does not raise an error when configurations are
accessed which have not been set as described in #35468.

This change removes the `DummyCompiler` and uses the standard `ERB::Compiler`.
It introduces the `DummyConfig` which delegates all known configurations to the
real `Rails::Application::Configuration` instance and returns a dummy string for
everything else. This restores the full ERB capabilities without compromising on
speed when generating the rake tasks for multiple databases.

Deprecates `config.active_record.suppress_multiple_database_warning`.
2022-09-27 17:07:40 +02:00
Yasuo Honda 0b80408b24 Use `system(.., exception: true)` for dummy apps
Related to #46064
2022-09-20 22:53:05 +09:00
Jamis Buck 5f342cfdbe make sure rendering the content attachment actually works 2022-08-04 10:07:23 -06:00
Jamis Buck 9918ce4a78 linter appeasement 2022-08-03 08:19:41 -06:00
Jamis Buck 4499a3cdd0 update ContentAttachment so that it works with "content" attributes
this makes it possible for an application to embed markup in a document
that would otherwise be undesirable or expensive to process. For example,
an incoming email may include a complicated bit of DOM in a quote, and
while you don't want to have to process and rewrite it, you also don't want
to discard it; the content attribute of ContentAttachment allows you to
make that work.
2022-08-02 15:08:16 -06:00
Jonathan Hefner 471ab2347f Omit workers and preload_app! from Puma config
Since Puma 5.0 (puma/puma@05936689c8),
Puma will automatically set `workers` to `ENV["WEB_CONCURRENCY"] || 0`.
Additionally, if `ENV["WEB_CONCURRENCY"]` > 1, Puma will automatically
set `preload_app`.

This can lead to confusing scenarios for users who are unaware of this
behavior and have customized `config/puma.rb`.  For example, if a user
uncomments the `workers` and `preload_app!` directives, it is clear that
Puma will preload the app, and the number of workers can be configured
by setting `ENV["WEB_CONCURRENCY"]`.  If the user sets
`ENV["WEB_CONCURRENCY"]` > 1, but then changes their mind and removes
the `workers` or `preload_app!` directives *without* clearing
`ENV["WEB_CONCURRENCY"]`, Puma will still preload the app and launch
`ENV["WEB_CONCURRENCY"]` number of workers.  Similarly, if a user
uncomments *only* the `workers` directive and sets
`ENV["WEB_CONCURRENCY"]` > 1, Puma will preload the app even though the
`preload_app!` directive is still commented out.

To avoid such scenarios, this commit removes the commented-out `workers`
and `preload_app!` directives from the default `config/puma.rb`.

Also, to improve discoverability of available configuration options,
this commit adds a link to the Puma DSL documentation at the top of the
file.
2022-06-03 12:38:21 -05:00