grip 0.6.1 → 0.6.2

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 CHANGED
Binary file
data/README.markdown ADDED
@@ -0,0 +1,71 @@
1
+ grip
2
+ ====
3
+
4
+ GridFS attachments for MongoMapper.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ The grip gem is hosted on gemcutter.org and uses Miso (http://github.com/Fingertips/Miso) for image resizing
10
+
11
+ gem install miso grip
12
+
13
+ Setup (Rails)
14
+ -------------
15
+
16
+ # config/environment.rb
17
+
18
+ config.gem 'grip'
19
+
20
+ Then:
21
+
22
+ rake gems:install
23
+
24
+
25
+
26
+
27
+
28
+ Usage (See tests for better docs)
29
+ ---------------------------------
30
+
31
+ Create a MongoMapper model and `include MongoMapper::Grip::HasAttachment` and call `has_grid_attachment :<attachment_name>` to the model. Then if your attachment is an image, you can specify a hash of variants along with their :width & :height.
32
+
33
+
34
+ class Doc
35
+ include MongoMapper::Document
36
+ include MongoMapper::Grip::HasAttachment
37
+ has_grid_attachment :image, :variants => {:thumb => {:width=>50,:height=>50}}
38
+ end
39
+
40
+ To save a file to your model, just send any file to the symbol that you specified.
41
+
42
+ image = File.open('foo.png', 'r')
43
+ @doc = Doc.create(:image => image)
44
+
45
+ Alternately just set it on the model and save:
46
+
47
+ @doc.image = image
48
+
49
+ Each attachment as well as variants respond to the following methods:
50
+
51
+ puts @doc.image.name
52
+ => "image"
53
+
54
+ puts @doc.image.file_size
55
+ => 100
56
+
57
+ puts @doc.image.file_name
58
+ => "foo.png"
59
+
60
+ puts @doc.image.grid_key
61
+ => "docs/<id>/image"
62
+
63
+ @doc.image.file # contents read from gridfs for serving from rack/metal/controller
64
+
65
+ Variants are created `after_save` and can be referenced through the attachment they belong to:
66
+
67
+ @doc.image.thumb.name
68
+ @doc.image.thumb.file_name
69
+ @doc.image.thumb.grid_key
70
+ @doc.image.thumb.content_type
71
+
data/Rakefile CHANGED
@@ -12,10 +12,12 @@ begin
12
12
  g.email = 'signalstatic@gmail.com'
13
13
  g.homepage = 'http://github.com/twoism/grip'
14
14
  g.authors = %w(twoism jnunemaker)
15
+ g.add_dependency 'rmagick'
16
+ g.add_dependency 'miso'
15
17
  end
16
18
  Jeweler::GemcutterTasks.new
17
19
  rescue LoadError
18
- puts 'Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com'
20
+ puts 'Jeweler not available. Install it with: sudo gem install jeweler'
19
21
  end
20
22
 
21
23
  Rake::TestTask.new do |t|
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.1
1
+ 0.6.2
data/grip.gemspec CHANGED
@@ -5,22 +5,22 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{grip}
8
- s.version = "0.6.1"
8
+ s.version = "0.6.2"
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-31}
12
+ s.date = %q{2010-01-07}
13
13
  s.description = %q{GridFS attachments for MongoMapper}
14
14
  s.email = %q{signalstatic@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.markdown"
18
18
  ]
19
19
  s.files = [
20
20
  ".DS_Store",
21
21
  ".gitignore",
22
22
  "LICENSE",
23
- "README.rdoc",
23
+ "README.markdown",
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "grip.gemspec",
@@ -30,10 +30,10 @@ Gem::Specification.new do |s|
30
30
  "test/factories.rb",
31
31
  "test/fixtures/cthulhu.png",
32
32
  "test/fixtures/sample.pdf",
33
+ "test/grip_attachment_test.rb",
33
34
  "test/growler.rb",
35
+ "test/has_attachment_test.rb",
34
36
  "test/models.rb",
35
- "test/test_grip_attachment.rb",
36
- "test/test_has_attachment.rb",
37
37
  "test/test_helper.rb"
38
38
  ]
39
39
  s.homepage = %q{http://github.com/twoism/grip}
@@ -43,10 +43,10 @@ Gem::Specification.new do |s|
43
43
  s.summary = %q{GridFS attachments for MongoMapper}
44
44
  s.test_files = [
45
45
  "test/factories.rb",
46
+ "test/grip_attachment_test.rb",
46
47
  "test/growler.rb",
48
+ "test/has_attachment_test.rb",
47
49
  "test/models.rb",
48
- "test/test_grip_attachment.rb",
49
- "test/test_has_attachment.rb",
50
50
  "test/test_helper.rb"
51
51
  ]
52
52
 
@@ -55,9 +55,15 @@ Gem::Specification.new do |s|
55
55
  s.specification_version = 3
56
56
 
57
57
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
58
+ s.add_runtime_dependency(%q<rmagick>, [">= 0"])
59
+ s.add_runtime_dependency(%q<miso>, [">= 0"])
58
60
  else
61
+ s.add_dependency(%q<rmagick>, [">= 0"])
62
+ s.add_dependency(%q<miso>, [">= 0"])
59
63
  end
60
64
  else
65
+ s.add_dependency(%q<rmagick>, [">= 0"])
66
+ s.add_dependency(%q<miso>, [">= 0"])
61
67
  end
62
68
  end
63
69
 
data/lib/grip.rb CHANGED
@@ -1,4 +1,4 @@
1
- %w{mongo_mapper mongo/gridfs mime/types ftools tempfile rmagick miso}.each { |lib| require lib }
1
+ %w{mongo_mapper mongo/gridfs mime/types ftools tempfile RMagick miso}.each { |lib| require lib }
2
2
 
3
3
  module MongoMapper
4
4
  module Grip
@@ -3,8 +3,13 @@ module MongoMapper
3
3
  class Attachment
4
4
  include MongoMapper::Document
5
5
 
6
- belongs_to :owner, :polymorphic => true
7
- many :attached_variants, :as => :owner, :class_name => "MongoMapper::Grip::Attachment", :dependent => :destroy
6
+ belongs_to :owner,
7
+ :polymorphic => true
8
+
9
+ many :attached_variants,
10
+ :as => :owner,
11
+ :class_name => "MongoMapper::Grip::Attachment",
12
+ :dependent => :destroy
8
13
 
9
14
  key :owner_id, ObjectId, :required => true
10
15
  key :owner_type, String, :required => true
@@ -15,8 +20,8 @@ module MongoMapper
15
20
  key :content_type, String
16
21
  key :variants, Hash
17
22
 
18
- after_save :build_variants
19
- before_destroy :destroy_file
23
+ after_save :build_variants
24
+ before_destroy :destroy_file
20
25
 
21
26
  def file=new_file
22
27
  raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
@@ -25,7 +30,7 @@ module MongoMapper
25
30
  self.file_size = File.size(new_file.path)
26
31
  self.content_type = MIME::Types.type_for(new_file.path)
27
32
 
28
- write_to_grid new_file
33
+ write_to_grid self,new_file
29
34
  end
30
35
 
31
36
  def file
@@ -36,32 +41,32 @@ module MongoMapper
36
41
  "#{owner_type.pluralize}/#{owner_id}/#{name}".downcase
37
42
  end
38
43
 
44
+ def self.create_method method_name, &block
45
+ define_method(method_name) do |*args|
46
+ yield *args
47
+ end
48
+ end
49
+
39
50
  private
40
51
  def build_variants
41
52
  self.variants.each do |variant, dimensions|
42
53
 
43
- eval <<-EOF
44
- def #{variant}
45
- Attachment.find_or_initialize_by_name_and_owner_id("#{variant.to_s}",self._id)
46
- end
47
- EOF
54
+ self.class.create_method variant.to_sym do
55
+ attached_variants.find_or_create_by_name(:name=>"#{variant.to_s}")
56
+ end
48
57
 
49
- eval <<-EOF
50
- def #{variant}=(file_hash)
51
- new_attachment = Attachment.find_or_initialize_by_name_and_owner_id("#{variant.to_s}",self._id)
52
- new_attachment.owner_type = self.class.to_s
53
- new_attachment.file_name = File.basename(file_hash[:uploaded_file].path)
54
- new_attachment.file_size = File.size(file_hash[:resized_file].path)
55
- new_attachment.content_type = MIME::Types.type_for(file_hash[:uploaded_file].path)
56
- new_attachment.save!
58
+ self.class.create_method "#{variant}=".to_sym do |file_hash|
59
+
60
+ new_attachment = Attachment.find_or_initialize_by_name_and_owner_id("#{variant.to_s}",self._id)
61
+ new_attachment.owner_type = self.class.to_s
62
+ new_attachment.file_name = File.basename(file_hash[:uploaded_file].path)
63
+ new_attachment.file_size = File.size(file_hash[:resized_file].path)
64
+ new_attachment.content_type = MIME::Types.type_for(file_hash[:uploaded_file].path)
65
+ new_attachment.save!
57
66
 
58
- GridFS::GridStore.open(self.class.database, new_attachment.grid_key, 'w', :content_type => new_attachment.content_type) do |f|
59
- f.write file_hash[:resized_file].read
60
- end
61
-
62
- end
63
- EOF
64
-
67
+ write_to_grid new_attachment, file_hash[:resized_file]
68
+ end
69
+
65
70
  end
66
71
  end
67
72
 
@@ -69,8 +74,8 @@ module MongoMapper
69
74
  GridFS::GridStore.unlink(self.class.database, grid_key)
70
75
  end
71
76
 
72
- def write_to_grid new_file
73
- GridFS::GridStore.open(self.class.database, grid_key, 'w', :content_type => content_type) do |f|
77
+ def write_to_grid attachment, new_file
78
+ GridFS::GridStore.open(self.class.database, attachment.grid_key, 'w', :content_type => attachment.content_type) do |f|
74
79
  f.write new_file.read
75
80
  end
76
81
  end
@@ -13,11 +13,11 @@ module MongoMapper
13
13
 
14
14
  define_method("#{name}=") do |new_file|
15
15
  raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
16
-
16
+
17
+ self.class.uploaded_files[name][:file] = new_file
17
18
  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
+ new_attachment = attachments.find_or_create_by_name(name.to_s)
19
20
  update_attachment_attributes!(new_attachment, new_file, opts)
20
- self.class.uploaded_files[name][:file] = new_file
21
21
  end
22
22
 
23
23
  end
@@ -31,8 +31,10 @@ module MongoMapper
31
31
  base.extend ClassMethods
32
32
  base.class_eval do
33
33
  after_save :save_attachments
34
-
35
- many :attachments, :as => :owner, :class_name => "MongoMapper::Grip::Attachment", :dependent => :destroy
34
+ many :attachments,
35
+ :as => :owner,
36
+ :class_name => "MongoMapper::Grip::Attachment",
37
+ :dependent => :destroy
36
38
  end
37
39
  end
38
40
 
@@ -56,16 +58,20 @@ module MongoMapper
56
58
 
57
59
  def create_variant attachment, variant, dimensions
58
60
  tmp_file = self.class.uploaded_files[attachment.name.to_sym][:file]
59
- tmp = Tempfile.new("#{attachment.name}_#{variant}")
60
- image = Miso::Image.new(tmp_file.path)
61
+ begin
62
+ tmp = Tempfile.new("#{attachment.name}_#{variant}")
63
+ image = Miso::Image.new(tmp_file.path)
61
64
 
62
- image.crop(dimensions[:width], dimensions[:height]) if dimensions[:crop]
63
- image.fit(dimensions[:width], dimensions[:height]) unless dimensions[:crop]
65
+ image.crop(dimensions[:width], dimensions[:height]) if dimensions[:crop]
66
+ image.fit(dimensions[:width], dimensions[:height]) unless dimensions[:crop]
64
67
 
65
- image.write(tmp.path)
68
+ image.write(tmp.path)
69
+ rescue RuntimeError => e
70
+ warn "Image was not resized. #{e}"
71
+ tmp = tmp_file
72
+ end
66
73
 
67
74
  file_hash = {:resized_file => tmp,:uploaded_file => tmp_file}
68
-
69
75
  attachment.send("#{variant}=", file_hash)
70
76
  end
71
77
 
data/test/test_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'rubygems'
1
2
  %w{test/unit shoulda factory_girl mongo_mapper factories}.each { |lib| require lib }
2
3
  require 'growler'
3
4
  require File.expand_path(File.dirname(__FILE__) + '/../lib/grip')
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.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - twoism
@@ -10,10 +10,29 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-12-31 00:00:00 -05:00
13
+ date: 2010-01-07 00:00:00 -05:00
14
14
  default_executable:
15
- dependencies: []
16
-
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rmagick
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: miso
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ version:
17
36
  description: GridFS attachments for MongoMapper
18
37
  email: signalstatic@gmail.com
19
38
  executables: []
@@ -22,12 +41,12 @@ extensions: []
22
41
 
23
42
  extra_rdoc_files:
24
43
  - LICENSE
25
- - README.rdoc
44
+ - README.markdown
26
45
  files:
27
46
  - .DS_Store
28
47
  - .gitignore
29
48
  - LICENSE
30
- - README.rdoc
49
+ - README.markdown
31
50
  - Rakefile
32
51
  - VERSION
33
52
  - grip.gemspec
@@ -37,10 +56,10 @@ files:
37
56
  - test/factories.rb
38
57
  - test/fixtures/cthulhu.png
39
58
  - test/fixtures/sample.pdf
59
+ - test/grip_attachment_test.rb
40
60
  - test/growler.rb
61
+ - test/has_attachment_test.rb
41
62
  - test/models.rb
42
- - test/test_grip_attachment.rb
43
- - test/test_has_attachment.rb
44
63
  - test/test_helper.rb
45
64
  has_rdoc: true
46
65
  homepage: http://github.com/twoism/grip
@@ -72,8 +91,8 @@ specification_version: 3
72
91
  summary: GridFS attachments for MongoMapper
73
92
  test_files:
74
93
  - test/factories.rb
94
+ - test/grip_attachment_test.rb
75
95
  - test/growler.rb
96
+ - test/has_attachment_test.rb
76
97
  - test/models.rb
77
- - test/test_grip_attachment.rb
78
- - test/test_has_attachment.rb
79
98
  - test/test_helper.rb
data/README.rdoc DELETED
@@ -1,47 +0,0 @@
1
- = grip
2
-
3
- GridFS attachments for MongoMapper
4
-
5
- = installation
6
-
7
- The grip gem is hosted on gemcutter.org:
8
-
9
- gem install miso grip
10
-
11
-
12
- = Usage (See tests for better docs)
13
-
14
- class Doc
15
- include MongoMapper::Document
16
- include MongoMapper::Grip::HasAttachment
17
-
18
- has_grid_attachment :image, :versions => {:thumb => {:width=>50,:height=>50}}
19
-
20
- end
21
-
22
- image = File.open('foo.png', 'r')
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
-