grip 0.4.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/VERSION +1 -1
- data/grip.gemspec +2 -2
- data/lib/grip.rb +73 -38
- data/test/test_grip.rb +11 -2
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -15,7 +15,7 @@ The grip gem is hosted on gemcutter.org:
|
|
15
15
|
include MongoMapper::Document
|
16
16
|
include Grip
|
17
17
|
|
18
|
-
has_grid_attachment :image, :
|
18
|
+
has_grid_attachment :image, :versions => {:thumb => {:width=>50,:height=>50}}
|
19
19
|
has_grid_attachment :pdf
|
20
20
|
|
21
21
|
# Hook for resizing or whatever gets you there.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.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.5.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-30}
|
13
13
|
s.description = %q{GridFS attachments for MongoMapper}
|
14
14
|
s.email = %q{signalstatic@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/grip.rb
CHANGED
@@ -1,28 +1,14 @@
|
|
1
1
|
require 'mongo/gridfs'
|
2
2
|
require 'mime/types'
|
3
3
|
require 'tempfile'
|
4
|
-
|
5
|
-
# if thumbnailable?
|
6
|
-
# tmp = Tempfile.new("thumb_#{filename}")
|
7
|
-
# MojoMagick::resize(uploaded_file.path, tmp.path, {:width => 50, :height => 40, :scale => '>'})
|
8
|
-
# self.thumbnail = tmp.read
|
9
|
-
# end
|
10
|
-
|
11
|
-
# open : db, name, mode, options (:root, :metadata, :content_type)
|
12
|
-
# read : db, name, length, offset
|
13
|
-
# unlink : db, names
|
14
|
-
# list : db, root collection
|
15
|
-
#
|
16
|
-
# GridStore.open(database, 'filename', 'w') { |f|
|
17
|
-
# f.puts "Hello, world!"
|
18
|
-
# }
|
4
|
+
require 'mojo_magick'
|
19
5
|
|
20
6
|
module Grip
|
21
7
|
def self.included(base)
|
22
8
|
base.extend Grip::ClassMethods
|
23
9
|
base.class_eval do
|
24
|
-
after_save
|
25
|
-
before_destroy
|
10
|
+
after_save :save_attachments
|
11
|
+
before_destroy :destroy_attached_files
|
26
12
|
end
|
27
13
|
end
|
28
14
|
|
@@ -31,26 +17,40 @@ module Grip
|
|
31
17
|
write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
|
32
18
|
attachment_definitions[name] = opts
|
33
19
|
|
34
|
-
|
35
|
-
key "#{name}_path".to_sym, String
|
36
|
-
key "#{name}_name".to_sym, String
|
37
|
-
key "#{name}_content_type".to_sym, String
|
20
|
+
build_attachment_keys_for name
|
38
21
|
|
39
22
|
define_method(name) do
|
40
|
-
# open returns the correct mime-type,
|
41
|
-
# read returns a string. Not sure if
|
42
|
-
# this is a GridFS problem or not
|
43
23
|
GridFS::GridStore.open(self.class.database, self["#{name}_path"], 'r') {|f| f }
|
44
24
|
end
|
45
25
|
|
46
26
|
define_method("#{name}=") do |file|
|
47
|
-
|
48
|
-
self["#{name}_size"] = File.size(file)
|
49
|
-
self["#{name}_name"] = File.basename(file.path)
|
50
|
-
self["#{name}_path"] = "#{self.class.to_s.underscore}/#{name}/#{_id}"
|
51
|
-
self["#{name}_content_type"] = file.content_type rescue MIME::Types.type_for(self["#{name}_name"]).to_s
|
27
|
+
update_attachment_attributes!(name, file)
|
52
28
|
self.class.attachment_definitions[name][:file] = file
|
53
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
54
|
end
|
55
55
|
|
56
56
|
def attachment_definitions
|
@@ -58,23 +58,58 @@ module Grip
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
+
def update_attachment_attributes! name, file, is_version = false
|
62
|
+
raise Grip::InvalidFileException unless (file.is_a?(File) || file.is_a?(Tempfile))
|
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.
|
61
79
|
def save_attachments
|
62
|
-
self.class.attachment_definitions.each do |
|
80
|
+
self.class.attachment_definitions.each do |definition|
|
81
|
+
attr_name, opts = definition
|
63
82
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
+
tmp = Tempfile.new("#{attr_name}_#{version}")
|
90
|
+
MojoMagick::resize(opts[:file].path, tmp.path, dimensions)
|
91
|
+
send("#{attr_name}_#{version}=", tmp)
|
92
|
+
GridFS::GridStore.open(self.class.database, self["#{attr_name}_#{version}_path"], 'w', :content_type => self["#{attr_name}_content_type"]) do |f|
|
93
|
+
f.write tmp.read
|
94
|
+
end
|
70
95
|
end
|
96
|
+
save_to_collection
|
71
97
|
end
|
72
|
-
end
|
98
|
+
end unless self.class.attachment_definitions.nil?
|
73
99
|
end
|
74
100
|
|
75
101
|
def destroy_attached_files
|
76
102
|
self.class.attachment_definitions.each do |name, attachment|
|
77
103
|
GridFS::GridStore.unlink(self.class.database, self["#{name}_path"])
|
78
|
-
|
104
|
+
unless attachment[:versions].nil?
|
105
|
+
attachment[:versions].each do |v,dim|
|
106
|
+
GridFS::GridStore.unlink(self.class.database, self["#{name}_#{v}_path"])
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end unless self.class.attachment_definitions.nil?
|
110
|
+
end
|
111
|
+
|
112
|
+
class Grip::InvalidFileException < Exception
|
79
113
|
end
|
114
|
+
|
80
115
|
end
|
data/test/test_grip.rb
CHANGED
@@ -4,7 +4,7 @@ class Foo
|
|
4
4
|
include MongoMapper::Document
|
5
5
|
include Grip
|
6
6
|
|
7
|
-
has_grid_attachment :image, :
|
7
|
+
has_grid_attachment :image, :versions => {:thumb => {:width=>50,:height=>50}}
|
8
8
|
has_grid_attachment :pdf
|
9
9
|
|
10
10
|
def process_image opts
|
@@ -38,9 +38,11 @@ class GripTest < Test::Unit::TestCase
|
|
38
38
|
assert_equal 'sample.pdf', @doc.pdf_name
|
39
39
|
|
40
40
|
assert_equal "image/png", @doc.image_content_type
|
41
|
+
assert_equal "image/png", @doc.image_thumb_content_type
|
41
42
|
assert_equal "application/pdf", @doc.pdf_content_type
|
42
43
|
|
43
44
|
assert_equal "foo/image/#{@doc.id}", @doc.image_path
|
45
|
+
assert_equal "foo/image_thumb/#{@doc.id}", @doc.image_thumb_path
|
44
46
|
assert_equal "foo/pdf/#{@doc.id}", @doc.pdf_path
|
45
47
|
|
46
48
|
collection = MongoMapper.database['fs.files']
|
@@ -51,7 +53,8 @@ class GripTest < Test::Unit::TestCase
|
|
51
53
|
|
52
54
|
test "responds to dynamic keys" do
|
53
55
|
[ :pdf_size, :pdf_path, :pdf_name, :pdf_content_type,
|
54
|
-
:image_size, :image_path, :image_name, :image_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
|
55
58
|
].each do |method|
|
56
59
|
assert @doc.respond_to?(method)
|
57
60
|
end
|
@@ -77,6 +80,12 @@ class GripTest < Test::Unit::TestCase
|
|
77
80
|
@doc.destroy
|
78
81
|
|
79
82
|
assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.image_path)
|
83
|
+
assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.image_thumb_path)
|
80
84
|
assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.pdf_path)
|
81
85
|
end
|
86
|
+
|
87
|
+
test "should raise invalid file exception" do
|
88
|
+
assert_raise(Grip::InvalidFileException) { @doc.image = ""; @doc.save! }
|
89
|
+
end
|
90
|
+
|
82
91
|
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.5.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-30 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|