grip 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.DS_Store +0 -0
- data/README.rdoc +28 -20
- data/VERSION +1 -1
- data/grip.gemspec +16 -5
- data/lib/grip.rb +7 -122
- data/lib/grip/attachment.rb +84 -0
- data/lib/grip/has_attachment.rb +75 -0
- data/test/factories.rb +11 -0
- data/test/growler.rb +9 -0
- data/test/models.rb +5 -0
- data/test/test_grip_attachment.rb +64 -0
- data/test/test_has_attachment.rb +68 -0
- data/test/test_helper.rb +16 -16
- metadata +15 -4
- data/test/test_grip.rb +0 -91
data/.DS_Store
ADDED
Binary file
|
data/README.rdoc
CHANGED
@@ -6,34 +6,42 @@ GridFS attachments for MongoMapper
|
|
6
6
|
|
7
7
|
The grip gem is hosted on gemcutter.org:
|
8
8
|
|
9
|
-
|
9
|
+
gem install miso grip
|
10
10
|
|
11
11
|
|
12
|
-
= Usage
|
12
|
+
= Usage (See tests for better docs)
|
13
13
|
|
14
|
-
class
|
14
|
+
class Doc
|
15
15
|
include MongoMapper::Document
|
16
16
|
include Grip
|
17
17
|
|
18
18
|
has_grid_attachment :image, :versions => {:thumb => {:width=>50,:height=>50}}
|
19
|
-
has_grid_attachment :pdf
|
20
19
|
|
21
|
-
# Hook for resizing or whatever gets you there.
|
22
|
-
# any specified options are passed back to you
|
23
|
-
# at this point.
|
24
|
-
# Just return the file when you're done.
|
25
|
-
def process_image opts
|
26
|
-
puts "processing"
|
27
|
-
opts[:file]
|
28
|
-
end
|
29
20
|
end
|
30
21
|
|
31
22
|
image = File.open('foo.png', 'r')
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
23
|
+
@doc = Doc.create(:image => image)
|
24
|
+
|
25
|
+
puts @doc.image.name
|
26
|
+
=> "image"
|
27
|
+
puts @doc.image.file_size
|
28
|
+
=> 100
|
29
|
+
puts @doc.image.file_name
|
30
|
+
=> "foo.png"
|
31
|
+
puts @doc.image.grid_key
|
32
|
+
=> "docs/<id>/image"
|
33
|
+
|
34
|
+
# works on nested variants as well
|
35
|
+
|
36
|
+
puts @doc.image.thumb.name
|
37
|
+
=> "thumb"
|
38
|
+
puts @doc.image.thumb.file_size
|
39
|
+
=> 50
|
40
|
+
puts @doc.image.thumb.file_name
|
41
|
+
=> "foo.png"
|
42
|
+
puts @doc.image.thumb.grid_key
|
43
|
+
=> "attachments/<id>/thumb"
|
44
|
+
|
45
|
+
|
46
|
+
@doc.image.file # contents read from gridfs for serving from rack/metal/controller
|
47
|
+
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/grip.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{grip}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.6.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["twoism", "jnunemaker"]
|
12
|
-
s.date = %q{2009-12-
|
12
|
+
s.date = %q{2009-12-31}
|
13
13
|
s.description = %q{GridFS attachments for MongoMapper}
|
14
14
|
s.email = %q{signalstatic@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,16 +17,23 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
".
|
20
|
+
".DS_Store",
|
21
|
+
".gitignore",
|
21
22
|
"LICENSE",
|
22
23
|
"README.rdoc",
|
23
24
|
"Rakefile",
|
24
25
|
"VERSION",
|
25
26
|
"grip.gemspec",
|
26
27
|
"lib/grip.rb",
|
28
|
+
"lib/grip/attachment.rb",
|
29
|
+
"lib/grip/has_attachment.rb",
|
30
|
+
"test/factories.rb",
|
27
31
|
"test/fixtures/cthulhu.png",
|
28
32
|
"test/fixtures/sample.pdf",
|
29
|
-
"test/
|
33
|
+
"test/growler.rb",
|
34
|
+
"test/models.rb",
|
35
|
+
"test/test_grip_attachment.rb",
|
36
|
+
"test/test_has_attachment.rb",
|
30
37
|
"test/test_helper.rb"
|
31
38
|
]
|
32
39
|
s.homepage = %q{http://github.com/twoism/grip}
|
@@ -35,7 +42,11 @@ Gem::Specification.new do |s|
|
|
35
42
|
s.rubygems_version = %q{1.3.5}
|
36
43
|
s.summary = %q{GridFS attachments for MongoMapper}
|
37
44
|
s.test_files = [
|
38
|
-
"test/
|
45
|
+
"test/factories.rb",
|
46
|
+
"test/growler.rb",
|
47
|
+
"test/models.rb",
|
48
|
+
"test/test_grip_attachment.rb",
|
49
|
+
"test/test_has_attachment.rb",
|
39
50
|
"test/test_helper.rb"
|
40
51
|
]
|
41
52
|
|
data/lib/grip.rb
CHANGED
@@ -1,125 +1,10 @@
|
|
1
|
-
|
2
|
-
require 'mime/types'
|
3
|
-
require 'tempfile'
|
4
|
-
require 'miso'
|
1
|
+
%w{mongo_mapper mongo/gridfs mime/types ftools tempfile rmagick miso}.each { |lib| require lib }
|
5
2
|
|
6
|
-
module
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
after_save :save_attachments
|
11
|
-
before_destroy :destroy_attached_files
|
12
|
-
end
|
3
|
+
module MongoMapper
|
4
|
+
module Grip
|
5
|
+
class GripError < StandardError; end
|
6
|
+
class InvalidFile < GripError; end
|
13
7
|
end
|
14
|
-
|
15
|
-
module ClassMethods
|
16
|
-
def has_grid_attachment(name,opts={})
|
17
|
-
write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
|
18
|
-
attachment_definitions[name] = opts
|
19
|
-
|
20
|
-
build_attachment_keys_for name
|
21
|
-
|
22
|
-
define_method(name) do
|
23
|
-
GridFS::GridStore.open(self.class.database, self["#{name}_path"], 'r') {|f| f }
|
24
|
-
end
|
25
|
-
|
26
|
-
define_method("#{name}=") do |file|
|
27
|
-
update_attachment_attributes!(name, file)
|
28
|
-
self.class.attachment_definitions[name][:file] = file
|
29
|
-
end
|
30
|
-
|
31
|
-
unless opts[:versions].nil?
|
32
|
-
opts[:versions].each do |v,dimensions|
|
33
|
-
|
34
|
-
version_name = "#{name}_#{v}"
|
35
|
-
build_attachment_keys_for version_name
|
36
|
-
|
37
|
-
define_method(version_name) do
|
38
|
-
GridFS::GridStore.open(self.class.database, self["#{version_name}_path"], 'r') {|f| f }
|
39
|
-
end
|
40
|
-
|
41
|
-
define_method("#{version_name}=") do |file|
|
42
|
-
update_attachment_attributes!("#{version_name}", file, true)
|
43
|
-
self["#{version_name}_content_type"] = self["#{name}_content_type"]
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def build_attachment_keys_for name
|
50
|
-
key "#{name}_size".to_sym, Integer
|
51
|
-
key "#{name}_path".to_sym, String
|
52
|
-
key "#{name}_name".to_sym, String
|
53
|
-
key "#{name}_content_type".to_sym, String
|
54
|
-
end
|
55
|
-
|
56
|
-
def attachment_definitions
|
57
|
-
read_inheritable_attribute(:attachment_definitions)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def update_attachment_attributes! name, file, is_version = false
|
62
|
-
raise Grip::InvalidFileException unless (file.is_a?(File) || file.is_a?(Tempfile) || file.is_a?(Miso::Image))
|
63
|
-
|
64
|
-
self["#{name}_size"] = File.size(file)
|
65
|
-
self["#{name}_name"] = File.basename(file.path)
|
66
|
-
|
67
|
-
unless is_version
|
68
|
-
self['_id'] = Mongo::ObjectID.new if _id.blank?
|
69
|
-
self["#{name}_content_type"] = file.content_type rescue MIME::Types.type_for(self["#{name}_name"]).to_s
|
70
|
-
self["#{name}_path"] = "#{self.class.to_s.underscore}/#{name}/#{_id}"
|
71
|
-
else
|
72
|
-
self["#{name}_path"] = "#{self.class.to_s.underscore}/#{name}/#{_id}"
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
# Roll through attachment definitions and check if they are a File or Tempfile. Both types are
|
77
|
-
# nescessary for file uploads to work properly. Each file checks for a <attr_name>_process
|
78
|
-
# callback for pre-processing before save.
|
79
|
-
def save_attachments
|
80
|
-
self.class.attachment_definitions.each do |definition|
|
81
|
-
attr_name, opts = definition
|
82
|
-
|
83
|
-
GridFS::GridStore.open(self.class.database, self["#{attr_name}_path"], 'w', :content_type => self["#{attr_name}_content_type"]) do |f|
|
84
|
-
f.write opts[:file].read
|
85
|
-
end
|
86
|
-
|
87
|
-
unless opts[:versions].nil?
|
88
|
-
opts[:versions].each do |version,dimensions|
|
89
|
-
|
90
|
-
tmp = Tempfile.new("#{attr_name}_#{version}")
|
91
|
-
image = Miso::Image.new(opts[:file].path)
|
92
|
-
|
93
|
-
image.crop(dimensions[:width], dimensions[:height]) if opts[:crop]
|
94
|
-
image.fit(dimensions[:width], dimensions[:height]) unless opts[:crop]
|
95
|
-
|
96
|
-
image.write(tmp.path)
|
97
|
-
|
98
|
-
# update <name>_<version> attrs and assign the file
|
99
|
-
send("#{attr_name}_#{version}=", tmp)
|
100
|
-
|
101
|
-
GridFS::GridStore.open(self.class.database, self["#{attr_name}_#{version}_path"], 'w', :content_type => self["#{attr_name}_content_type"]) do |f|
|
102
|
-
f.write tmp.read
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|
106
|
-
save_to_collection
|
107
|
-
end
|
108
|
-
end unless self.class.attachment_definitions.nil?
|
109
|
-
end
|
110
|
-
|
111
|
-
def destroy_attached_files
|
112
|
-
self.class.attachment_definitions.each do |name, attachment|
|
113
|
-
GridFS::GridStore.unlink(self.class.database, self["#{name}_path"])
|
114
|
-
unless attachment[:versions].nil?
|
115
|
-
attachment[:versions].each do |v,dim|
|
116
|
-
GridFS::GridStore.unlink(self.class.database, self["#{name}_#{v}_path"])
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end unless self.class.attachment_definitions.nil?
|
120
|
-
end
|
121
|
-
|
122
|
-
class Grip::InvalidFileException < Exception
|
123
|
-
end
|
124
|
-
|
125
8
|
end
|
9
|
+
|
10
|
+
%w{grip/attachment grip/has_attachment}.each { |lib| require lib }
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Grip
|
3
|
+
class Attachment
|
4
|
+
include MongoMapper::Document
|
5
|
+
|
6
|
+
belongs_to :owner, :polymorphic => true
|
7
|
+
many :attached_variants, :as => :owner, :class_name => "MongoMapper::Grip::Attachment", :dependent => :destroy
|
8
|
+
|
9
|
+
key :owner_id, ObjectId, :required => true
|
10
|
+
key :owner_type, String, :required => true
|
11
|
+
|
12
|
+
key :name, String
|
13
|
+
key :file_name, String
|
14
|
+
key :file_size, Integer
|
15
|
+
key :content_type, String
|
16
|
+
key :variants, Hash
|
17
|
+
|
18
|
+
after_save :build_variants
|
19
|
+
|
20
|
+
def file=new_file
|
21
|
+
raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
|
22
|
+
|
23
|
+
self.file_name = File.basename(new_file.path)
|
24
|
+
self.file_size = File.size(new_file.path)
|
25
|
+
self.content_type = MIME::Types.type_for(new_file.path)
|
26
|
+
|
27
|
+
write_to_grid new_file
|
28
|
+
end
|
29
|
+
|
30
|
+
def file
|
31
|
+
read_from_grid grid_key
|
32
|
+
end
|
33
|
+
|
34
|
+
def grid_key
|
35
|
+
"#{owner_type.pluralize}/#{owner_id}/#{name}".downcase
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.create_method sym, &block
|
39
|
+
define_method sym do |*block.args|
|
40
|
+
yield
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def build_variants
|
46
|
+
self.variants.each do |variant, dimensions|
|
47
|
+
|
48
|
+
eval <<-EOF
|
49
|
+
def #{variant}
|
50
|
+
Attachment.find_or_initialize_by_name_and_owner_id("#{variant.to_s}",self._id)
|
51
|
+
end
|
52
|
+
EOF
|
53
|
+
|
54
|
+
eval <<-EOF
|
55
|
+
def #{variant}=(file_hash)
|
56
|
+
new_attachment = Attachment.find_or_initialize_by_name_and_owner_id("#{variant.to_s}",self._id)
|
57
|
+
new_attachment.owner_type = self.class.to_s
|
58
|
+
new_attachment.file_name = File.basename(file_hash[:uploaded_file].path)
|
59
|
+
new_attachment.file_size = File.size(file_hash[:resized_file].path)
|
60
|
+
new_attachment.content_type = MIME::Types.type_for(file_hash[:uploaded_file].path)
|
61
|
+
new_attachment.save!
|
62
|
+
|
63
|
+
GridFS::GridStore.open(self.class.database, new_attachment.grid_key, 'w', :content_type => new_attachment.content_type) do |f|
|
64
|
+
f.write file_hash[:resized_file].read
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
EOF
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def write_to_grid new_file
|
74
|
+
GridFS::GridStore.open(self.class.database, grid_key, 'w', :content_type => content_type) do |f|
|
75
|
+
f.write new_file.read
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def read_from_grid key
|
80
|
+
GridFS::GridStore.open(self.class.database, key, 'r') { |f| f }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Grip
|
3
|
+
module HasAttachment
|
4
|
+
module ClassMethods
|
5
|
+
|
6
|
+
def has_grid_attachment name, opts={}
|
7
|
+
write_inheritable_attribute(:uploaded_files, {}) if uploaded_files.nil?
|
8
|
+
uploaded_files[name] = opts
|
9
|
+
|
10
|
+
define_method(name) do
|
11
|
+
attachments.find(:first,:conditions=>{:name=>name.to_s})
|
12
|
+
end
|
13
|
+
|
14
|
+
define_method("#{name}=") do |new_file|
|
15
|
+
raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
|
16
|
+
|
17
|
+
self['_id'] = Mongo::ObjectID.new if _id.blank?
|
18
|
+
new_attachment = Attachment.find_or_initialize_by_name_and_owner_id(name.to_s,self._id)
|
19
|
+
update_attachment_attributes!(new_attachment, new_file, opts)
|
20
|
+
self.class.uploaded_files[name][:file] = new_file
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def uploaded_files
|
26
|
+
read_inheritable_attribute(:uploaded_files)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
def self.included(base)
|
31
|
+
base.extend ClassMethods
|
32
|
+
base.class_eval do
|
33
|
+
after_save :save_attachments
|
34
|
+
before_destroy :destroy_attached_files
|
35
|
+
|
36
|
+
many :attachments, :as => :owner, :class_name => "MongoMapper::Grip::Attachment", :dependent => :destroy
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def update_attachment_attributes! new_attachment, new_file, opts
|
41
|
+
new_attachment.owner_type = self.class.to_s
|
42
|
+
new_attachment.file_name = File.basename(new_file.path)
|
43
|
+
new_attachment.file_size = File.size(new_file.path)
|
44
|
+
new_attachment.content_type = MIME::Types.type_for(new_file.path)
|
45
|
+
new_attachment.file = new_file
|
46
|
+
new_attachment.variants = opts[:variants] || {}
|
47
|
+
new_attachment.save!
|
48
|
+
end
|
49
|
+
|
50
|
+
def save_attachments
|
51
|
+
attachments.each do |attachment|
|
52
|
+
attachment.variants.each do |variant,dimensions|
|
53
|
+
create_variant(attachment,variant,dimensions)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def create_variant attachment, variant, dimensions
|
59
|
+
tmp_file = self.class.uploaded_files[attachment.name.to_sym][:file]
|
60
|
+
tmp = Tempfile.new("#{attachment.name}_#{variant}")
|
61
|
+
image = Miso::Image.new(tmp_file.path)
|
62
|
+
|
63
|
+
image.crop(dimensions[:width], dimensions[:height]) if dimensions[:crop]
|
64
|
+
image.fit(dimensions[:width], dimensions[:height]) unless dimensions[:crop]
|
65
|
+
|
66
|
+
image.write(tmp.path)
|
67
|
+
|
68
|
+
file_hash = {:resized_file => tmp,:uploaded_file => tmp_file}
|
69
|
+
|
70
|
+
attachment.send("#{variant}=", file_hash)
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/test/factories.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "mongo_mapper"
|
2
|
+
variants = { :thumb => {:width=>50,:height=>50} }
|
3
|
+
Factory.define :attachment do |a|
|
4
|
+
a.owner_id Mongo::ObjectID.new
|
5
|
+
a.owner_type 'Mock'
|
6
|
+
a.name 'image'
|
7
|
+
a.file_name 'mock.png'
|
8
|
+
a.file_size 100
|
9
|
+
a.content_type 'image/png'
|
10
|
+
a.variants variants
|
11
|
+
end
|
data/test/growler.rb
ADDED
data/test/models.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
include MongoMapper::Grip
|
4
|
+
|
5
|
+
class GripAttachmentTest < Test::Unit::TestCase
|
6
|
+
context "An Attachment" do
|
7
|
+
setup do
|
8
|
+
@attachment = Attachment.new( :name => "image", :owner_type => "Mock", :owner_id => Mongo::ObjectID.new )
|
9
|
+
@dir = File.dirname(__FILE__) + '/fixtures'
|
10
|
+
@image = File.open("#{@dir}/cthulhu.png", 'r')
|
11
|
+
@file_system = MongoMapper.database['fs.files']
|
12
|
+
end
|
13
|
+
|
14
|
+
teardown do
|
15
|
+
@file_system.drop
|
16
|
+
@image.close
|
17
|
+
end
|
18
|
+
|
19
|
+
should "be an Attachment" do
|
20
|
+
assert @attachment.is_a?(Attachment)
|
21
|
+
end
|
22
|
+
|
23
|
+
should "belong to :owner" do
|
24
|
+
name,assoc = Attachment.associations.first
|
25
|
+
#assert_equal(:owner, assoc.name)
|
26
|
+
#assert_equal(:belongs_to, assoc.type)
|
27
|
+
end
|
28
|
+
|
29
|
+
context "with a valid file" do
|
30
|
+
setup do
|
31
|
+
@attachment.file = @image
|
32
|
+
end
|
33
|
+
|
34
|
+
should "have correct :content_type" do
|
35
|
+
assert_equal("image/png", @attachment.content_type)
|
36
|
+
end
|
37
|
+
|
38
|
+
should "have correct :file_size" do
|
39
|
+
assert_equal(27582, @attachment.file_size)
|
40
|
+
end
|
41
|
+
|
42
|
+
should "have correct :file_name" do
|
43
|
+
assert_equal("cthulhu.png", @attachment.file_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
should "read file from grid store" do
|
47
|
+
assert_equal "image/png", @file_system.find_one(:filename => @attachment.grid_key)['contentType']
|
48
|
+
end
|
49
|
+
|
50
|
+
should "return a GridStore" do
|
51
|
+
assert_equal(GridFS::GridStore, @attachment.file.class)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
context "with an invalid file" do
|
57
|
+
should "raise Grip::InvalidFile" do
|
58
|
+
assert_raise(InvalidFile) { @attachment.file = Hash.new }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "models"
|
3
|
+
include MongoMapper::Grip
|
4
|
+
|
5
|
+
class HasAttachmentTest < Test::Unit::TestCase
|
6
|
+
context "A Doc that has_grid_attachment :image" do
|
7
|
+
setup do
|
8
|
+
@document = Doc.new
|
9
|
+
@dir = File.dirname(__FILE__) + '/fixtures'
|
10
|
+
@image = File.open("#{@dir}/cthulhu.png", 'r')
|
11
|
+
@file_system = MongoMapper.database['fs.files']
|
12
|
+
end
|
13
|
+
|
14
|
+
teardown do
|
15
|
+
@file_system.drop
|
16
|
+
@image.close
|
17
|
+
end
|
18
|
+
|
19
|
+
should "have many attachments" do
|
20
|
+
name,assoc = Doc.associations.first
|
21
|
+
assert_equal(:attachments, assoc.name)
|
22
|
+
assert_equal(:many, assoc.type)
|
23
|
+
end
|
24
|
+
|
25
|
+
should "have :after_save callback" do
|
26
|
+
assert_equal(1, Doc.after_save.collect(&:method).count)
|
27
|
+
end
|
28
|
+
|
29
|
+
should "have :before_destroy callback" do
|
30
|
+
assert_equal(1, Doc.before_destroy.collect(&:method).count)
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when assigned a file" do
|
34
|
+
setup do
|
35
|
+
@document.image = @image
|
36
|
+
@document.save!
|
37
|
+
end
|
38
|
+
|
39
|
+
should "should return an Attachment" do
|
40
|
+
assert_equal(MongoMapper::Grip::Attachment, @document.image.class)
|
41
|
+
end
|
42
|
+
|
43
|
+
should "read file from grid store" do
|
44
|
+
assert_equal "image/png", @file_system.find_one(:filename => @document.image.grid_key)['contentType']
|
45
|
+
end
|
46
|
+
|
47
|
+
should "have :thumb variant" do
|
48
|
+
assert @document.image.respond_to?( :thumb )
|
49
|
+
end
|
50
|
+
|
51
|
+
should "have 2 variants" do
|
52
|
+
assert_equal(2, @document.image.attached_variants.count)
|
53
|
+
end
|
54
|
+
|
55
|
+
should "be resized" do
|
56
|
+
assert @document.image.file_size > @document.image.thumb.file_size
|
57
|
+
assert @document.image.file_size > @document.image.super_thumb.file_size
|
58
|
+
end
|
59
|
+
|
60
|
+
should "retrieve variants from grid" do
|
61
|
+
assert_equal "image/png", @file_system.find_one(:filename => @document.image.thumb.grid_key)['contentType']
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
|
4
|
-
require 'mongo_mapper'
|
5
|
-
|
1
|
+
%w{test/unit shoulda factory_girl mongo_mapper factories}.each { |lib| require lib }
|
2
|
+
require 'growler'
|
6
3
|
require File.expand_path(File.dirname(__FILE__) + '/../lib/grip')
|
7
4
|
|
8
|
-
|
5
|
+
TEST_DB = 'test-grip'
|
6
|
+
|
7
|
+
MongoMapper.database = TEST_DB
|
9
8
|
|
10
9
|
class Test::Unit::TestCase
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
10
|
+
def teardown
|
11
|
+
MongoMapper.database.collections.each do |coll|
|
12
|
+
coll.remove
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Make sure that each test case has a teardown
|
17
|
+
# method to clear the db after each test.
|
18
|
+
def inherited(base)
|
19
|
+
base.define_method teardown do
|
20
|
+
super
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- twoism
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-12-
|
13
|
+
date: 2009-12-31 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -24,6 +24,7 @@ extra_rdoc_files:
|
|
24
24
|
- LICENSE
|
25
25
|
- README.rdoc
|
26
26
|
files:
|
27
|
+
- .DS_Store
|
27
28
|
- .gitignore
|
28
29
|
- LICENSE
|
29
30
|
- README.rdoc
|
@@ -31,9 +32,15 @@ files:
|
|
31
32
|
- VERSION
|
32
33
|
- grip.gemspec
|
33
34
|
- lib/grip.rb
|
35
|
+
- lib/grip/attachment.rb
|
36
|
+
- lib/grip/has_attachment.rb
|
37
|
+
- test/factories.rb
|
34
38
|
- test/fixtures/cthulhu.png
|
35
39
|
- test/fixtures/sample.pdf
|
36
|
-
- test/
|
40
|
+
- test/growler.rb
|
41
|
+
- test/models.rb
|
42
|
+
- test/test_grip_attachment.rb
|
43
|
+
- test/test_has_attachment.rb
|
37
44
|
- test/test_helper.rb
|
38
45
|
has_rdoc: true
|
39
46
|
homepage: http://github.com/twoism/grip
|
@@ -64,5 +71,9 @@ signing_key:
|
|
64
71
|
specification_version: 3
|
65
72
|
summary: GridFS attachments for MongoMapper
|
66
73
|
test_files:
|
67
|
-
- test/
|
74
|
+
- test/factories.rb
|
75
|
+
- test/growler.rb
|
76
|
+
- test/models.rb
|
77
|
+
- test/test_grip_attachment.rb
|
78
|
+
- test/test_has_attachment.rb
|
68
79
|
- test/test_helper.rb
|
data/test/test_grip.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
class Foo
|
4
|
-
include MongoMapper::Document
|
5
|
-
include Grip
|
6
|
-
|
7
|
-
has_grid_attachment :image, :versions => {:thumb => {:width=>50,:height=>50}}
|
8
|
-
has_grid_attachment :pdf
|
9
|
-
|
10
|
-
def process_image opts
|
11
|
-
opts[:file]
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class GripTest < Test::Unit::TestCase
|
16
|
-
def setup
|
17
|
-
MongoMapper.connection.drop_database "test-attachments"
|
18
|
-
MongoMapper.database = "test-attachments"
|
19
|
-
|
20
|
-
dir = File.dirname(__FILE__) + '/fixtures'
|
21
|
-
@pdf = File.open("#{dir}/sample.pdf", 'r')
|
22
|
-
@image = File.open("#{dir}/cthulhu.png", 'r')
|
23
|
-
|
24
|
-
@doc = Foo.create(:image => @image, :pdf => @pdf)
|
25
|
-
@doc.reload
|
26
|
-
end
|
27
|
-
|
28
|
-
def teardown
|
29
|
-
@pdf.close
|
30
|
-
@image.close
|
31
|
-
end
|
32
|
-
|
33
|
-
test "assigns keys correctly" do
|
34
|
-
assert_equal 27582, @doc.image_size
|
35
|
-
assert_equal 8775, @doc.pdf_size
|
36
|
-
|
37
|
-
assert_equal 'cthulhu.png', @doc.image_name
|
38
|
-
assert_equal 'sample.pdf', @doc.pdf_name
|
39
|
-
|
40
|
-
assert_equal "image/png", @doc.image_content_type
|
41
|
-
assert_equal "image/png", @doc.image_thumb_content_type
|
42
|
-
assert_equal "application/pdf", @doc.pdf_content_type
|
43
|
-
|
44
|
-
assert_equal "foo/image/#{@doc.id}", @doc.image_path
|
45
|
-
assert_equal "foo/image_thumb/#{@doc.id}", @doc.image_thumb_path
|
46
|
-
assert_equal "foo/pdf/#{@doc.id}", @doc.pdf_path
|
47
|
-
|
48
|
-
collection = MongoMapper.database['fs.files']
|
49
|
-
|
50
|
-
assert_equal "image/png", collection.find_one(:filename => @doc.image_path)['contentType']
|
51
|
-
assert_equal "application/pdf", collection.find_one(:filename => @doc.pdf_path)['contentType']
|
52
|
-
end
|
53
|
-
|
54
|
-
test "responds to dynamic keys" do
|
55
|
-
[ :pdf_size, :pdf_path, :pdf_name, :pdf_content_type,
|
56
|
-
:image_size, :image_path, :image_name, :image_content_type,
|
57
|
-
:image_thumb_size, :image_thumb_path, :image_thumb_name, :image_thumb_content_type
|
58
|
-
].each do |method|
|
59
|
-
assert @doc.respond_to?(method)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
test "callbacks assigned only once each" do
|
64
|
-
assert_equal(1, Foo.after_save.collect(&:method).count)
|
65
|
-
assert_equal(1, Foo.before_destroy.collect(&:method).count)
|
66
|
-
end
|
67
|
-
|
68
|
-
test "saves attachments correctly" do
|
69
|
-
assert_equal "image/png", @doc.image.content_type
|
70
|
-
assert_equal "application/pdf", @doc.pdf.content_type
|
71
|
-
|
72
|
-
assert GridFS::GridStore.exist?(MongoMapper.database, @doc.image_path)
|
73
|
-
assert GridFS::GridStore.exist?(MongoMapper.database, @doc.pdf_path)
|
74
|
-
end
|
75
|
-
|
76
|
-
test "cleans up attachments on destroy" do
|
77
|
-
assert GridFS::GridStore.exist?(MongoMapper.database, @doc.image_path)
|
78
|
-
assert GridFS::GridStore.exist?(MongoMapper.database, @doc.pdf_path)
|
79
|
-
|
80
|
-
@doc.destroy
|
81
|
-
|
82
|
-
assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.image_path)
|
83
|
-
assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.image_thumb_path)
|
84
|
-
assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.pdf_path)
|
85
|
-
end
|
86
|
-
|
87
|
-
test "should raise invalid file exception" do
|
88
|
-
assert_raise(Grip::InvalidFileException) { @doc.image = ""; @doc.save! }
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|