Skip to content

Commit

Permalink
Merge pull request rails#52709 from ghiculescu/at-store-if-blank-vali…
Browse files Browse the repository at this point in the history
…dation

Make Action Text `store_if_blank` work for `presence` validation
  • Loading branch information
rafaelfranca authored Aug 26, 2024
2 parents 9558f00 + 36b167c commit 48aaffd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion actiontext/lib/action_text/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ def #{name}=(body)
if body.present?
self.#{name}.body = body
else
self.#{name}.mark_for_destruction if #{name}?
if #{name}?
self.#{name}.body = body
self.#{name}.mark_for_destruction
end
end
end
CODE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MessageWithoutBlanksWithContentValidation < MessageWithoutBlanks
validates :content, presence: true
end
12 changes: 12 additions & 0 deletions actiontext/test/unit/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,16 @@ class ActionText::ModelTest < ActiveSupport::TestCase
message.update(content: "")
end
end

test "if disallowing blanks, can still validate presence" do
message1 = MessageWithoutBlanksWithContentValidation.new(subject: "Greetings", content: "")
assert_not_predicate message1, :valid?
message1.content = "content"
assert_predicate message1, :valid?

message2 = MessageWithoutBlanksWithContentValidation.new(subject: "Greetings", content: "content")
assert_predicate message2, :valid?
message2.content = ""
assert_not_predicate message2, :valid?
end
end

0 comments on commit 48aaffd

Please sign in to comment.