the photo image url is now randomized

This commit is contained in:
zhitomirskiyi 2010-11-04 14:37:08 -07:00
parent c1703edc22
commit 7f5b98a6cb
3 changed files with 27 additions and 4 deletions

View file

@ -13,6 +13,7 @@ class Photo < Post
key :caption, String
key :remote_photo_path
key :remote_photo_name
key :random_string
timestamps!
@ -23,6 +24,7 @@ class Photo < Post
def self.instantiate(params = {})
photo = super(params)
image_file = params.delete(:user_file)
photo.random_string = gen_random_string(10)
photo.image.store! image_file
photo
@ -61,5 +63,12 @@ class Photo < Post
def mutable?
true
end
def self.gen_random_string(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
string = ""
1.upto(len) { |i| string << chars[rand(chars.size-1)] }
return string
end
end

View file

@ -5,6 +5,8 @@
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
def store_dir
"uploads/images"
end
@ -14,9 +16,11 @@ class ImageUploader < CarrierWave::Uploader::Base
end
def filename
model.id.to_s + File.extname(@filename) if @filename
model.random_string + model.id.to_s + File.extname(@filename) if @filename
end
version :thumb_small do
process :resize_to_fill => [50,50]
end

View file

@ -13,9 +13,10 @@ describe Photo do
@fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', @fixture_filename)
@fail_fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', 'msg.xml')
@photo = Photo.new
@photo.person = @user.person
@photo.diaspora_handle = @user.person.diaspora_handle
# @photo = Photo.new
#@photo.person = @user.person
#@photo.diaspora_handle = @user.person.diaspora_handle
@photo = @user.post(:photo, :user_file=> File.open(@fixture_name), :to => @aspect.id)
@photo2 = @user.post(:photo, :user_file=> File.open(@fixture_name), :to => @aspect.id)
end
@ -31,12 +32,21 @@ describe Photo do
@photo.update_attributes(:person_id => Factory(:person).id)
@photo.reload.person.should == @user.person
end
it 'allows assignmant of caption' do
@photo.save!
@photo.update_attributes(:caption => "this is awesome!!")
@photo.reload.caption.should == "this is awesome!!"
end
end
it 'should be mutable' do
@photo.mutable?.should == true
end
it 'has a random string key' do
@photo2.random_string.should_not be nil
end
describe '.instantiate' do
it 'sets the persons diaspora handle' do
@photo2.diaspora_handle.should == @user.person.diaspora_handle