Skip to content

Commit 48aaffd

Browse files
authored
Merge pull request rails#52709 from ghiculescu/at-store-if-blank-validation
Make Action Text `store_if_blank` work for `presence` validation
2 parents 9558f00 + 36b167c commit 48aaffd

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

actiontext/lib/action_text/attribute.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def #{name}=(body)
7373
if body.present?
7474
self.#{name}.body = body
7575
else
76-
self.#{name}.mark_for_destruction if #{name}?
76+
if #{name}?
77+
self.#{name}.body = body
78+
self.#{name}.mark_for_destruction
79+
end
7780
end
7881
end
7982
CODE
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class MessageWithoutBlanksWithContentValidation < MessageWithoutBlanks
2+
validates :content, presence: true
3+
end

actiontext/test/unit/model_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,16 @@ class ActionText::ModelTest < ActiveSupport::TestCase
152152
message.update(content: "")
153153
end
154154
end
155+
156+
test "if disallowing blanks, can still validate presence" do
157+
message1 = MessageWithoutBlanksWithContentValidation.new(subject: "Greetings", content: "")
158+
assert_not_predicate message1, :valid?
159+
message1.content = "content"
160+
assert_predicate message1, :valid?
161+
162+
message2 = MessageWithoutBlanksWithContentValidation.new(subject: "Greetings", content: "content")
163+
assert_predicate message2, :valid?
164+
message2.content = ""
165+
assert_not_predicate message2, :valid?
166+
end
155167
end

0 commit comments

Comments
 (0)