mongoid-paperclip 0.0.1
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/.gitignore +2 -0
- data/README.md +73 -0
- data/lib/mongoid_paperclip.rb +79 -0
- data/mongoid_paperclip.gemspec +20 -0
- metadata +81 -0
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
Mongoid::Paperclip - Making Paperclip play nice with Mongoid ORM
|
2
|
+
================================================================
|
3
|
+
|
4
|
+
As the title suggests, using this gem you will be able to use [Paperclip](https://github.com/thoughtbot/paperclip) with [Mongoid](http://mongoid.org/).
|
5
|
+
|
6
|
+
This is actually **easier** and **faster** to set up than when using the ActiveRecord ORM.
|
7
|
+
|
8
|
+
This example assumes you are using **Ruby on Rails 3** and **Bundler**. However it doesn't require either.
|
9
|
+
|
10
|
+
|
11
|
+
Setting it up in a few seconds
|
12
|
+
------------------------------
|
13
|
+
|
14
|
+
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.
|
15
|
+
|
16
|
+
**Rails.root/Gemfile**
|
17
|
+
|
18
|
+
gem "paperclip"
|
19
|
+
gem "mongoid-paperclip", :require => "mongoid_paperclip"
|
20
|
+
gem "aws-s3", :require => "aws/s3"
|
21
|
+
|
22
|
+
Next let's assume we have a User model and we want to allow our users to upload an avatar.
|
23
|
+
|
24
|
+
**Rails.root/app/models/user.rb**
|
25
|
+
|
26
|
+
class User
|
27
|
+
include Mongoid::Document
|
28
|
+
include Mongoid::Paperclip
|
29
|
+
|
30
|
+
has_attached_file :avatar
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
And there you go!
|
35
|
+
-----------------
|
36
|
+
|
37
|
+
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).
|
38
|
+
|
39
|
+
|
40
|
+
A more complex example
|
41
|
+
----------------------
|
42
|
+
|
43
|
+
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.
|
44
|
+
|
45
|
+
class User
|
46
|
+
include Mongoid::Document
|
47
|
+
embeds_many :pictures
|
48
|
+
end
|
49
|
+
|
50
|
+
class Picture
|
51
|
+
include Mongoid::Document
|
52
|
+
include Mongoid::Paperclip
|
53
|
+
|
54
|
+
embedded_in :user, :inverse_of => :pictures
|
55
|
+
|
56
|
+
has_attached_file :attachment,
|
57
|
+
:path => ':attachment/:id/:style.:extension',
|
58
|
+
:storage => :s3,
|
59
|
+
:url => ':s3_alias_url',
|
60
|
+
:s3_host_alias => 'something.cloudfront.net',
|
61
|
+
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
|
62
|
+
:styles => {
|
63
|
+
:original => ['1920x1680>', :jpg],
|
64
|
+
:small => ['100x100#', :jpg],
|
65
|
+
:medium => ['250x250', :jpg],
|
66
|
+
:large => ['500x500>', :jpg]
|
67
|
+
},
|
68
|
+
:convert_options => { :all => '-background white -flatten +matte' }
|
69
|
+
end
|
70
|
+
|
71
|
+
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!
|
72
|
+
|
73
|
+
© Copyright [Michael van Rooijen](http://michaelvanrooijen.com/)
|
@@ -0,0 +1,79 @@
|
|
1
|
+
##
|
2
|
+
# The logger is set to the Active Record logger by Paperclip itself.
|
3
|
+
# Because of this, we set the logger to false as "default" so that it doesn't raise
|
4
|
+
# an "uninitialized constant" exception.
|
5
|
+
#
|
6
|
+
# You can manually change loggers by adding for example an initializer and configuring the logger, like so:
|
7
|
+
#
|
8
|
+
# Paperclip.options[:log] = MyLogger.log
|
9
|
+
#
|
10
|
+
Paperclip.options[:log] = false
|
11
|
+
|
12
|
+
##
|
13
|
+
# If Ruby on Rails is defined and the logger method exists, the Paperclip logger
|
14
|
+
# will be set to the Rails logger by default. You may overwrite the logger by re-defining the
|
15
|
+
# logger in for example an initializer file, like so:
|
16
|
+
#
|
17
|
+
# Paperclip.options[:log] = MyLogger.log
|
18
|
+
#
|
19
|
+
if defined?(Rails)
|
20
|
+
if Rails.respond_to?(:logger)
|
21
|
+
Paperclip.options[:log] = Rails.logger
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# The Mongoid::Paperclip extension
|
27
|
+
# Makes Paperclip play nice with the Mongoid ORM
|
28
|
+
#
|
29
|
+
# Example:
|
30
|
+
#
|
31
|
+
# class User
|
32
|
+
# include Mongoid::Document
|
33
|
+
# include Mongoid::Paperclip
|
34
|
+
#
|
35
|
+
# has_attached_file :avatar
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# The above example is all you need to do. This will load the Paperclip library into the User model
|
39
|
+
# and add the "has_attached_file" class method. Provide this method with the same values as you would
|
40
|
+
# when using "vanilla Paperclip". The first parameter is a symbol [:field] and the second parameter is a hash of options [options = {}].
|
41
|
+
#
|
42
|
+
# Unlike Paperclip for ActiveRecord, since MongoDB does not use "schema" or "migrations", Mongoid::Paperclip automatically adds the neccesary "fields"
|
43
|
+
# to your Model (MongoDB collection) when you invoke the "#has_attached_file" method. When you invoke "has_attached_file :avatar" it will
|
44
|
+
# automatially add the following fields:
|
45
|
+
#
|
46
|
+
# field :avatar_file_name, :type => String
|
47
|
+
# field :avatar_content_type, :type => String
|
48
|
+
# field :avatar_file_size, :type => Integer
|
49
|
+
# field :avatar_updated_at, :type => DateTime
|
50
|
+
#
|
51
|
+
module Mongoid
|
52
|
+
module Paperclip
|
53
|
+
|
54
|
+
##
|
55
|
+
# Extends the model with the defined Class methods
|
56
|
+
def self.included(base)
|
57
|
+
base.extend(ClassMethods)
|
58
|
+
end
|
59
|
+
|
60
|
+
module ClassMethods
|
61
|
+
|
62
|
+
##
|
63
|
+
# Adds Mongoid::Paperclip's "#has_attached_file" class method to the model
|
64
|
+
# which includes Paperclip and Paperclip::Glue in to the model. Additionally
|
65
|
+
# it'll also add the required fields for Paperclip since MongoDB is schemaless and doesn't
|
66
|
+
# have migrations.
|
67
|
+
def has_attached_file(field, options = {})
|
68
|
+
include ::Paperclip
|
69
|
+
include ::Paperclip::Glue
|
70
|
+
has_attached_file(field, options)
|
71
|
+
field(:"#{field}_file_name", :type => String)
|
72
|
+
field(:"#{field}_content_type", :type => String)
|
73
|
+
field(:"#{field}_file_size", :type => Integer)
|
74
|
+
field(:"#{field}_updated_at", :type => DateTime)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
|
5
|
+
gem.name = 'mongoid-paperclip'
|
6
|
+
gem.version = '0.0.1'
|
7
|
+
gem.platform = Gem::Platform::RUBY
|
8
|
+
gem.authors = 'Michael van Rooijen'
|
9
|
+
gem.email = 'meskyanichi@gmail.com'
|
10
|
+
gem.homepage = 'https://github.com/meskyanichi/mongoid-paperclip'
|
11
|
+
gem.summary = 'Mongoid::Paperclip enables you to use Paperclip with the Mongoid ORM for MongoDB.'
|
12
|
+
gem.description = 'Mongoid::Paperclip enables you to use Paperclip with the Mongoid ORM for MongoDB.'
|
13
|
+
|
14
|
+
gem.files = %x[git ls-files].split("\n")
|
15
|
+
gem.test_files = %x[git ls-files -- {spec}/*].split("\n")
|
16
|
+
gem.require_path = 'lib'
|
17
|
+
|
18
|
+
gem.add_dependency 'paperclip', ['~> 2.3.6']
|
19
|
+
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-paperclip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Michael van Rooijen
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-10 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: paperclip
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 3
|
31
|
+
- 6
|
32
|
+
version: 2.3.6
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Mongoid::Paperclip enables you to use Paperclip with the Mongoid ORM for MongoDB.
|
36
|
+
email: meskyanichi@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- README.md
|
46
|
+
- lib/mongoid_paperclip.rb
|
47
|
+
- mongoid_paperclip.gemspec
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: https://github.com/meskyanichi/mongoid-paperclip
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.3.7
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Mongoid::Paperclip enables you to use Paperclip with the Mongoid ORM for MongoDB.
|
80
|
+
test_files: []
|
81
|
+
|