Created
March 22, 2011 14:40
-
-
Save urbanautomaton/881306 to your computer and use it in GitHub Desktop.
Simple Paperclip Processor to timestamp attachment filenames on upload
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 Paperclip | |
class TimeStamper < Processor | |
def initialize(file, options={}, attachment=nil) | |
super(file,options,attachment) | |
timestamp_filename | |
end | |
def timestamp_filename | |
original_filename = attachment.instance_read(:file_name) | |
extension = File.extname(original_filename) | |
date_format = @attachment.options[:date_format] || | |
"%Y%m%d%H%M%S" | |
timestamp = DateTime.now.strftime(date_format) | |
new_filename = "#{timestamp}-#{original_filename}" | |
@attachment.instance_write(:file_name, new_filename) | |
end | |
def make | |
@file | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment