grip 0.6.3 → 0.6.4
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/.gitignore +2 -1
- data/VERSION +1 -1
- data/grip.gemspec +4 -4
- data/lib/grip/has_attachment.rb +63 -55
- data/test/fixtures/fennec-fox.jpg +0 -0
- data/test/has_attachment_test.rb +22 -3
- data/test/test_helper.rb +4 -2
- metadata +3 -3
- data/.DS_Store +0 -0
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.4
|
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.6.
|
8
|
+
s.version = "0.6.4"
|
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{2010-01-
|
12
|
+
s.date = %q{2010-01-09}
|
13
13
|
s.description = %q{GridFS attachments for MongoMapper}
|
14
14
|
s.email = %q{signalstatic@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,8 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.markdown"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
".
|
21
|
-
".gitignore",
|
20
|
+
".gitignore",
|
22
21
|
"LICENSE",
|
23
22
|
"README.markdown",
|
24
23
|
"Rakefile",
|
@@ -29,6 +28,7 @@ Gem::Specification.new do |s|
|
|
29
28
|
"lib/grip/has_attachment.rb",
|
30
29
|
"test/factories.rb",
|
31
30
|
"test/fixtures/cthulhu.png",
|
31
|
+
"test/fixtures/fennec-fox.jpg",
|
32
32
|
"test/fixtures/sample.pdf",
|
33
33
|
"test/grip_attachment_test.rb",
|
34
34
|
"test/growler.rb",
|
data/lib/grip/has_attachment.rb
CHANGED
@@ -1,78 +1,86 @@
|
|
1
1
|
module Grip
|
2
2
|
module HasAttachment
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
base.instance_eval do
|
7
|
+
after_save :save_attachments
|
8
|
+
many :attachments,
|
9
|
+
:as => :owner,
|
10
|
+
:class_name => "Grip::Attachment",
|
11
|
+
:dependent => :destroy
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
3
15
|
module ClassMethods
|
4
16
|
|
5
17
|
def has_grid_attachment name, opts={}
|
6
|
-
write_inheritable_attribute(:
|
7
|
-
|
8
|
-
|
18
|
+
write_inheritable_attribute(:uploaded_file_options, {}) if uploaded_file_options.nil?
|
19
|
+
uploaded_file_options[name] = opts
|
20
|
+
self.send(:include, InstanceMethods)
|
9
21
|
define_method(name) do
|
10
|
-
attachments.find(:first
|
22
|
+
attachments.find(:first, :conditions=>{:name => name.to_s})
|
11
23
|
end
|
12
|
-
|
13
|
-
define_method("#{name}=") do |new_file|
|
24
|
+
define_method("#{name}=") do |new_file|
|
14
25
|
raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
|
15
|
-
|
16
|
-
|
17
|
-
self['_id']
|
18
|
-
new_attachment
|
26
|
+
uploaded_files[name] ||= {}
|
27
|
+
uploaded_files[name][:file] = new_file
|
28
|
+
self['_id'] = Mongo::ObjectID.new if _id.blank?
|
29
|
+
new_attachment = attachments.find_or_create_by_name(name.to_s)
|
19
30
|
update_attachment_attributes!(new_attachment, new_file, opts)
|
20
31
|
end
|
21
32
|
|
22
33
|
end
|
23
34
|
|
24
|
-
def
|
25
|
-
read_inheritable_attribute(:
|
35
|
+
def uploaded_file_options
|
36
|
+
read_inheritable_attribute(:uploaded_file_options)
|
26
37
|
end
|
27
38
|
|
28
39
|
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
40
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
41
|
+
module InstanceMethods
|
42
|
+
def uploaded_files
|
43
|
+
@uploaded_files ||= {}
|
44
|
+
end
|
45
|
+
def update_attachment_attributes! new_attachment, new_file, opts
|
46
|
+
new_attachment.owner_type = self.class.to_s
|
47
|
+
new_attachment.file_name = File.basename(new_file.path)
|
48
|
+
new_attachment.file_size = File.size(new_file.path)
|
49
|
+
new_attachment.content_type = MIME::Types.type_for(new_file.path)
|
50
|
+
new_attachment.file = new_file
|
51
|
+
new_attachment.variants = opts[:variants] || {}
|
52
|
+
new_attachment.save!
|
53
|
+
end
|
54
|
+
|
55
|
+
def save_attachments
|
56
|
+
attachments.each do |attachment|
|
57
|
+
attachment.variants.each do |variant,dimensions|
|
58
|
+
create_variant(attachment,variant,dimensions)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def create_variant attachment, variant, dimensions
|
64
|
+
tmp_file = uploaded_files[attachment.name.to_sym][:file]
|
65
|
+
begin
|
66
|
+
tmp = Tempfile.new("#{attachment.name}_#{variant}")
|
67
|
+
image = Miso::Image.new(tmp_file.path)
|
68
|
+
|
69
|
+
image.crop(dimensions[:width], dimensions[:height]) if dimensions[:crop]
|
70
|
+
image.fit(dimensions[:width], dimensions[:height]) unless dimensions[:crop]
|
71
|
+
|
72
|
+
image.write(tmp.path)
|
73
|
+
rescue RuntimeError => e
|
74
|
+
warn "Image was not resized. #{e}"
|
75
|
+
tmp = tmp_file
|
76
|
+
end
|
77
|
+
|
78
|
+
file_hash = {:resized_file => tmp,:uploaded_file => tmp_file}
|
79
|
+
attachment.send("#{variant}=", file_hash)
|
54
80
|
end
|
81
|
+
|
55
82
|
end
|
56
|
-
end
|
57
83
|
|
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)
|
63
|
-
|
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
|
71
|
-
end
|
72
|
-
|
73
|
-
file_hash = {:resized_file => tmp,:uploaded_file => tmp_file}
|
74
|
-
attachment.send("#{variant}=", file_hash)
|
75
|
-
end
|
76
84
|
|
77
85
|
end
|
78
86
|
end
|
Binary file
|
data/test/has_attachment_test.rb
CHANGED
@@ -15,13 +15,17 @@ class HasAttachmentTest < Test::Unit::TestCase
|
|
15
15
|
@file_system.drop
|
16
16
|
@image.close
|
17
17
|
end
|
18
|
+
|
19
|
+
should "respond to uploaded files" do
|
20
|
+
assert @document.respond_to?(:uploaded_files)
|
21
|
+
end
|
18
22
|
|
19
23
|
should "have many attachments" do
|
20
24
|
name,assoc = Doc.associations.first
|
21
25
|
assert_equal(:attachments, assoc.name)
|
22
26
|
assert_equal(:many, assoc.type)
|
23
27
|
end
|
24
|
-
|
28
|
+
|
25
29
|
should "have :after_save callback" do
|
26
30
|
assert_equal(1, Doc.after_save.collect(&:method).count)
|
27
31
|
end
|
@@ -47,18 +51,33 @@ class HasAttachmentTest < Test::Unit::TestCase
|
|
47
51
|
should "have 2 variants" do
|
48
52
|
assert_equal(2, @document.image.attached_variants.count)
|
49
53
|
end
|
50
|
-
|
54
|
+
|
51
55
|
should "be resized" do
|
52
56
|
assert @document.image.file_size > @document.image.thumb.file_size
|
53
57
|
assert @document.image.file_size > @document.image.super_thumb.file_size
|
54
58
|
end
|
55
|
-
|
59
|
+
|
56
60
|
should "retrieve variants from grid" do
|
57
61
|
assert_equal "image/png", @file_system.find_one(:filename => @document.image.thumb.grid_key)['contentType']
|
58
62
|
end
|
59
63
|
|
60
64
|
end
|
61
65
|
|
66
|
+
context "when multiple instances" do
|
67
|
+
setup do
|
68
|
+
@document2 = Doc.new
|
69
|
+
@image2 = File.open("#{@dir}/fennec-fox.jpg", 'r')
|
70
|
+
@document3 = Doc.new
|
71
|
+
@image3 = File.open("#{@dir}/cthulhu.png", 'r')
|
72
|
+
@document2.image = @image2
|
73
|
+
@document3.image = @image3
|
74
|
+
end
|
75
|
+
|
76
|
+
should "not overwrite each other" do
|
77
|
+
assert_not_equal @document2.uploaded_files[:image][:file], @document3.uploaded_files[:image][:file]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
62
81
|
end
|
63
82
|
|
64
83
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
1
2
|
require 'rubygems'
|
2
|
-
|
3
|
+
require 'grip'
|
3
4
|
require 'growler'
|
4
|
-
|
5
|
+
%w{test/unit shoulda factory_girl mongo_mapper factories}.each { |lib| require lib }
|
6
|
+
|
5
7
|
|
6
8
|
TEST_DB = 'test-grip'
|
7
9
|
|
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.6.
|
4
|
+
version: 0.6.4
|
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: 2010-01-
|
13
|
+
date: 2010-01-09 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -43,7 +43,6 @@ extra_rdoc_files:
|
|
43
43
|
- LICENSE
|
44
44
|
- README.markdown
|
45
45
|
files:
|
46
|
-
- .DS_Store
|
47
46
|
- .gitignore
|
48
47
|
- LICENSE
|
49
48
|
- README.markdown
|
@@ -55,6 +54,7 @@ files:
|
|
55
54
|
- lib/grip/has_attachment.rb
|
56
55
|
- test/factories.rb
|
57
56
|
- test/fixtures/cthulhu.png
|
57
|
+
- test/fixtures/fennec-fox.jpg
|
58
58
|
- test/fixtures/sample.pdf
|
59
59
|
- test/grip_attachment_test.rb
|
60
60
|
- test/growler.rb
|
data/.DS_Store
DELETED
Binary file
|