Skip to content

Instantly share code, notes, and snippets.

@rogal111
Created July 10, 2012 10:28
Show Gist options
  • Select an option

  • Save rogal111/3082550 to your computer and use it in GitHub Desktop.

Select an option

Save rogal111/3082550 to your computer and use it in GitHub Desktop.
Transliterate Paperclip file name to use in URLs
# put this file in config/initializers
#
# USAGE:
# in your paperclip model:
# has_attached_file :document
# before_post_process { |c| transliterate_file_name(:document) }
module TransliteratePaperclip
def transliterate_file_name(paperclip_file)
paperclip_file=[paperclip_file] unless paperclip_file.is_a?(Enumerable)
paperclip_file.each do |file|
filename=read_attribute("#{file}_file_name")
if filename.present?
extension = File.extname(filename).gsub(/^\.+/, '')
filename = filename.gsub(/\.#{extension}$/, '')
self.send(file).instance_write(:file_name, "#{filename.parameterize}.#{extension.parameterize}")
end
end
end
end
# include the extension
ActiveRecord::Base.send(:include, TransliteratePaperclip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment