Commit Graph

6 Commits

Author SHA1 Message Date
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
Mark VanLandingham bece535c98
Add config.action_text.attachment_tag_name 2021-03-05 16:57:36 -05:00
Sean Doyle 3500571b43 Improve ActionText extensiblibility
Extensible layout
---

Expose how we render the HTML _surrounding_ rich text content as an
extensible `layouts/action_text/contents/_content.html.erb` template to
encourage user-land customizations, while retaining private API control
over how the rich text itself is rendered by moving the
`#render_action_text_content` helper invocation to the
`action_text/contents/_content.html.erb` partial.

Extensible Attachable `#to_attachable_partial_path`
---

When an application declares a canonical partial for a record, there is
no way to override which partial is used when transformed to Rich Text.
For example, a default `Person < ApplicationRecord` instance returns
`"people/person"` from calls to `#to_partial_path`, resulting in the
`app/views/people/_person.html.erb` partial being rendered.

Prior to this change, when encountering an `<action-text-attachment
sgid="...">` element, ActionText retrieved the corresponding
`Attachable` instance (usually an `ActiveRecord::Base` instance) and
transformed it to rich text HTML by rendering the partial that
corresponds to its `#to_partial_path`.

This proposed change instead invokes
`Attachable#to_attachable_partial_path`. By default,
`#to_attachable_partial_path` is an alias for `#to_partial_path`.

Guides
---

Extend the `guides/action_text_overview` document to
describe how to customize these templates, and to better illustrate how
ActionText::Attachable instances are rendered into HTML.
2020-12-29 20:06:45 -05:00
Jonathan Hefner 76e111395c Always render Action Text HTML with :html format
Since #40222, Action Text HTML is rendered in the context of the current
request.  This causes the Action Text template format to default to the
request format, which prevents the template from being resolved when the
request format is not `:html` (e.g. `:json`).  Therefore, override the
template format to always be `:html`.

Fixes #40695.
2020-11-27 10:59:24 -06:00
Jonathan Hefner 614e813161
Disentangle Action Text from ApplicationController
This commit allows Action Text to be used without having an
ApplicationController defined.  In doing so, it also fixes Action Text
attachments to render the correct URL host in mailers.

It also avoids allocating an ActionController::Renderer per request.

Fixes #37183.
Fixes #35578.
Fixes #36963.
Closes #38714.

Co-authored-by: Jeremy Daer <jeremydaer@gmail.com>
2020-10-30 01:01:42 +00:00
George Claghorn 0decd2ddc4 Import Action Text 2019-01-04 22:22:49 -05:00