grip 0.6.2 → 0.6.3
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/VERSION +1 -1
- data/grip.gemspec +1 -1
- data/lib/grip.rb +3 -5
- data/lib/grip/attachment.rb +73 -75
- data/lib/grip/has_attachment.rb +64 -66
- data/test/grip_attachment_test.rb +1 -1
- data/test/has_attachment_test.rb +2 -2
- data/test/models.rb +1 -1
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.3
|
data/grip.gemspec
CHANGED
data/lib/grip.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
%w{mongo_mapper mongo/gridfs mime/types ftools tempfile RMagick miso}.each { |lib| require lib }
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
class InvalidFile < GripError; end
|
7
|
-
end
|
3
|
+
module Grip
|
4
|
+
class GripError < StandardError; end
|
5
|
+
class InvalidFile < GripError; end
|
8
6
|
end
|
9
7
|
|
10
8
|
%w{grip/attachment grip/has_attachment}.each { |lib| require lib }
|
data/lib/grip/attachment.rb
CHANGED
@@ -1,88 +1,86 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
include MongoMapper::Document
|
1
|
+
module Grip
|
2
|
+
class Attachment
|
3
|
+
include MongoMapper::Document
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
5
|
+
belongs_to :owner,
|
6
|
+
:polymorphic => true
|
7
|
+
|
8
|
+
many :attached_variants,
|
9
|
+
:as => :owner,
|
10
|
+
:class_name => "Grip::Attachment",
|
11
|
+
:dependent => :destroy
|
12
|
+
|
13
|
+
key :owner_id, ObjectId, :required => true
|
14
|
+
key :owner_type, String, :required => true
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
self.file_name = File.basename(new_file.path)
|
30
|
-
self.file_size = File.size(new_file.path)
|
31
|
-
self.content_type = MIME::Types.type_for(new_file.path)
|
32
|
-
|
33
|
-
write_to_grid self,new_file
|
34
|
-
end
|
35
|
-
|
36
|
-
def file
|
37
|
-
read_from_grid grid_key
|
38
|
-
end
|
16
|
+
key :name, String
|
17
|
+
key :file_name, String
|
18
|
+
key :file_size, Integer
|
19
|
+
key :content_type, String
|
20
|
+
key :variants, Hash
|
21
|
+
|
22
|
+
after_save :build_variants
|
23
|
+
before_destroy :destroy_file
|
24
|
+
|
25
|
+
def file=new_file
|
26
|
+
raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
|
39
27
|
|
40
|
-
|
41
|
-
|
42
|
-
|
28
|
+
self.file_name = File.basename(new_file.path)
|
29
|
+
self.file_size = File.size(new_file.path)
|
30
|
+
self.content_type = MIME::Types.type_for(new_file.path)
|
43
31
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
32
|
+
write_to_grid self,new_file
|
33
|
+
end
|
34
|
+
|
35
|
+
def file
|
36
|
+
read_from_grid grid_key
|
37
|
+
end
|
38
|
+
|
39
|
+
def grid_key
|
40
|
+
"#{owner_type.pluralize}/#{owner_id}/#{name}".downcase
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.create_method method_name, &block
|
44
|
+
define_method(method_name) do |*args|
|
45
|
+
yield *args
|
48
46
|
end
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def build_variants
|
51
|
+
self.variants.each do |variant, dimensions|
|
52
|
+
|
53
|
+
self.class.create_method variant.to_sym do
|
54
|
+
attached_variants.find_or_create_by_name(:name=>"#{variant.to_s}")
|
55
|
+
end
|
56
|
+
|
57
|
+
self.class.create_method "#{variant}=".to_sym do |file_hash|
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
new_attachment.content_type = MIME::Types.type_for(file_hash[:uploaded_file].path)
|
65
|
-
new_attachment.save!
|
59
|
+
new_attachment = Attachment.find_or_initialize_by_name_and_owner_id("#{variant.to_s}",self._id)
|
60
|
+
new_attachment.owner_type = self.class.to_s
|
61
|
+
new_attachment.file_name = File.basename(file_hash[:uploaded_file].path)
|
62
|
+
new_attachment.file_size = File.size(file_hash[:resized_file].path)
|
63
|
+
new_attachment.content_type = MIME::Types.type_for(file_hash[:uploaded_file].path)
|
64
|
+
new_attachment.save!
|
66
65
|
|
67
|
-
|
68
|
-
end
|
69
|
-
|
66
|
+
write_to_grid new_attachment, file_hash[:resized_file]
|
70
67
|
end
|
71
|
-
end
|
72
68
|
|
73
|
-
def destroy_file
|
74
|
-
GridFS::GridStore.unlink(self.class.database, grid_key)
|
75
69
|
end
|
70
|
+
end
|
76
71
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
GridFS::GridStore.open(self.class.database, key, 'r') { |f| f }
|
72
|
+
def destroy_file
|
73
|
+
GridFS::GridStore.unlink(self.class.database, grid_key)
|
74
|
+
end
|
75
|
+
|
76
|
+
def write_to_grid attachment, new_file
|
77
|
+
GridFS::GridStore.open(self.class.database, attachment.grid_key, 'w', :content_type => attachment.content_type) do |f|
|
78
|
+
f.write new_file.read
|
85
79
|
end
|
86
|
-
|
80
|
+
end
|
81
|
+
|
82
|
+
def read_from_grid key
|
83
|
+
GridFS::GridStore.open(self.class.database, key, 'r') { |f| f }
|
84
|
+
end
|
87
85
|
end
|
88
|
-
end
|
86
|
+
end
|
data/lib/grip/has_attachment.rb
CHANGED
@@ -1,80 +1,78 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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.class.uploaded_files[name][:file] = new_file
|
18
|
-
self['_id'] = Mongo::ObjectID.new if _id.blank?
|
19
|
-
new_attachment = attachments.find_or_create_by_name(name.to_s)
|
20
|
-
update_attachment_attributes!(new_attachment, new_file, opts)
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
1
|
+
module Grip
|
2
|
+
module HasAttachment
|
3
|
+
module ClassMethods
|
4
|
+
|
5
|
+
def has_grid_attachment name, opts={}
|
6
|
+
write_inheritable_attribute(:uploaded_files, {}) if uploaded_files.nil?
|
7
|
+
uploaded_files[name] = opts
|
24
8
|
|
25
|
-
|
26
|
-
|
9
|
+
define_method(name) do
|
10
|
+
attachments.find(:first,:conditions=>{:name=>name.to_s})
|
27
11
|
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
:class_name => "MongoMapper::Grip::Attachment",
|
37
|
-
:dependent => :destroy
|
12
|
+
|
13
|
+
define_method("#{name}=") do |new_file|
|
14
|
+
raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
|
15
|
+
|
16
|
+
self.class.uploaded_files[name][:file] = new_file
|
17
|
+
self['_id'] = Mongo::ObjectID.new if _id.blank?
|
18
|
+
new_attachment = attachments.find_or_create_by_name(name.to_s)
|
19
|
+
update_attachment_attributes!(new_attachment, new_file, opts)
|
38
20
|
end
|
21
|
+
|
39
22
|
end
|
40
23
|
|
41
|
-
def
|
42
|
-
|
43
|
-
new_attachment.file_name = File.basename(new_file.path)
|
44
|
-
new_attachment.file_size = File.size(new_file.path)
|
45
|
-
new_attachment.content_type = MIME::Types.type_for(new_file.path)
|
46
|
-
new_attachment.file = new_file
|
47
|
-
new_attachment.variants = opts[:variants] || {}
|
48
|
-
new_attachment.save!
|
24
|
+
def uploaded_files
|
25
|
+
read_inheritable_attribute(:uploaded_files)
|
49
26
|
end
|
50
27
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
28
|
+
end
|
29
|
+
def self.included(base)
|
30
|
+
base.extend ClassMethods
|
31
|
+
base.class_eval do
|
32
|
+
after_save :save_attachments
|
33
|
+
many :attachments,
|
34
|
+
:as => :owner,
|
35
|
+
:class_name => "Grip::Attachment",
|
36
|
+
: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)
|
56
54
|
end
|
57
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
|
+
begin
|
61
|
+
tmp = Tempfile.new("#{attachment.name}_#{variant}")
|
62
|
+
image = Miso::Image.new(tmp_file.path)
|
58
63
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
image.fit(dimensions[:width], dimensions[:height]) unless dimensions[:crop]
|
67
|
-
|
68
|
-
image.write(tmp.path)
|
69
|
-
rescue RuntimeError => e
|
70
|
-
warn "Image was not resized. #{e}"
|
71
|
-
tmp = tmp_file
|
72
|
-
end
|
73
|
-
|
74
|
-
file_hash = {:resized_file => tmp,:uploaded_file => tmp_file}
|
75
|
-
attachment.send("#{variant}=", file_hash)
|
64
|
+
image.crop(dimensions[:width], dimensions[:height]) if dimensions[:crop]
|
65
|
+
image.fit(dimensions[:width], dimensions[:height]) unless dimensions[:crop]
|
66
|
+
|
67
|
+
image.write(tmp.path)
|
68
|
+
rescue RuntimeError => e
|
69
|
+
warn "Image was not resized. #{e}"
|
70
|
+
tmp = tmp_file
|
76
71
|
end
|
77
72
|
|
73
|
+
file_hash = {:resized_file => tmp,:uploaded_file => tmp_file}
|
74
|
+
attachment.send("#{variant}=", file_hash)
|
78
75
|
end
|
76
|
+
|
79
77
|
end
|
80
|
-
end
|
78
|
+
end
|
data/test/has_attachment_test.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
require "models"
|
3
|
-
include
|
3
|
+
include Grip
|
4
4
|
|
5
5
|
class HasAttachmentTest < Test::Unit::TestCase
|
6
6
|
context "A Doc that has_grid_attachment :image" do
|
@@ -33,7 +33,7 @@ class HasAttachmentTest < Test::Unit::TestCase
|
|
33
33
|
end
|
34
34
|
|
35
35
|
should "should return an Attachment" do
|
36
|
-
assert_equal(
|
36
|
+
assert_equal(Grip::Attachment, @document.image.class)
|
37
37
|
end
|
38
38
|
|
39
39
|
should "read file from grid store" do
|
data/test/models.rb
CHANGED