Skip to content

Instantly share code, notes, and snippets.

@notblizzard
Created May 2, 2015 22:12
Show Gist options
  • Save notblizzard/348ebd6ab11f8acf9be8 to your computer and use it in GitHub Desktop.
Save notblizzard/348ebd6ab11f8acf9be8 to your computer and use it in GitHub Desktop.
require 'securerandom'
class Photo < ActiveRecord::Base
validates :title, presence: true
validates :photo, presence: true
before_create :rename_photo
has_many :comments
has_attached_file :photo,
:path => ":rails_root/public/uploads/:class/:attachment/:id/:style/:basename.:extension",
:url => '/i/:basename.:extension',
:styles => {:medium => "500x500>", :thumb => "100x100>"}
validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
belongs_to :user
def rename_photo
extension = File.extname(photo_file_name).downcase
self.photo.instance_write :file_name, "#{SecureRandom.urlsafe_base64(10)}#{extension}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment