Skip to content

Commit

Permalink
merge mongoid validations from jeroenvandijk, add spec test
Browse files Browse the repository at this point in the history
  • Loading branch information
saberma committed Aug 14, 2010
1 parent a7ba627 commit 061490c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 30 deletions.
6 changes: 2 additions & 4 deletions lib/carrierwave/validations/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
113 changes: 87 additions & 26 deletions spec/orm/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@
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

context "when nothing is assigned" do

before do
@document = MongoidUser.new
@document = @user.new
end

it "returns a blank uploader" do
Expand All @@ -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

Expand All @@ -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
Expand All @@ -71,7 +73,7 @@
describe '#image=' do

before do
@doc = MongoidUser.new
@doc = @user.new
end

context "when nil is assigned" do
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -163,7 +224,7 @@
describe '#destroy' do

before do
@doc = MongoidUser.new
@doc = @user.new
end

describe "when file assigned" do
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 061490c

Please sign in to comment.