Skip to content

Commit f002536

Browse files
authored
Merge pull request rails#46271 from seanpdoyle/has-rich-text-strict-loading
Support `has_rich_text` with `strict_loading:`
2 parents 4435b7a + 4a743b9 commit f002536

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

actiontext/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Support `strict_loading:` option for `has_rich_text` declaration
2+
3+
*Sean Doyle*
4+
15
* Update ContentAttachment so that it can encapsulate arbitrary HTML content in a document.
26

37
*Jamis Buck*

actiontext/lib/action_text/attribute.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ module Attribute
3030
#
3131
# * <tt>:encrypted</tt> - Pass true to encrypt the rich text attribute. The encryption will be non-deterministic. See
3232
# +ActiveRecord::Encryption::EncryptableRecord.encrypts+. Default: false.
33-
def has_rich_text(name, encrypted: false)
33+
#
34+
# * <tt>:strict_loading</tt> - Pass true to force strict loading. When
35+
# omitted, <tt>strict_loading:</tt> will be set to the value of the
36+
# <tt>strict_loading_by_default</tt> class attribute (false by default).
37+
def has_rich_text(name, encrypted: false, strict_loading: strict_loading_by_default)
3438
class_eval <<-CODE, __FILE__, __LINE__ + 1
3539
def #{name}
3640
rich_text_#{name} || build_rich_text_#{name}
@@ -47,7 +51,8 @@ def #{name}=(body)
4751

4852
rich_text_class_name = encrypted ? "ActionText::EncryptedRichText" : "ActionText::RichText"
4953
has_one :"rich_text_#{name}", -> { where(name: name) },
50-
class_name: rich_text_class_name, as: :record, inverse_of: :record, autosave: true, dependent: :destroy
54+
class_name: rich_text_class_name, as: :record, inverse_of: :record, autosave: true, dependent: :destroy,
55+
strict_loading: strict_loading
5156

5257
scope :"with_rich_text_#{name}", -> { includes("rich_text_#{name}") }
5358
scope :"with_rich_text_#{name}_and_embeds", -> { includes("rich_text_#{name}": { embeds_attachments: :blob }) }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class ActionText::StrictLoadingTest < ActiveSupport::TestCase
6+
class MessageWithStrictLoading < Message
7+
self.strict_loading_by_default = true
8+
9+
has_rich_text :strict_loading_content
10+
end
11+
12+
class MessageWithFooter < MessageWithStrictLoading
13+
has_rich_text :footer, strict_loading: false
14+
end
15+
16+
test "has_rich_text reads strict_loading: option from strict_loading_by_default" do
17+
MessageWithStrictLoading.create! strict_loading_content: "ignored"
18+
19+
assert_raises ActiveRecord::StrictLoadingViolationError do
20+
MessageWithStrictLoading.all.map(&:strict_loading_content)
21+
end
22+
23+
MessageWithStrictLoading.with_rich_text_strict_loading_content.map(&:strict_loading_content)
24+
end
25+
26+
test "pre-loading the association does not raise a StrictLoadingViolationError" do
27+
MessageWithStrictLoading.create! strict_loading_content: "ignored"
28+
29+
records = MessageWithStrictLoading.with_rich_text_strict_loading_content.all
30+
31+
records.map(&:strict_loading_content)
32+
end
33+
34+
test "has_rich_text accepts strict_loading: overrides" do
35+
MessageWithFooter.create! footer: "ignored"
36+
37+
records = MessageWithFooter.all
38+
39+
records.map(&:footer)
40+
end
41+
end

0 commit comments

Comments
 (0)