Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael van Rooijen committed Dec 11, 2010
2 parents d84fca3 + 2e0e6ec commit 74dce54
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
40 changes: 23 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
Mongoid::Paperclip - Making Paperclip play nice with Mongoid ORM
Mongoid::Paperclip - Making Paperclip play nice with Mongoid ODM
================================================================

As the title suggests, using this gem you will be able to use [Paperclip](https://github.com/thoughtbot/paperclip) with [Mongoid](http://mongoid.org/).

This is actually **easier** and **faster** to set up than when using the ActiveRecord ORM.
As the title suggests: `Mongoid::Paperclip` makes it easy to hook up [Paperclip](https://github.com/thoughtbot/paperclip) with [Mongoid](http://mongoid.org/).

This is actually easier and faster to set up than when using Paperclip the ActiveRecord ORM.
This example assumes you are using **Ruby on Rails 3** and **Bundler**. However it doesn't require either.


Setting it up in a few seconds
------------------------------
Setting it up
-------------

First require the **Paperclip** gem as you normally would, followed by the **Mongoid::Paperclip** extension. Additionally if you are working with Amazon S3 you will want to add the AWS::S3 gem to your Gemfile as well.
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`.*

**Rails.root/Gemfile**
**Rails.root/Gemfile - Just define the following:**

gem "paperclip"
gem "mongoid-paperclip", :require => "mongoid_paperclip"
gem "aws-s3", :require => "aws/s3"
gem "aws-s3", :require => "aws/s3"

Next let's assume we have a User model and we want to allow our users to upload an avatar.

**Rails.root/app/models/user.rb**
**Rails.root/app/models/user.rb - include the Mongoid::Paperclip and invoke the class method**

class User
include Mongoid::Document
Expand All @@ -31,16 +29,16 @@ Next let's assume we have a User model and we want to allow our users to upload
end


And there you go!
-----------------
That's it
--------

As you can see you use it in practically the same as when you use vanilla Paperclip. However, since we're using Mongoid and not ActiveRecord, we do not have a schema or any migrations. So with **Mongoid::Paperclip** when you invoke the `has_attached_file :avatar` it will create the necessary Mongoid **fields** for the specified attribute (`:avatar` in this case).
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_attached_file` method will automatically define the necessary `:avatar` fields for you in the background.


A more complex example
----------------------

Just like vanilla Paperclip, Mongoid::Paperclip takes a second argument (hash of options) for the `has_attached_file` method, so you can do more complex things such as in the following example.
Just like Paperclip, Mongoid::Paperclip takes a second argument (hash of options) for the `has_attached_file` method, so you can do more complex things such as in the following example.

class User
include Mongoid::Document
Expand All @@ -67,7 +65,15 @@ Just like vanilla Paperclip, Mongoid::Paperclip takes a second argument (hash of
},
:convert_options => { :all => '-background white -flatten +matte' }
end

@user.pictures.each do |picture|
<%= picture.attachment.url %>
end


There you go
------------

Quite a lot of people have been looking for a solution to use [Paperclip](https://github.com/thoughtbot/paperclip) with [Mongoid](http://mongoid.org/) so I hope this helps!
Quite a lot of people have been looking for a solution to use Paperclip with Mongoid so I hope this helps!

&copy; Copyright [Michael van Rooijen](http://michaelvanrooijen.com/)
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.
9 changes: 8 additions & 1 deletion lib/mongoid_paperclip.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
begin
require "paperclip"
rescue LoadError
puts "Mongoid::Paperclip requires that you install the Paperclip gem."
exit
end

##
# The logger is set to the Active Record logger by Paperclip itself.
# Because of this, we set the logger to false as "default" so that it doesn't raise
Expand All @@ -24,7 +31,7 @@

##
# The Mongoid::Paperclip extension
# Makes Paperclip play nice with the Mongoid ORM
# Makes Paperclip play nice with the Mongoid ODM
#
# Example:
#
Expand Down
6 changes: 3 additions & 3 deletions mongoid_paperclip.gemspec → mongoid-paperclip.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Gem::Specification.new do |gem|

gem.name = 'mongoid-paperclip'
gem.version = '0.0.1'
gem.version = '0.0.2'
gem.platform = Gem::Platform::RUBY
gem.authors = 'Michael van Rooijen'
gem.email = '[email protected]'
gem.homepage = 'https://github.com/meskyanichi/mongoid-paperclip'
gem.summary = 'Mongoid::Paperclip enables you to use Paperclip with the Mongoid ORM for MongoDB.'
gem.description = 'Mongoid::Paperclip enables you to use Paperclip with the Mongoid ORM for MongoDB.'
gem.summary = 'Mongoid::Paperclip enables you to use Paperclip with the Mongoid ODM for MongoDB.'
gem.description = 'Mongoid::Paperclip enables you to use Paperclip with the Mongoid ODM for MongoDB.'

gem.files = %x[git ls-files].split("\n")
gem.test_files = %x[git ls-files -- {spec}/*].split("\n")
Expand Down

0 comments on commit 74dce54

Please sign in to comment.