Skip to content

Commit

Permalink
Make Action Text store_if_blank work for presence validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiculescu committed Aug 25, 2024
1 parent c6e3336 commit 36b167c
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 36b167c

Please sign in to comment.