Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

raise InvalidFileException when non files are sent to a grip managed …

…attr
  • Loading branch information...
commit 68d5eab5db2f7ebcc277dac7a3b85dcab5fd75a7 1 parent 9efb0fc
Christopher Burnett authored
Showing with 19 additions and 11 deletions.
  1. +1 −1  grip.gemspec
  2. +13 −10 lib/grip.rb
  3. +5 −0 test/test_grip.rb
2  grip.gemspec
View
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{grip}
- s.version = "0.4.3"
+ s.version = "0.4.4"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["twoism", "jnunemaker"]
23 lib/grip.rb
View
@@ -44,8 +44,9 @@ def has_grid_attachment(name,opts={})
end
define_method("#{name}=") do |file|
+ raise Grip::InvalidFileException unless (file.is_a?(File) || file.is_a?(Tempfile))
self['_id'] = Mongo::ObjectID.new if _id.blank?
- self["#{name}_size"] = File.size(file)
+ self["#{name}_size"] = File.size(file)
self["#{name}_name"] = File.basename(file.path)
self["#{name}_path"] = "#{self.class.to_s.underscore}/#{name}/#{_id}"
self["#{name}_content_type"] = file.content_type rescue MIME::Types.type_for(self["#{name}_name"]).to_s
@@ -58,16 +59,14 @@ def attachment_definitions
end
end
+ # Roll through attachment definitions and check if they are a File or Tempfile. Both types are
+ # nescessary for file uploads to work properly. Each file checks for a <attr_name>_process
+ # callback for pre-processing before save.
def save_attachments
- self.class.attachment_definitions.each do |attachment|
-
- name, opts = attachment
-
- if (opts[:file].is_a?(File) || opts[:file].is_a?(Tempfile))
- GridFS::GridStore.open(self.class.database, self["#{name}_path"], 'w', :content_type => self["#{name}_content_type"]) do |f|
- processed_or_not = self.respond_to?("process_#{name}") ? send("process_#{name}",opts) : opts[:file]
- f.write(processed_or_not)
- end
+ self.class.attachment_definitions.each do |definition|
+ attr_name, opts = definition
+ GridFS::GridStore.open(self.class.database, self["#{attr_name}_path"], 'w', :content_type => self["#{attr_name}_content_type"]) do |f|
+ f.write send("process_#{attr_name}",opts) rescue opts[:file]
end
end
end
@@ -77,4 +76,8 @@ def destroy_attached_files
GridFS::GridStore.unlink(self.class.database, self["#{name}_path"])
end
end
+
+ class Grip::InvalidFileException < Exception
+ end
+
end
5 test/test_grip.rb
View
@@ -79,4 +79,9 @@ def teardown
assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.image_path)
assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.pdf_path)
end
+
+ test "should raise invalid file exception" do
+ assert_raise(Grip::InvalidFileException) { @doc.image = ""; @doc.save! }
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.