Created
April 27, 2009 07:47
-
-
Save rob-at-thewebfellas/102379 to your computer and use it in GitHub Desktop.
Deletable attachment module for 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
module TheWebFellas | |
module HasDeletableAttachment | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def has_deletable_attachment(*attr_names) | |
attr_names.each do |name| | |
attr_accessible "delete_#{name}" | |
before_validation "delete_#{name}_before_validation" | |
define_method("delete_#{name}=") do |value| | |
instance_variable_set("@delete_#{name}", value.respond_to?(:to_i) ? !value.to_i.zero? : value) | |
end | |
define_method("delete_#{name}") do | |
!!instance_variable_get("@delete_#{name}") | |
end | |
alias_method "delete_#{name}?", "delete_#{name}" | |
define_method("delete_#{name}_before_validation") do | |
attachment = send(name) | |
attachment.clear if send("delete_#{name}?") && !attachment.dirty? | |
true | |
end | |
protected("delete_#{name}_before_validation") | |
end | |
end | |
end | |
end | |
end |
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
ActiveRecord::Base.send(:include, TheWebFellas::HasDeletableAttachment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment