Skip to content
Newer
Older
100644 95 lines (66 sloc) 3.7 KB
1e62be4 @meskyanichi Fixed ORM => ODM typo's.
meskyanichi authored
1 Mongoid::Paperclip - Making Paperclip play nice with Mongoid ODM
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
2 ================================================================
3
075e647 @meskyanichi Updated the README.
meskyanichi authored
4 As the title suggests: `Mongoid::Paperclip` makes it easy to hook up [Paperclip](https://github.com/thoughtbot/paperclip) with [Mongoid](http://mongoid.org/).
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
5
3ae36d5 @meskyanichi Fixed some README typos.
meskyanichi authored
6 This is actually easier and faster to set up than when using Paperclip and the ActiveRecord ORM.
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
7 This example assumes you are using **Ruby on Rails 3** and **Bundler**. However it doesn't require either.
8
e18b43d @kristianmandrup updated deps to use paperclip and mongoid 3.0
authored
9 Paperclip 3.0
10 -------------
11 Paperclip 3.0 introduces a non-backward compatible change in your attachment
12 path. This will help to prevent attachment name clashes when you have
13 multiple attachments with the same name. If you didn't alter your
14 attachment's path and are using Paperclip's default, you'll have to add
15 `:path` and `:url` to your `has_attached_file` definition. For example:
16
17 has_attached_file :avatar,
18 :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
19 :url => "/system/:attachment/:id/:style/:filename"
20
21 Rails 3 Dummy app
22 -----------------
23 This project should contain a Rails 3 dummy app or siilar in order to test the functionality.
24
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
25
075e647 @meskyanichi Updated the README.
meskyanichi authored
26 Setting it up
27 -------------
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
28
075e647 @meskyanichi Updated the README.
meskyanichi authored
29 Simply define the `mongoid-paperclip` gem inside your `Gemfile`. Additionally, you can define the `aws-s3` gem if you want to upload your files to Amazon S3. *You do not need to explicitly define the `paperclip` gem itself, since this is handled by `mongoid-paperclip`.*
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
30
075e647 @meskyanichi Updated the README.
meskyanichi authored
31 **Rails.root/Gemfile - Just define the following:**
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
32
33 gem "mongoid-paperclip", :require => "mongoid_paperclip"
075e647 @meskyanichi Updated the README.
meskyanichi authored
34 gem "aws-s3", :require => "aws/s3"
0ecbcc8 @meskyanichi Fixes issue #1 regarding the broken functionality of adding multiple …
meskyanichi authored
35
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
36 Next let's assume we have a User model and we want to allow our users to upload an avatar.
37
3ae36d5 @meskyanichi Fixed some README typos.
meskyanichi authored
38 **Rails.root/app/models/user.rb - include the Mongoid::Paperclip module and invoke the provided class method**
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
39
40 class User
41 include Mongoid::Document
42 include Mongoid::Paperclip
0ecbcc8 @meskyanichi Fixes issue #1 regarding the broken functionality of adding multiple …
meskyanichi authored
43
44 has_mongoid_attached_file :avatar
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
45 end
46
47
075e647 @meskyanichi Updated the README.
meskyanichi authored
48 That's it
49 --------
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
50
0ecbcc8 @meskyanichi Fixes issue #1 regarding the broken functionality of adding multiple …
meskyanichi authored
51 That's all you have to do. Users can now upload avatars. Unlike ActiveRecord, Mongoid doesn't use migrations, so we don't need to define the Paperclip columns in a separate file. Invoking the `has_mongoid_attached_file` method will automatically define the necessary `:avatar` fields for you in the background.
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
52
53
54 A more complex example
55 ----------------------
56
0ecbcc8 @meskyanichi Fixes issue #1 regarding the broken functionality of adding multiple …
meskyanichi authored
57 Just like Paperclip, Mongoid::Paperclip takes a second argument (hash of options) for the `has_mongoid_attached_file` method, so you can do more complex things such as in the following example.
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
58
59 class User
60 include Mongoid::Document
61 embeds_many :pictures
62 end
0ecbcc8 @meskyanichi Fixes issue #1 regarding the broken functionality of adding multiple …
meskyanichi authored
63
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
64 class Picture
65 include Mongoid::Document
66 include Mongoid::Paperclip
0ecbcc8 @meskyanichi Fixes issue #1 regarding the broken functionality of adding multiple …
meskyanichi authored
67
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
68 embedded_in :user, :inverse_of => :pictures
0ecbcc8 @meskyanichi Fixes issue #1 regarding the broken functionality of adding multiple …
meskyanichi authored
69
70 has_mongoid_attached_file :attachment,
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
71 :path => ':attachment/:id/:style.:extension',
72 :storage => :s3,
73 :url => ':s3_alias_url',
74 :s3_host_alias => 'something.cloudfront.net',
75 :s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
76 :styles => {
77 :original => ['1920x1680>', :jpg],
78 :small => ['100x100#', :jpg],
79 :medium => ['250x250', :jpg],
80 :large => ['500x500>', :jpg]
81 },
82 :convert_options => { :all => '-background white -flatten +matte' }
83 end
0ecbcc8 @meskyanichi Fixes issue #1 regarding the broken functionality of adding multiple …
meskyanichi authored
84
075e647 @meskyanichi Updated the README.
meskyanichi authored
85 @user.pictures.each do |picture|
86 <%= picture.attachment.url %>
87 end
88
89
90 There you go
91 ------------
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
92
075e647 @meskyanichi Updated the README.
meskyanichi authored
93 Quite a lot of people have been looking for a solution to use Paperclip with Mongoid so I hope this helps!
d84fca3 @meskyanichi Releasing version 0.0.1!
meskyanichi authored
94
075e647 @meskyanichi Updated the README.
meskyanichi authored
95 If you need more information on either [Mongoid](http://mongoid.org/) or [Paperclip](https://github.com/thoughtbot/paperclip) I suggest checking our their official documentation and website.
Something went wrong with that request. Please try again.