diff --git a/app/models/photo.rb b/app/models/photo.rb index 927b3db57..b52851af0 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -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 diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb index 9beba3ef6..cf1a67ee0 100644 --- a/app/uploaders/image_uploader.rb +++ b/app/uploaders/image_uploader.rb @@ -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 diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index fd7994494..275b2b40d 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -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