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 CHANGED
@@ -1 +1 @@
1
- 0.6.2
1
+ 0.6.3
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{grip}
8
- s.version = "0.6.2"
8
+ s.version = "0.6.3"
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"]
@@ -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 MongoMapper
4
- module Grip
5
- class GripError < StandardError; end
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 }
@@ -1,88 +1,86 @@
1
- module MongoMapper
2
- module Grip
3
- class Attachment
4
- include MongoMapper::Document
1
+ module Grip
2
+ class Attachment
3
+ include MongoMapper::Document
5
4
 
6
- belongs_to :owner,
7
- :polymorphic => true
8
-
9
- many :attached_variants,
10
- :as => :owner,
11
- :class_name => "MongoMapper::Grip::Attachment",
12
- :dependent => :destroy
13
-
14
- key :owner_id, ObjectId, :required => true
15
- key :owner_type, String, :required => true
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
- key :name, String
18
- key :file_name, String
19
- key :file_size, Integer
20
- key :content_type, String
21
- key :variants, Hash
22
-
23
- after_save :build_variants
24
- before_destroy :destroy_file
25
-
26
- def file=new_file
27
- raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
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
- def grid_key
41
- "#{owner_type.pluralize}/#{owner_id}/#{name}".downcase
42
- end
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
- def self.create_method method_name, &block
45
- define_method(method_name) do |*args|
46
- yield *args
47
- end
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
- private
51
- def build_variants
52
- self.variants.each do |variant, dimensions|
53
-
54
- self.class.create_method variant.to_sym do
55
- attached_variants.find_or_create_by_name(:name=>"#{variant.to_s}")
56
- end
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
- 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!
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
- write_to_grid new_attachment, file_hash[:resized_file]
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
- 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|
79
- f.write new_file.read
80
- end
81
- end
82
-
83
- def read_from_grid key
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
- end
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
@@ -1,80 +1,78 @@
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.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
- def uploaded_files
26
- read_inheritable_attribute(:uploaded_files)
9
+ define_method(name) do
10
+ attachments.find(:first,:conditions=>{:name=>name.to_s})
27
11
  end
28
-
29
- end
30
- def self.included(base)
31
- base.extend ClassMethods
32
- base.class_eval do
33
- after_save :save_attachments
34
- many :attachments,
35
- :as => :owner,
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 update_attachment_attributes! new_attachment, new_file, opts
42
- new_attachment.owner_type = self.class.to_s
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
- def save_attachments
52
- attachments.each do |attachment|
53
- attachment.variants.each do |variant,dimensions|
54
- create_variant(attachment,variant,dimensions)
55
- end
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
- def create_variant attachment, variant, dimensions
60
- tmp_file = self.class.uploaded_files[attachment.name.to_sym][:file]
61
- begin
62
- tmp = Tempfile.new("#{attachment.name}_#{variant}")
63
- image = Miso::Image.new(tmp_file.path)
64
-
65
- image.crop(dimensions[:width], dimensions[:height]) if dimensions[:crop]
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
@@ -1,6 +1,6 @@
1
1
  require "test_helper"
2
2
 
3
- include MongoMapper::Grip
3
+ include Grip
4
4
 
5
5
  class GripAttachmentTest < Test::Unit::TestCase
6
6
  context "An Attachment" do
@@ -1,6 +1,6 @@
1
1
  require "test_helper"
2
2
  require "models"
3
- include MongoMapper::Grip
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(MongoMapper::Grip::Attachment, @document.image.class)
36
+ assert_equal(Grip::Attachment, @document.image.class)
37
37
  end
38
38
 
39
39
  should "read file from grid store" do
@@ -1,5 +1,5 @@
1
1
  class Doc
2
2
  include MongoMapper::Document
3
- include MongoMapper::Grip::HasAttachment
3
+ include Grip::HasAttachment
4
4
  has_grid_attachment :image, :variants => {:thumb=>{:width=>50,:height=>50},:super_thumb=>{:width=>10,:height=>10}}
5
5
  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.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - twoism