-
-
Save arjunmenon/9688019 to your computer and use it in GitHub Desktop.
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
#http://blog.wyeworks.com/2009/7/13/paperclip-file-rename | |
Paperclip.interpolates :default_image_type do |attachment, style| | |
attachment.instance.get_user_default_profile_image | |
end | |
Paperclip.interpolates :normalized_avatar_file_name do |attachment, style| | |
attachment.instance.normalized_avatar_file_name | |
end | |
# store User avatar | |
has_attached_file :avatar, :styles => { | |
:hero => '20x20', # To show on top header with user name | |
:small => '45x45', # To show with activity | |
:thumb => '110x110' # To show on User profile page, or on thumbnails. | |
}, | |
:default_url => "/images/:default_image_type_:style.png", # default image for an user. TODO: Change it as per user sex (m|f) | |
:url => "/system/avatars/:id/:style/:normalized_avatar_file_name", | |
:path => ":rails_root/public/system/avatars/:id/:style/:normalized_avatar_file_name" | |
validates_attachment_size :avatar, :less_than=>500.kilobyte | |
validates_attachment_content_type :avatar, :content_type=>['image/jpeg', 'image/png', 'image/gif'] | |
def normalized_avatar_file_name | |
return transliterate(self.avatar_file_name) | |
end | |
def transliterate(str) | |
# Based on permalink_fu by Rick Olsen | |
# Escape str by transliterating to UTF-8 with Iconv | |
s = Iconv.iconv('ascii//ignore//translit', 'utf-8', str).to_s | |
# Downcase string | |
s.downcase! | |
# Remove apostrophes so isn't changes to isnt | |
s.gsub!(/'/, '') | |
# Replace any non-letter or non-number character with a space | |
s.gsub!(/[^A-Za-z0-9_\.]+/, ' ') | |
s.strip! | |
# Replace groups of spaces with single hyphen | |
s.gsub!(/\ +/, '-') | |
return s | |
end | |
#Paperclip Custom Interpolations to pick default user on his/her sex | |
def get_user_default_profile_image | |
default_image_type = 'm' | |
if self.avatar_file_name.blank? # Means no file uploaded yet | |
if !self.sex.blank? and self.sex.upcase == SEX_TYPE[:female].upcase | |
default_image_type = 'f' | |
end | |
end | |
return default_image_type | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment