require 'vos' shared_examples_for 'file model crud' do describe "file model crud" do before :all do class ImageFile inherit FileModel def build_path name, version = nil '/storage/images' + super end def build_url name, version = nil '/images' + super end def validate super errors << 'invalid name' if original and (original.name =~ /invalid/) end end end after(:all){remove_constants :ImageFile} before do require 'tempfile' @tmp_dir = Dir.tmpdir.to_dir.dir('/shard_crud').create.path @shared_dir = __FILE__.to_entry.parent.dir(:shared_crud).path ImageFile.box = Vos::Box.new(Vos::Drivers::Local.new(@tmp_dir)) end after do @tmp_dir.to_dir.destroy end it 'CRUD' do # read model = @model_class.new model.image.class.should == ImageFile model.image.url.should be_nil model.image.file.should be_nil # preserving file = "#{@shared_dir}/ship.jpg" model.image = file model.image.original.path.should == "#{@shared_dir}/ship.jpg" model.instance_variable_get(:@image).should == 'ship.jpg' model.image.url.should be_nil model.image.file.should be_nil # saving model.save.should be_true model.instance_variable_get(:@image).should == 'ship.jpg' model.image.url.should == '/images/ship.jpg' model.image.file.path.should == "/storage/images/ship.jpg" "#{@tmp_dir}/storage/images/ship.jpg".to_file.exist?.should be_true # reading model = @model_class.new model.instance_variable_set(:@image, 'ship.jpg') model.image.url.should == '/images/ship.jpg' model.image.file.path.should == "/storage/images/ship.jpg" # updating file2 = "#{@shared_dir}/ship2.jpg".to_file model.image = file2 model.instance_variable_get(:@image).should == 'ship2.jpg' model.image.original.path.should == "#{@shared_dir}/ship2.jpg" model.image.url.should == '/images/ship.jpg' model.image.file.path.should == "/storage/images/ship.jpg" model.save.should be_true model.instance_variable_get(:@image).should == 'ship2.jpg' model.image.url.should == '/images/ship2.jpg' model.image.file.path.should == "/storage/images/ship2.jpg" "#{@tmp_dir}/storage/images/ship.jpg".to_file.exist?.should be_false "#{@tmp_dir}/storage/images/ship2.jpg".to_file.exist?.should be_true # destroying model.destroy.should be_true "#{@tmp_dir}/storage/images/ship2.jpg".to_file.exist?.should be_false end it "should be able to submit errors and interrupt model saving" do file = "#{@shared_dir}/invalid.txt".to_file model = @model_class.new model.image = file model.image.should_not_receive(:save) model.save.should be_false model.errors[:image].should == ['invalid name'] end end end