mm-attach-it 0.1.2 → 0.1.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/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 if you to store it on file system or GridFS.
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 for GridFS you should do:
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 system or GridFS)
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 attaches on both: file system or GridFS)
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: photo, :content_type => ['image/jpeg', 'image/gif', 'image/png']
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, you can attach whatever kind of file!
57
+ OBS: But remember! You can attach any desirable file.
54
58
 
55
- If you are using the file system to store the files you can specify the storage directory
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 '/:rails_root/public/system/:attachment/:id/:style/:filename'
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 ('foo' the example above)
70
- * :attachment - is the name of the column's collection ('photo' the example above)
73
+ * :class - the name of the class (in the example given above, ‘foo’)
74
+ * :attachment - is the name of the columns 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
- :style => { :thumb => '100x100>' },
90
+ :styles => { :thumb => '100x100>' },
87
91
  :url => '/assets/groups/:id/:style/:filename',
88
- :path => '/:rails_root/public/image/foos/:id/:style/:filename', :storage => 'gridfs',
92
+ :path => '/:rails_root/public/image/foos/:id/:style/:filename'
89
93
  }
90
94
 
91
95
  # on GridFS
92
96
  has_attachment :avatar, {
93
- :style => { :thumb => '100x100>' },
97
+ :styles => { :thumb => '100x100>' },
94
98
  :storage => 'gridfs',
95
99
  }
96
100
  end
97
101
 
98
- On form you must set the "multipart" option and the field as "file_field":
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
- The safest way to show the images is using Base64, doesn't matter if you are using the file system or GridFS.
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, only for file system, if you specify the 'url' option you can do:
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 your files and youn don't want to use Base64 data to show the images you've got to create a action on a controller.
137
+ If you are using the GridFS to store files, and you dont want to use Base64 data to present the images, youve got to create an action on a controller.
134
138
 
135
- But first create a new route on the route's file:
139
+ But first create a new route on the routes 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 if you to store it on file system or GridFS.
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 for GridFS you should do:
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 system or GridFS)
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 attaches on both: file system or GridFS)
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: photo, :content_type => ['image/jpeg', 'image/gif', 'image/png']
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, you can attach whatever kind of file!
57
+ OBS: But remember! You can attach any desirable file.
54
58
 
55
- If you are using the file system to store the files you can specify the storage directory
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 '/:rails_root/public/system/:attachment/:id/:style/:filename'
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 ('foo' the example above)
70
- * :attachment - is the name of the column's collection ('photo' the example above)
73
+ * :class - the name of the class (in the example given above, ‘foo’)
74
+ * :attachment - is the name of the columns 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
- :style => { :thumb => '100x100>' },
90
+ :styles => { :thumb => '100x100>' },
87
91
  :url => '/assets/groups/:id/:style/:filename',
88
- :path => '/:rails_root/public/image/foos/:id/:style/:filename', :storage => 'gridfs',
92
+ :path => '/:rails_root/public/image/foos/:id/:style/:filename'
89
93
  }
90
94
 
91
95
  # on GridFS
92
96
  has_attachment :avatar, {
93
- :style => { :thumb => '100x100>' },
97
+ :styles => { :thumb => '100x100>' },
94
98
  :storage => 'gridfs',
95
99
  }
96
100
  end
97
101
 
98
- On form you must set the "multipart" option and the field as "file_field":
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
- The safest way to show the images is using Base64, doesn't matter if you are using the file system or GridFS.
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, only for file system, if you specify the 'url' option you can do:
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 your files and youn don't want to use Base64 data to show the images you've got to create a action on a controller.
137
+ If you are using the GridFS to store files, and you dont want to use Base64 data to present the images, youve got to create an action on a controller.
134
138
 
135
- But first create a new route on the route's file:
139
+ But first create a new route on the routes 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
 
@@ -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.keys.each do |name|
78
- @attachment_options[name].delete
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
- resize(style_value, image_options.assigned_file.path).write(image_options.path(style_name))
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(resize(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}")
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
@@ -1,3 +1,3 @@
1
1
  module AttachIt
2
- Version = '0.1.2'
2
+ Version = '0.1.3'
3
3
  end
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/mm_attach_it')
6
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/mm-attach-it')
7
7
 
8
8
  MongoMapper.database = "testing_mm_attach_it"
9
9
 
@@ -60,9 +60,7 @@ class TestAttachIt < Test::Unit::TestCase
60
60
  end
61
61
 
62
62
 
63
- context "Attaching not an image on file system" do
64
-
65
- end
63
+ context "Attaching not an image on file system" do
66
64
  setup do
67
65
  @doc = DocumentOne.new
68
66
  @doc.name = 'Mydocument'
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: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
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-05-30 00:00:00 Z
18
+ date: 2011-06-26 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: wand