Add has_one_attached_with_default
This commit is contained in:
parent
afecb1d63f
commit
60eb01c2ee
1 changed files with 10 additions and 0 deletions
|
@ -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
|
Loading…
Reference in a new issue