Commit Graph

130 Commits

Author SHA1 Message Date
Sean Doyle 0d8b3f09af Provide guidance for renaming classes in polymorphic associations [ci skip]
Add guidance to the Association Basics and `.belongs_to` method
documentation to encourage the renaming of a model's Ruby class to
coincide with updates to the existing data in the database.

Since Action Text and Active Storage rely on polymorphic associations,
add similar warnings to their guides.

Co-authored-by: Petrik de Heus <petrik@deheus.net>
Co-authored-by: Stephen Hanson <s.hanson5@gmail.com>
Co-authored-by: zzak <zzakscott@gmail.com>
2023-12-08 09:19:21 -05: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
Sean Doyle 591d88aef4 Document Action Text Sanitization
Add documentation for `ActionText::RichText#to_s` and
`ActionText::Content#to_s` that demonstrates Action Text's ability to
sanitize and scrub its content.

Co-authored-by: Mike Dalessio <mike.dalessio@gmail.com>
Co-authored-by: Petrik de Heus <petrik@deheus.net>
2023-10-30 17:49:13 -04:00
Rafael Mendonça França fb6c6007d0
Development of Rails 7.2 starts now
🎉
2023-09-27 03:59:11 +00:00
Rafael Mendonça França e5386cb402
Preparing for 7.1.0.rc1 release 2023-09-27 03:08:31 +00:00
Rafael Mendonça França cf44b9e3b3
Merge pull request #49272 from a5-stable/attachable-as-json
Fix as_json behavior in ActionText::Attachable
2023-09-26 12:13:32 -04:00
a5-stable 7809146a03 fix ActionText::Attachable#as_json to allow options 2023-09-27 00:51:18 +09:00
Rafael Mendonça França 699dfdb426
Preparing for 7.1.0.beta1 release 2023-09-13 00:36:01 +00:00
Jason Meller 274bc97d63
Add Bun support (#49241)
* Add Bun support to `rails new -j` generator

* Add additional generation consideration for Bun

* Use development gems to test the whole workflow

* Remove custom gems from local testing

* Revert lock

* Revert errant custom gem declaration

* Fix linting errors

* Fix remnants of bad merge

* Always use latest bun

* Update actioncable/lib/rails/generators/channel/channel_generator.rb

Co-authored-by: Cadu Ribeiro <mail@cadu.dev>

* Update guides/source/working_with_javascript_in_rails.md

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>

* Only use the latest bun if nothing is specified

* Hardcode known good version

---------

Co-authored-by: Cadu Ribeiro <mail@cadu.dev>
Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
2023-09-12 16:55:27 -04:00
Ryuta Kamizono 97737abbeb Merge pull request #49201 from akhilgkrishnan/remove-webpacker-file
Remove unused webpacker related files
2023-09-09 07:44:54 +09:00
Akhil G Krishnan cafad7a23a
Remove unused webpacker related files 2023-09-08 11:22:09 +00:00
Mike Dalessio ebe9166535
Fix Action Text sanitizer initialization
Using `initializer` in the engine may run the block before application
initializers. Instead, use `config.after_initialize` to ensure that
application initializers take effect properly.

Also, don't bother deleting the config value, since that pattern isn't
needed here (as it is in other railties like action_view/railtie.rb).
2023-07-17 15:07:18 -04:00
Mike Dalessio 44d3b44d9d
Add config.action_text.sanitizer_vendor
Rails 7.1 defaults to Rails::HTML::Sanitizer.best_supported_vendor,
earlier configs will use Rails::HTML4::Sanitizer.

Related to #48523 / 55bca6b8.
2023-07-04 08:54:46 -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
zzak e3c73fd183
Replace all occurrences of '<tt>(\w+::\w+)</tt>' with '+$1+'
E.g.:

* <tt>ActiveRecord::Base</tt> -> +ActiveRecord::Base+

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
Co-authored-by: Petrik de Heus <petrik@deheus.net>
2023-05-25 06:52:32 +09:00
Petrik 71a89d5e29 Document more methods for ActionText::Attachable [ci-skip]
Document the `*_partial_path`, `attachable_sgid` and `as_json` methods.
2023-05-05 22:06:27 +02:00
Petrik de Heus 68b500e739
Merge pull request #48142 from p8/actiontext/document-actiontext-content
Some cleanups for ActionText documentation [ci-skip]
2023-05-05 21:26:55 +02:00
Petrik fdf9682301 Some cleanups for ActionText documentation [ci-skip]
Add some escaping of namespaces.
2023-05-05 21:12:10 +02:00
Petrik 344bb00eab Add documentation for ActionText::Attachment [ci-skip]
Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
2023-05-05 18:34:35 +02:00
Petrik de Heus 6a665a597d
Merge pull request #48133 from p8/actiontext/document-actiontext-content
Add documentation for ActionText::Content [ci-skip]
2023-05-05 18:28:05 +02:00
Petrik d6d57ae35a Document ActionText::Attachable [ci-skip] 2023-05-05 11:34:52 +02:00
Petrik 6f6d1f91bd Add documentation for ActionText::Content [ci-skip] 2023-05-05 10:23:39 +02:00
Petrik c09f5fa956 Add missing headers to Action Mailbox/Mailer/Text/View docs [ci-skip]
Having a h1 heading will improve SEO and makes things look more consistent.
2023-04-02 17:47:17 +02:00
Petrik de Heus dc0f20595d
Merge pull request #47717 from p8/docs/include-readmes
Include READMEs in main framework pages of the API documentation
2023-03-30 16:43:14 +02: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
Petrik 7c94708d24 Include READMEs in main framework pages of the API documentation
Currently when opening the main framework pages there is no introduction
to the framework. Instead we only see a whole lot of modules and the
`gem_version` and `version` methods.

By including the READMEs using the `:include:` directive each frameworks
has a nice introduction.
For markdown READMEs we need to add the :markup: directive.

[ci-skip]

Co-authored-by: zzak <zzakscott@gmail.com>
2023-03-21 21:16:28 +01: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
Matt Swanson fbf0145f19 Allow Attachables to override default template when attachment is missing 2023-02-13 10:45:12 -05:00
Jonathan Hefner 6225b5f59f Load version.rb for Action Mailbox and Action Text
This matches the other frameworks, ensuring that `ActionMailbox.version`
and `ActionText.version` are available when using Action Mailbox and
Action Text, respectively.

__Before__

  ```console
  $ bin/rails runner 'puts ActiveRecord.version'
  7.1.0.alpha

  $ bin/rails runner 'puts ActionMailbox.version'
  undefined method `version' for ActionMailbox:Module

  $ bin/rails runner 'puts ActionText.version'
  undefined method `version' for ActionText:Module
  ```

__After__

  ```console
  $ bin/rails runner 'puts ActiveRecord.version'
  7.1.0.alpha

  $ bin/rails runner 'puts ActionMailbox.version'
  7.1.0.alpha

  $ bin/rails runner 'puts ActionText.version'
  7.1.0.alpha
  ```
2023-01-12 17:34:49 -06:00
Drew Breunig 04177c1b7b
Prepend a '\n' to the string to be added to `application.js` to prevent errors occurring when `application.js` doesn't end in a blank line.
Running `bin/rails action_text:install` appends `import "trix"` to the `application.js` file without inserting a newline.

In my app this resulted in the following:

```js
// import statements....

import Rails from "@rails/ujs"
Rails.start()import "trix"
import "@rails/actiontext"

```

I assume this bug occurred because my `application.js` was listed at some point and removed an extra line, which was not expected by this generator.

I recommend prepending the string to be inserted with a `\n` just to be safe.
2023-01-02 13:36:03 -08: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
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
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
Étienne Barrié 3d6a7b2faa Initialize deprecators before configuring them
Since engine initializers run later in the process, we need to run this
initializer earlier than the default.

This ensures they're all registered before the environments are loaded.
2022-11-28 10:47:26 +01:00
Jonathan Hefner ec4e34034d Remove unnecessary link markup [ci-skip]
Because they are CamelCase, RDoc will automatically link these
references.  Automatic links have monospace formatting, whereas, in some
cases, manual links do not.
2022-11-15 13:57:15 -06:00
Jonathan Hefner b2bc006815 Add ActionText.deprecator
This commit adds `ActionText.deprecator`, and adds it to
`Rails.application.deprecators` so that it can be configured via
settings such as `config.active_support.report_deprecations`.
2022-11-01 17:39:39 -05: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
Jonathan Hefner 763941e81b Always provide a fallback renderer for Action Text
Follow-up to #45144.

This ensures that a renderer is always available for Action Text, even
when `ActionController::Base` was not previously loaded.

Fixes #46113.

As with #45144, this still avoids loading `ActionController::Base`
unnecessarily when rendering mail after Action Text has been loaded.

**Before:**

```
$ bin/rails r 'Benchmark.memory { |x| x.report("load"){ MyBlankMailer.blank_email.body } }'
Calculating -------------------------------------
                load     4.466M memsize (     1.205M retained)
                        29.202k objects (    11.943k retained)
                        50.000  strings (    50.000  retained)
```

**After:**

```
$ bin/rails r 'Benchmark.memory { |x| x.report("load"){ MyBlankMailer.blank_email.body } }'
Calculating -------------------------------------
                load     4.462M memsize (     1.205M retained)
                        29.141k objects (    11.940k retained)
                        50.000  strings (    50.000  retained)
```

Co-authored-by: Christopher Louvet <cl@nonplaces.com>
2022-09-23 14:36:04 -05:00
Jamis Buck 5f342cfdbe make sure rendering the content attachment actually works 2022-08-04 10:07:23 -06:00
Jamis Buck 67ab654dfa ContentAttachment is an internal API; let's indicate that with :nodoc: 2022-08-02 16:06:38 -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 327ba2d979
Merge pull request #43706 from jacobherrington/jacobherrington/address-actiontext-multiple-renders
Avoid renders in equality check of rich_text content
2022-05-22 10:10:09 -05:00
Jonathan Hefner c2a3ff0027 Avoid ActionController when render mail, AT loaded
This avoids unnecessarily loading `ActionController::Base` when
rendering mail after Action Text has been loaded.

Before:

```
$ bin/rails r 'Benchmark.memory { |x| x.report("load"){ MyBlankMailer.blank_email.body } }'
Calculating -------------------------------------
                load    12.679M memsize (     1.927M retained)
                       101.143k objects (    19.947k retained)
                        50.000  strings (    50.000  retained)
```

After:

```
$ bin/rails r 'Benchmark.memory { |x| x.report("load"){ MyBlankMailer.blank_email.body } }'
Calculating -------------------------------------
                load     7.678M memsize (     1.302M retained)
                        61.420k objects (    13.644k retained)
                        50.000  strings (    50.000  retained)
```
2022-05-20 11:57:47 -05:00
Ghouse Mohamed 6ee6cb554b Fix #version docs and some typos 2022-03-16 01:48:37 +05:30
Koichi ITO 819871cc4e Enable `Style/MapToHash` cop
Ruby 2.6 added block argument processing to `Enumerable#to_h`.
https://bugs.ruby-lang.org/issues/15143

Rails 7 requires Ruby 2.7.0 or higher, so the new feature can use it.
`Style/MapToHash` cop will detect it. And this cop in the `Style` department,
but this seems to improve performance as follows:

```ruby
# map_to_hash.rb
require 'benchmark/ips'

ARRAY = (1..100).to_a
HASH = {foo: 1, bar: 2}

Benchmark.ips do |x|
  x.report('array.map.to_h') { ARRAY.map { |v| [v, v * 2] }.to_h }
  x.report('array.to_h')     { ARRAY.to_h { |v| [v, v * 2] } }

  x.compare!
end

Benchmark.ips do |x|
  x.report('hash.map.to_h') { HASH.map { |k, v| [k.to_s, v * 2] }.to_h }
  x.report('hash.to_h')     { HASH.to_h { |k, v| [k.to_s, v * 2] } }

  x.compare!
end
```

```console
% ruby map_to_hash.rb
Warming up --------------------------------------
      array.map.to_h     9.063k i/100ms
          array.to_h     9.609k i/100ms
Calculating -------------------------------------
      array.map.to_h     89.063k (± 3.9%) i/s -    453.150k in  5.096572s
          array.to_h     96.449k (± 1.7%) i/s -    490.059k in  5.082529s

Comparison:
          array.to_h:    96448.7 i/s
      array.map.to_h:    89063.4 i/s - 1.08x  (± 0.00) slower

Warming up --------------------------------------
       hash.map.to_h   106.284k i/100ms
           hash.to_h   149.354k i/100ms
Calculating -------------------------------------
       hash.map.to_h      1.102M (± 2.2%) i/s -      5.527M in   5.019657s
           hash.to_h      1.490M (± 0.9%) i/s -      7.468M in   5.013264s

Comparison:
           hash.to_h:  1489707.0 i/s
       hash.map.to_h:  1101561.5 i/s - 1.35x  (± 0.00) slower
```

`Style/MapToHash` cop ... https://docs.rubocop.org/rubocop/1.25/cops_style.html#stylemaptohash
2022-02-26 04:31:03 +09:00
Jonathan Hefner a199aaedb8 Cross-link API docs [ci-skip]
RDoc will automatically format and link API references as long as they
are not already marked up as inline code.

This commit removes markup from various API references so that those
references will link to the relevant API docs.
2022-02-21 11:45:25 -06:00
Jacob Herrington 5811097ece Avoid renders in equality check of rich_text content 2022-02-18 14:33:12 -06:00
Jonathan Hefner 8cc8fe9628 Don't render layout in ActionText::Content#inspect
Prior to this commit, `ActionText::Content#inspect` called `#to_s` and
truncated the result to 25 characters.  However, `#to_s` calls
`#to_rendered_html_with_layout` which, by default, renders the same
first 25 characters for every instance.

For example, with models such as:

```ruby
class Message < ActiveRecord::Base
  has_rich_text :content
end

Message.create(content: "first message")
Message.create(content: "second message")
Message.create(content: "third message")
```

The output of `#inspect` is indistinguishable:

```irb
irb> Message.all.map { |message| message.content.body }
  Rendered .../actiontext/app/views/action_text/contents/_content.html.erb within layouts/action_text/contents/_content (Duration: 2.4ms | Allocations: 881)
  Rendered .../actiontext/app/views/action_text/contents/_content.html.erb within layouts/action_text/contents/_content (Duration: 0.7ms | Allocations: 257)
  Rendered .../actiontext/app/views/action_text/contents/_content.html.erb within layouts/action_text/contents/_content (Duration: 0.6ms | Allocations: 257)
=> [#<ActionText::Content "<div class=\"trix-conte...">,
    #<ActionText::Content "<div class=\"trix-conte...">,
    #<ActionText::Content "<div class=\"trix-conte...">]
```

This commit changes `#inspect` to call `#to_html`, which does not render
the Action Text layout.  So the output of `#inspect` will be:

```irb
irb> Message.all.map { |message| message.content.body }
=> [#<ActionText::Content "first message">,
    #<ActionText::Content "second message">,
    #<ActionText::Content "third message">]
```
2022-02-01 11:58:12 -06:00
Brian Kelly e9cf3e4e1c Fixes Options documention formatting for has_rich_text 2022-01-22 13:57:47 -06:00