mm-attach-it 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +23 -19
- data/README.rdoc +23 -19
- data/lib/attach_it/attach_it.rb +6 -4
- data/lib/attach_it/storage/filesystem.rb +1 -1
- data/lib/attach_it/storage/gridfs.rb +1 -1
- data/lib/attach_it/storage/storage.rb +20 -0
- data/lib/attach_it/version.rb +1 -1
- data/test/test_helper.rb +1 -1
- data/test/unit/test_attach_it.rb +1 -3
- metadata +4 -4
data/README
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
= Mm-attach-it
|
2
2
|
|
3
|
-
Attach files (images, videos, pdfs, txts, zips and etc) to a MongoMapper record. You can choose
|
3
|
+
Attach files (images, videos, pdfs, txts, zips and etc) to a MongoMapper record. You can even choose to store them on file system or GridFS.
|
4
4
|
|
5
5
|
== Install
|
6
6
|
|
7
7
|
sudo gem install mm-attach-it
|
8
8
|
|
9
|
+
Or add it to your Rails’s Gemfile::
|
10
|
+
|
11
|
+
gem "mm-attach-it"
|
12
|
+
|
9
13
|
== Usage
|
10
14
|
|
11
15
|
=== Model
|
@@ -19,7 +23,7 @@ Declare the plugin and use the attachment method to make attachments.
|
|
19
23
|
has_attachment :photo
|
20
24
|
end
|
21
25
|
|
22
|
-
The default storage is the file system, if you want to change
|
26
|
+
The default storage destination is the file system, if you want to change it to GridFS you should add the following:
|
23
27
|
|
24
28
|
class Bar
|
25
29
|
include MongoMapper::Document
|
@@ -28,7 +32,7 @@ The default storage is the file system, if you want to change for GridFS you sho
|
|
28
32
|
has_attachment :photo, { :storage => 'gridfs' }
|
29
33
|
end
|
30
34
|
|
31
|
-
If you want to resize the images (you can store resized images on both: file
|
35
|
+
If you want to resize the images (you can store resized images on both: file systems or GridFS):
|
32
36
|
|
33
37
|
class Foo
|
34
38
|
include MongoMapper::Document
|
@@ -37,7 +41,7 @@ If you want to resize the images (you can store resized images on both: file sys
|
|
37
41
|
has_attachment :photo, { :styles => { :small => '100x100>', :medium => '200x200>' } }
|
38
42
|
end
|
39
43
|
|
40
|
-
If you want to validate the attached file (again you can validate
|
44
|
+
If you want to validate the attached file (again, you can validate attachments on both: file system or GridFS):
|
41
45
|
|
42
46
|
class Foo
|
43
47
|
include MongoMapper::Document
|
@@ -46,13 +50,13 @@ If you want to validate the attached file (again you can validate attaches on bo
|
|
46
50
|
has_attch :photo
|
47
51
|
|
48
52
|
validates_attachment_presence :photo
|
49
|
-
validates_attachment_content_type:
|
53
|
+
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/gif', 'image/png']
|
50
54
|
validates_attachment_size :photo, { :less_than => 1.megabyte, :greater_than => 500.kilobytes }
|
51
55
|
end
|
52
56
|
|
53
|
-
OBS: But remember
|
57
|
+
OBS: But remember! You can attach any desirable file.
|
54
58
|
|
55
|
-
If you are using the file system to store
|
59
|
+
If you are using the file system to store files, you can specify the directory/folder to store them.
|
56
60
|
|
57
61
|
class Foo
|
58
62
|
include MongoMapper::Document
|
@@ -61,13 +65,13 @@ If you are using the file system to store the files you can specify the storage
|
|
61
65
|
has_attachment :photo, { :path => '/:rails_root/public/images/foos/:id/:style/:filename' }
|
62
66
|
end
|
63
67
|
|
64
|
-
OBS: The default directory is
|
68
|
+
OBS: The default directory is ’/:rails_root/public/system/:attachment/:id/:style/:filename’
|
65
69
|
|
66
70
|
Where:
|
67
71
|
* :rails_root - is the root directory of your Rails application
|
68
72
|
* :environment - can be "production", "test" or "development"
|
69
|
-
* :class - the name of the class (
|
70
|
-
* :attachment - is the name of the column
|
73
|
+
* :class - the name of the class (in the example given above, ‘foo’)
|
74
|
+
* :attachment - is the name of the column’s collection (in the example given above, ‘photo’)
|
71
75
|
* :id - the "id" of the record on MongoDB
|
72
76
|
* :style - if you specified the styles
|
73
77
|
* :filename - the name of the file
|
@@ -83,19 +87,19 @@ Check the model below
|
|
83
87
|
|
84
88
|
# on file system
|
85
89
|
has_attachment :photo, {
|
86
|
-
:
|
90
|
+
:styles => { :thumb => '100x100>' },
|
87
91
|
:url => '/assets/groups/:id/:style/:filename',
|
88
|
-
:path => '/:rails_root/public/image/foos/:id/:style/:filename'
|
92
|
+
:path => '/:rails_root/public/image/foos/:id/:style/:filename'
|
89
93
|
}
|
90
94
|
|
91
95
|
# on GridFS
|
92
96
|
has_attachment :avatar, {
|
93
|
-
:
|
97
|
+
:styles => { :thumb => '100x100>' },
|
94
98
|
:storage => 'gridfs',
|
95
99
|
}
|
96
100
|
end
|
97
101
|
|
98
|
-
|
102
|
+
Within forms you must set the "multipart" option and the field as "file_field":
|
99
103
|
|
100
104
|
<%= form_for(@foo, :html => { :multipart => true }) do |f| %>
|
101
105
|
|
@@ -115,7 +119,7 @@ On form you must set the "multipart" option and the field as "file_field":
|
|
115
119
|
<% end %>
|
116
120
|
|
117
121
|
|
118
|
-
|
122
|
+
Regardless of using the file system or GridFS the safest way to present the images is using Base64.
|
119
123
|
|
120
124
|
<img src="<%= foo.photo.base64 %>">
|
121
125
|
<img src="<%= foo.photo.base64('thumb') %>">
|
@@ -123,16 +127,16 @@ The safest way to show the images is using Base64, doesn't matter if you are usi
|
|
123
127
|
<img src="<%= foo.avatar.base64 %>">
|
124
128
|
<img src="<%= foo.avatar.base64('thumb') %>">
|
125
129
|
|
126
|
-
Also,
|
130
|
+
Also, specifically when using the file system, if you specify the ‘url’ option, you can do:
|
127
131
|
|
128
132
|
<%= image_tag foo.photo.url %>
|
129
133
|
<%= image_tag foo.photo.url('thumb') %>
|
130
134
|
|
131
135
|
=== Controller
|
132
136
|
|
133
|
-
If you are using the GridFS to store
|
137
|
+
If you are using the GridFS to store files, and you don’t want to use Base64 data to present the images, you’ve got to create an action on a controller.
|
134
138
|
|
135
|
-
But first create a new route on the route
|
139
|
+
But first create a new route on the route’s file:
|
136
140
|
|
137
141
|
match '/foos/avatar/:id(/:style)', :to => 'foos#avatar'
|
138
142
|
|
@@ -153,7 +157,7 @@ Now the controller:
|
|
153
157
|
|
154
158
|
end
|
155
159
|
|
156
|
-
And the view do that:
|
160
|
+
And, for the view, do that:
|
157
161
|
|
158
162
|
<%= image_tag "/foos/avatar/#{foo.id.to_s}/small" %>
|
159
163
|
|
data/README.rdoc
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
= Mm-attach-it
|
2
2
|
|
3
|
-
Attach files (images, videos, pdfs, txts, zips and etc) to a MongoMapper record. You can choose
|
3
|
+
Attach files (images, videos, pdfs, txts, zips and etc) to a MongoMapper record. You can even choose to store them on file system or GridFS.
|
4
4
|
|
5
5
|
== Install
|
6
6
|
|
7
7
|
sudo gem install mm-attach-it
|
8
8
|
|
9
|
+
Or add it to your Rails’s Gemfile::
|
10
|
+
|
11
|
+
gem "mm-attach-it"
|
12
|
+
|
9
13
|
== Usage
|
10
14
|
|
11
15
|
=== Model
|
@@ -19,7 +23,7 @@ Declare the plugin and use the attachment method to make attachments.
|
|
19
23
|
has_attachment :photo
|
20
24
|
end
|
21
25
|
|
22
|
-
The default storage is the file system, if you want to change
|
26
|
+
The default storage destination is the file system, if you want to change it to GridFS you should add the following:
|
23
27
|
|
24
28
|
class Bar
|
25
29
|
include MongoMapper::Document
|
@@ -28,7 +32,7 @@ The default storage is the file system, if you want to change for GridFS you sho
|
|
28
32
|
has_attachment :photo, { :storage => 'gridfs' }
|
29
33
|
end
|
30
34
|
|
31
|
-
If you want to resize the images (you can store resized images on both: file
|
35
|
+
If you want to resize the images (you can store resized images on both: file systems or GridFS):
|
32
36
|
|
33
37
|
class Foo
|
34
38
|
include MongoMapper::Document
|
@@ -37,7 +41,7 @@ If you want to resize the images (you can store resized images on both: file sys
|
|
37
41
|
has_attachment :photo, { :styles => { :small => '100x100>', :medium => '200x200>' } }
|
38
42
|
end
|
39
43
|
|
40
|
-
If you want to validate the attached file (again you can validate
|
44
|
+
If you want to validate the attached file (again, you can validate attachments on both: file system or GridFS):
|
41
45
|
|
42
46
|
class Foo
|
43
47
|
include MongoMapper::Document
|
@@ -46,13 +50,13 @@ If you want to validate the attached file (again you can validate attaches on bo
|
|
46
50
|
has_attch :photo
|
47
51
|
|
48
52
|
validates_attachment_presence :photo
|
49
|
-
validates_attachment_content_type:
|
53
|
+
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/gif', 'image/png']
|
50
54
|
validates_attachment_size :photo, { :less_than => 1.megabyte, :greater_than => 500.kilobytes }
|
51
55
|
end
|
52
56
|
|
53
|
-
OBS: But remember
|
57
|
+
OBS: But remember! You can attach any desirable file.
|
54
58
|
|
55
|
-
If you are using the file system to store
|
59
|
+
If you are using the file system to store files, you can specify the directory/folder to store them.
|
56
60
|
|
57
61
|
class Foo
|
58
62
|
include MongoMapper::Document
|
@@ -61,13 +65,13 @@ If you are using the file system to store the files you can specify the storage
|
|
61
65
|
has_attachment :photo, { :path => '/:rails_root/public/images/foos/:id/:style/:filename' }
|
62
66
|
end
|
63
67
|
|
64
|
-
OBS: The default directory is
|
68
|
+
OBS: The default directory is ’/:rails_root/public/system/:attachment/:id/:style/:filename’
|
65
69
|
|
66
70
|
Where:
|
67
71
|
* :rails_root - is the root directory of your Rails application
|
68
72
|
* :environment - can be "production", "test" or "development"
|
69
|
-
* :class - the name of the class (
|
70
|
-
* :attachment - is the name of the column
|
73
|
+
* :class - the name of the class (in the example given above, ‘foo’)
|
74
|
+
* :attachment - is the name of the column’s collection (in the example given above, ‘photo’)
|
71
75
|
* :id - the "id" of the record on MongoDB
|
72
76
|
* :style - if you specified the styles
|
73
77
|
* :filename - the name of the file
|
@@ -83,19 +87,19 @@ Check the model below
|
|
83
87
|
|
84
88
|
# on file system
|
85
89
|
has_attachment :photo, {
|
86
|
-
:
|
90
|
+
:styles => { :thumb => '100x100>' },
|
87
91
|
:url => '/assets/groups/:id/:style/:filename',
|
88
|
-
:path => '/:rails_root/public/image/foos/:id/:style/:filename'
|
92
|
+
:path => '/:rails_root/public/image/foos/:id/:style/:filename'
|
89
93
|
}
|
90
94
|
|
91
95
|
# on GridFS
|
92
96
|
has_attachment :avatar, {
|
93
|
-
:
|
97
|
+
:styles => { :thumb => '100x100>' },
|
94
98
|
:storage => 'gridfs',
|
95
99
|
}
|
96
100
|
end
|
97
101
|
|
98
|
-
|
102
|
+
Within forms you must set the "multipart" option and the field as "file_field":
|
99
103
|
|
100
104
|
<%= form_for(@foo, :html => { :multipart => true }) do |f| %>
|
101
105
|
|
@@ -115,7 +119,7 @@ On form you must set the "multipart" option and the field as "file_field":
|
|
115
119
|
<% end %>
|
116
120
|
|
117
121
|
|
118
|
-
|
122
|
+
Regardless of using the file system or GridFS the safest way to present the images is using Base64.
|
119
123
|
|
120
124
|
<img src="<%= foo.photo.base64 %>">
|
121
125
|
<img src="<%= foo.photo.base64('thumb') %>">
|
@@ -123,16 +127,16 @@ The safest way to show the images is using Base64, doesn't matter if you are usi
|
|
123
127
|
<img src="<%= foo.avatar.base64 %>">
|
124
128
|
<img src="<%= foo.avatar.base64('thumb') %>">
|
125
129
|
|
126
|
-
Also,
|
130
|
+
Also, specifically when using the file system, if you specify the ‘url’ option, you can do:
|
127
131
|
|
128
132
|
<%= image_tag foo.photo.url %>
|
129
133
|
<%= image_tag foo.photo.url('thumb') %>
|
130
134
|
|
131
135
|
=== Controller
|
132
136
|
|
133
|
-
If you are using the GridFS to store
|
137
|
+
If you are using the GridFS to store files, and you don’t want to use Base64 data to present the images, you’ve got to create an action on a controller.
|
134
138
|
|
135
|
-
But first create a new route on the route
|
139
|
+
But first create a new route on the route’s file:
|
136
140
|
|
137
141
|
match '/foos/avatar/:id(/:style)', :to => 'foos#avatar'
|
138
142
|
|
@@ -153,7 +157,7 @@ Now the controller:
|
|
153
157
|
|
154
158
|
end
|
155
159
|
|
156
|
-
And the view do that:
|
160
|
+
And, for the view, do that:
|
157
161
|
|
158
162
|
<%= image_tag "/foos/avatar/#{foo.id.to_s}/small" %>
|
159
163
|
|
data/lib/attach_it/attach_it.rb
CHANGED
@@ -7,7 +7,7 @@ module AttachIt
|
|
7
7
|
|
8
8
|
module ClassMethods
|
9
9
|
def has_attachment(name, options = {})
|
10
|
-
options.symbolize_keys
|
10
|
+
options = options.symbolize_keys
|
11
11
|
name = name.to_sym
|
12
12
|
|
13
13
|
after_save :save_attachments
|
@@ -25,7 +25,7 @@ module AttachIt
|
|
25
25
|
define_method("#{name}") do
|
26
26
|
information_for(name, options)
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
validates_each name, :logic => lambda { information_for(name, options).send(:flush_errors) }
|
30
30
|
end
|
31
31
|
|
@@ -74,8 +74,10 @@ module AttachIt
|
|
74
74
|
end
|
75
75
|
|
76
76
|
def destroy_attachments
|
77
|
-
@attachment_options.
|
78
|
-
@attachment_options
|
77
|
+
unless @attachment_options.nil?
|
78
|
+
@attachment_options.keys.each do |name|
|
79
|
+
@attachment_options[name].delete
|
80
|
+
end
|
79
81
|
end
|
80
82
|
end
|
81
83
|
|
@@ -4,7 +4,7 @@ class Filesystem < Storage
|
|
4
4
|
image_options.styles.each do |style_name, style_value|
|
5
5
|
begin
|
6
6
|
FileUtils.mkdir_p(File.dirname(image_options.path(style_name)))
|
7
|
-
|
7
|
+
transform(style_value, image_options.assigned_file.path).write(image_options.path(style_name))
|
8
8
|
FileUtils.chmod(0644, image_options.path(style_name))
|
9
9
|
rescue Exception => exception
|
10
10
|
image_options.add_error(exception.to_s)
|
@@ -7,7 +7,7 @@ class Gridfs < Storage
|
|
7
7
|
def flush_write(image_options = nil)
|
8
8
|
image_options.styles.each do |style_name, style_value|
|
9
9
|
begin
|
10
|
-
gridfs_id = @grid.put(
|
10
|
+
gridfs_id = @grid.put(transform(style_value, image_options.assigned_file.path).to_blob, :filename => style_name.to_s + '_' + image_options.file_name, :_id => "#{image_options.object_id}_#{image_options.name}_#{style_name}")
|
11
11
|
rescue Exception => exception
|
12
12
|
image_options.add_error(exception.to_s)
|
13
13
|
end
|
@@ -2,9 +2,29 @@ require 'RMagick'
|
|
2
2
|
|
3
3
|
class Storage
|
4
4
|
|
5
|
+
def transform(style_value = nil, filename = nil)
|
6
|
+
style_value.gsub!(/\s+/, '')
|
7
|
+
|
8
|
+
if style_value.match(/^(\d+)x(\d+)\#$/)
|
9
|
+
crop($1.to_i, $2.to_i, filename)
|
10
|
+
else
|
11
|
+
resize(style_value, filename)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
5
16
|
def resize(style_value = nil, filename = nil)
|
6
17
|
new_image = Magick::Image.read(filename).first
|
7
18
|
new_image.change_geometry!(style_value) { |cols, rows, img| img.resize!(cols, rows) }
|
19
|
+
new_image
|
20
|
+
end
|
21
|
+
|
22
|
+
def crop(new_width = nil, new_height = nil, filename = nil)
|
23
|
+
new_image = Magick::Image.read(filename).first
|
24
|
+
width = new_image.columns
|
25
|
+
height = new_image.rows
|
26
|
+
new_image.crop!(width/2 - new_width/2, height/2 - new_height/2, new_width, new_height)
|
27
|
+
new_image
|
8
28
|
end
|
9
29
|
|
10
30
|
end
|
data/lib/attach_it/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -3,7 +3,7 @@ require 'tempfile'
|
|
3
3
|
require 'mongo_mapper'
|
4
4
|
require 'shoulda'
|
5
5
|
require 'mocha'
|
6
|
-
require File.expand_path(File.dirname(__FILE__) + '/../lib/
|
6
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/mm-attach-it')
|
7
7
|
|
8
8
|
MongoMapper.database = "testing_mm_attach_it"
|
9
9
|
|
data/test/unit/test_attach_it.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mm-attach-it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adilson Chacon
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-26 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: wand
|