Created
October 25, 2012 17:03
-
-
Save sunny/3954054 to your computer and use it in GitHub Desktop.
Rails Initializer to add destroyable attachments with Paperclip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
class ActiveRecord::Base | |
# Class method to add destroyable paperclip attachments. | |
# | |
# Example: | |
# has_attached_file :image | |
# has_destroyable_file :image | |
# attr_accessible :image_delete | |
# | |
# Adds `image_delete`, `image_delete=` methods. Before_save if `image_delete` is | |
# set to "1", the `image` attachment gets deleted. | |
# | |
# Example html in form: | |
# <%= f.check_box :image_delete %> | |
# <%= f.label :image_delete, 'Delete image' %> | |
def self.has_destroyable_file(*attachments) | |
attachments.each do |attachment| | |
attr_accessor :"#{attachment}_delete" | |
before_save do | |
self.send(attachment).clear if self.send(:"#{attachment}_delete") == "1" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment