From 061490c4c591ad8b17070f30474b7948ee045c4f Mon Sep 17 00:00:00 2001 From: po Date: Sat, 14 Aug 2010 18:19:44 +0800 Subject: [PATCH] merge mongoid validations from jeroenvandijk, add spec test --- lib/carrierwave/validations/active_model.rb | 6 +- spec/orm/mongoid_spec.rb | 113 +++++++++++++++----- 2 files changed, 89 insertions(+), 30 deletions(-) diff --git a/lib/carrierwave/validations/active_model.rb b/lib/carrierwave/validations/active_model.rb index f65ae1200..32286aaca 100644 --- a/lib/carrierwave/validations/active_model.rb +++ b/lib/carrierwave/validations/active_model.rb @@ -15,8 +15,7 @@ class IntegrityValidator < ::ActiveModel::EachValidator def validate_each(record, attribute, value) if record.send("#{attribute}_processing_error") - options[:message] ||= I18n.t('carrierwave.errors.processing', :default => 'failed to be processed.') - record.errors.add attribute, :integrity, options + record.errors[attribute] = I18n.t('carrierwave.errors.processing', :default => 'failed to be processed.') end end end @@ -25,8 +24,7 @@ class ProcessingValidator < ::ActiveModel::EachValidator def validate_each(record, attribute, value) if record.send("#{attribute}_integrity_error") - options[:message] ||= I18n.t('carrierwave.errors.integrity', :default => 'is not an allowed type of file.') - record.errors.add attribute, :processing, options.merge!(:value => value) + record.errors[attribute] = I18n.t('carrierwave.errors.integrity', :default => 'is not an allowed type of file.') end end end diff --git a/spec/orm/mongoid_spec.rb b/spec/orm/mongoid_spec.rb index 1096a41c5..99d108daa 100644 --- a/spec/orm/mongoid_spec.rb +++ b/spec/orm/mongoid_spec.rb @@ -6,18 +6,20 @@ connection = Mongo::Connection.new Mongoid.database = connection.db("carrierwave_test") -MongoidUploader = Class.new(CarrierWave::Uploader::Base) -MongoidUser = Class.new -MongoidUser.class_eval do - include Mongoid::Document - store_in :users - mount_uploader :image, MongoidUploader -end - describe CarrierWave::Mongoid do + before do + @uploader = Class.new(CarrierWave::Uploader::Base) + @user = Class.new + @user.class_eval do + include Mongoid::Document + store_in :users + end + @user.mount_uploader :image, @uploader + end + after do - MongoidUser.collection.drop + @user.collection.drop end describe '#image' do @@ -25,7 +27,7 @@ context "when nothing is assigned" do before do - @document = MongoidUser.new + @document = @user.new end it "returns a blank uploader" do @@ -37,12 +39,12 @@ context "when an empty string is assigned" do before do - @document = MongoidUser.new(:image_filename => "") + @document = @user.new(:image_filename => "") @document.save end it "returns a blank uploader" do - @saved_doc = MongoidUser.first + @saved_doc = @user.first @saved_doc.image.should be_blank end @@ -51,13 +53,13 @@ context "when a filename is saved in the database" do before do - @document = MongoidUser.new(:image_filename => "test.jpg") + @document = @user.new(:image_filename => "test.jpg") @document.save - @doc = MongoidUser.first + @doc = @user.first end it "returns an uploader" do - @doc.image.should be_an_instance_of(MongoidUploader) + @doc.image.should be_an_instance_of(@uploader) end it "sets the path to the store directory" do @@ -71,7 +73,7 @@ describe '#image=' do before do - @doc = MongoidUser.new + @doc = @user.new end context "when nil is assigned" do @@ -96,7 +98,7 @@ it "should cache a file" do @doc.image = stub_file('test.jpeg') - @doc.image.should be_an_instance_of(MongoidUploader) + @doc.image.should be_an_instance_of(@uploader) end it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do @@ -110,12 +112,71 @@ end + context 'when validating integrity' do + before do + @uploader.class_eval do + def extension_white_list + %(txt) + end + end + @doc = @user.new + @doc.image = stub_file('test.jpg') + end + + it "should make the document invalid when an integrity error occurs" do + @doc.should_not be_valid + end + + it "should use I18n for integrity error messages" do + @doc.valid? + @doc.errors[:image].should == ['is not an allowed type of file.'] + + change_locale_and_store_translations(:pt, :carrierwave => { + :errors => { :integrity => 'tipo de imagem não permitido.' } + }) do + @doc.should_not be_valid + @doc.errors[:image].should == ['tipo de imagem não permitido.'] + end + end + end + + context 'when validating processing' do + before do + @uploader.class_eval do + process :monkey + def monkey + raise CarrierWave::ProcessingError, "Ohh noez!" + end + def extension_white_list + %(jpg) + end + end + @doc.image = stub_file('test.jpg') + end + + it "should make the document invalid when a processing error occurs" do + @doc.should_not be_valid + end + + it "should use I18n for processing error messages" do + @doc.valid? + @doc.errors[:image].should == ['failed to be processed.'] + + change_locale_and_store_translations(:pt, :carrierwave => { + :errors => { :processing => 'falha ao processar imagem.' } + }) do + @doc.should_not be_valid + @doc.errors[:image].should == ['falha ao processar imagem.'] + end + end + end + end describe "#save" do before do - @doc = MongoidUser.new + @doc = @user.new end context "when no file is assigned" do @@ -132,7 +193,7 @@ it "copies the file to the upload directory" do @doc.image = stub_file('test.jpg') @doc.save - @doc.image.should be_an_instance_of(MongoidUploader) + @doc.image.should be_an_instance_of(@uploader) @doc.image.current_path.should == public_path('uploads/test.jpg') end @@ -163,7 +224,7 @@ describe '#destroy' do before do - @doc = MongoidUser.new + @doc = @user.new end describe "when file assigned" do @@ -172,7 +233,7 @@ @doc.image = stub_file('test.jpeg') @doc.save.should be_true File.exist?(public_path('uploads/test.jpeg')).should be_true - @doc.image.should be_an_instance_of(MongoidUploader) + @doc.image.should be_an_instance_of(@uploader) @doc.image.current_path.should == public_path('uploads/test.jpeg') @doc.destroy File.exist?(public_path('uploads/test.jpeg')).should be_false @@ -182,16 +243,16 @@ describe "when file is not assigned" do - it "deletes the instance of MongoidUser after save" do + it "deletes the instance of @user after save" do @doc.save - MongoidUser.count.should eql(1) + @user.count.should eql(1) @doc.destroy end - it "deletes the instance of MongoidUser after save and then re-looking up the instance" do + it "deletes the instance of @user after save and then re-looking up the instance" do @doc.save - MongoidUser.count.should eql(1) - @doc = MongoidUser.first + @user.count.should eql(1) + @doc = @user.first @doc.destroy end