Skip to content

Commit 2a2e292

Browse files
committed
[browsermedia#534] Bug Fix - Can't delete image/file blocks
Validations were preventing Image/File blocks from deleting, since they required a file.
1 parent 00f1b64 commit 2a2e292

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/cms/behaviors/attaching.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ def validates_attachment_size(name, options = {})
120120
def validates_attachment_presence(name, options = {})
121121
message = options[:message] || "Must provide at least one #{name}"
122122
validate(options) do |record|
123-
record.errors.add(:attachment, message) unless record.attachments.any? { |a| a.attachment_name == name.to_s }
123+
return if record.deleted?
124+
unless record.attachments.any? { |a| a.attachment_name == name.to_s }
125+
record.errors.add(:attachment, message)
126+
end
124127
end
125128
end
126129

test/unit/models/image_block_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def teardown
2828
assert_equal :original_record_id, ImageBlock.version_foreign_key
2929
end
3030

31+
3132
end
3233

3334
class PaperclipAttachmentsTest < ActiveSupport::TestCase
@@ -36,6 +37,31 @@ def setup
3637
@image.save!
3738
end
3839

40+
test "validates_attachment_presence should ensure blocks have uploaded files.'" do
41+
image = ImageBlock.new
42+
assert_equal false, image.valid?
43+
assert_equal true, image.errors.messages.include?(:attachment)
44+
assert_equal ["You must upload a file"], image.errors.messages[:attachment]
45+
end
46+
47+
48+
test "validates_attachment_presence doesn't fire when deleting a block'" do
49+
image = ImageBlock.new(:name => "A valid name")
50+
image.deleted = true
51+
assert image.valid?, "A block being deleted doesn't need an attachment to be valid'"
52+
53+
end
54+
55+
test "Can delete a block" do
56+
@image.destroy
57+
assert @image.valid?
58+
assert @image.deleted?
59+
60+
@image.reload
61+
assert @image.attachments.empty?, "Should remove attachments"
62+
assert_equal true, @image.deleted?, "The image should be deleted."
63+
end
64+
3965
test "#attachable_type" do
4066
assert_equal "Cms::AbstractFileBlock", @image.attachable_type
4167
end

0 commit comments

Comments
 (0)