From 60eb01c2eeba8d0f71c43b307830b2656f08f15e Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 15 May 2020 12:10:07 -0500 Subject: [PATCH] Add has_one_attached_with_default --- app/models/concerns/image/attachment_extensions.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/models/concerns/image/attachment_extensions.rb b/app/models/concerns/image/attachment_extensions.rb index 71f29916..4d10c0d4 100644 --- a/app/models/concerns/image/attachment_extensions.rb +++ b/app/models/concerns/image/attachment_extensions.rb @@ -36,5 +36,15 @@ module Image::AttachmentExtensions end RUBY end + + def has_one_attached_with_default(attribute_name, default_path, **options) + after_save do + attribute = send(attribute_name) + unless attribute.attached? + attribute.attach(io: File.open(default_path), **options) + end + self + end + end end end \ No newline at end of file