glebtv-mongoid-paperclip 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +4 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +45 -0
- data/LICENSE +21 -0
- data/README.md +103 -0
- data/Rakefile +1 -0
- data/glebtv-mongoid-paperclip.gemspec +24 -0
- data/lib/glebtv-mongoid-paperclip.rb +105 -0
- data/lib/mongoid-paperclip/version.rb +5 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NGM0YjAxZjY2ODU1ODFkYjAxZmU0ZWIyYTkwOWUxMWNhNjhmOWQ4Ng==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ODM4NjBiYjk2NDMwYjYzZDgyZDViNzc5NTA1MWJiMzFkNWUyOGRiMA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDQ3NjQzMjU3NmM3YzBjNzdjMjM1Mjg3OTg4YzhhNjgzNDkyODlhNjZkZmVi
|
10
|
+
YzFiMTk2MTdjNjM1YmU2MTZhMWJiZDQ2NjBkY2FmMzI0NTI5OTU2ODhjYTY1
|
11
|
+
NWRiOGE3ZWJmN2Y3MWMwZTU2ZTAwODU2NGQ5ZDlmYTQ2MDdhYmY=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZGMwMmE0ZTA0NDMwNjMyY2EyNDZkZGMyMmU2NWE5MDI0YzJmMThiNDNkNzk1
|
14
|
+
Y2RlOGVkNzlmYTdmMjUwODlkMmMxODc0ZjNkM2M3MDU4YjY2OTYzOGY5MzUx
|
15
|
+
NDI3OWVmYzc1NmRmOTdmNjkwYTNiYzA4ODU3MmNjNjVjNzFkN2I=
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
mongoid-paperclip
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
glebtv-mongoid-paperclip (0.1.0)
|
5
|
+
paperclip (>= 2.3.6)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (4.0.0)
|
11
|
+
activesupport (= 4.0.0)
|
12
|
+
builder (~> 3.1.0)
|
13
|
+
activesupport (4.0.0)
|
14
|
+
i18n (~> 0.6, >= 0.6.4)
|
15
|
+
minitest (~> 4.2)
|
16
|
+
multi_json (~> 1.3)
|
17
|
+
thread_safe (~> 0.1)
|
18
|
+
tzinfo (~> 0.3.37)
|
19
|
+
atomic (1.1.14)
|
20
|
+
builder (3.1.4)
|
21
|
+
climate_control (0.0.3)
|
22
|
+
activesupport (>= 3.0)
|
23
|
+
cocaine (0.5.2)
|
24
|
+
climate_control (>= 0.0.3, < 1.0)
|
25
|
+
i18n (0.6.5)
|
26
|
+
mime-types (1.25)
|
27
|
+
minitest (4.7.5)
|
28
|
+
multi_json (1.8.2)
|
29
|
+
paperclip (3.5.1)
|
30
|
+
activemodel (>= 3.0.0)
|
31
|
+
activesupport (>= 3.0.0)
|
32
|
+
cocaine (~> 0.5.0)
|
33
|
+
mime-types
|
34
|
+
rake (10.1.0)
|
35
|
+
thread_safe (0.1.3)
|
36
|
+
atomic
|
37
|
+
tzinfo (0.3.38)
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
bundler (~> 1.3)
|
44
|
+
glebtv-mongoid-paperclip!
|
45
|
+
rake
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) Michael van Rooijen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
Mongoid::Paperclip - Making Paperclip play nice with Mongoid ODM
|
2
|
+
================================================================
|
3
|
+
|
4
|
+
As the title suggests: `Mongoid::Paperclip` makes it easy to hook up [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 Paperclip and the ActiveRecord ORM.
|
7
|
+
This example assumes you are using **Ruby on Rails 3** and **Bundler**. However it doesn't require either.
|
8
|
+
|
9
|
+
|
10
|
+
Setting it up
|
11
|
+
-------------
|
12
|
+
|
13
|
+
Simply define the `mongoid-paperclip` gem inside your `Gemfile`. Additionally, you can define the `aws-sdk` 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`.*
|
14
|
+
|
15
|
+
**Rails.root/Gemfile - Just define the following:**
|
16
|
+
|
17
|
+
gem "mongoid-paperclip", :require => "mongoid_paperclip"
|
18
|
+
gem 'aws-sdk', '~> 1.3.4'
|
19
|
+
|
20
|
+
Next let's assume we have a User model and we want to allow our users to upload an avatar.
|
21
|
+
|
22
|
+
**Rails.root/app/models/user.rb - include the Mongoid::Paperclip module and invoke the provided class method**
|
23
|
+
|
24
|
+
class User
|
25
|
+
include Mongoid::Document
|
26
|
+
include Mongoid::Paperclip
|
27
|
+
|
28
|
+
has_mongoid_attached_file :avatar
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
That's it
|
33
|
+
--------
|
34
|
+
|
35
|
+
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.
|
36
|
+
|
37
|
+
|
38
|
+
A more complex example
|
39
|
+
----------------------
|
40
|
+
|
41
|
+
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.
|
42
|
+
|
43
|
+
class User
|
44
|
+
include Mongoid::Document
|
45
|
+
embeds_many :pictures
|
46
|
+
end
|
47
|
+
|
48
|
+
class Picture
|
49
|
+
include Mongoid::Document
|
50
|
+
include Mongoid::Paperclip
|
51
|
+
|
52
|
+
embedded_in :user, :inverse_of => :pictures
|
53
|
+
|
54
|
+
has_mongoid_attached_file :attachment,
|
55
|
+
:path => ':attachment/:id/:style.:extension',
|
56
|
+
:storage => :s3,
|
57
|
+
:url => ':s3_alias_url',
|
58
|
+
:s3_host_alias => 'something.cloudfront.net',
|
59
|
+
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
|
60
|
+
:styles => {
|
61
|
+
:original => ['1920x1680>', :jpg],
|
62
|
+
:small => ['100x100#', :jpg],
|
63
|
+
:medium => ['250x250', :jpg],
|
64
|
+
:large => ['500x500>', :jpg]
|
65
|
+
},
|
66
|
+
:convert_options => { :all => '-background white -flatten +matte' }
|
67
|
+
end
|
68
|
+
|
69
|
+
@user.pictures.each do |picture|
|
70
|
+
<%= picture.attachment.url %>
|
71
|
+
end
|
72
|
+
|
73
|
+
Note on embedded documents: if you plan to save or update the parent document, you MUST add cascade_callbacks: true to your
|
74
|
+
embeds_XXX statement. Otherwise, your data will be updated but the paperclip functions will not run to copy/update your file.
|
75
|
+
|
76
|
+
In the above example:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
class User
|
80
|
+
...
|
81
|
+
embeds_many :pictures, :cascade_callbacks => true
|
82
|
+
accepts_nested_attributes_for :pictures, ...
|
83
|
+
attr_accepted :pictures_attributes, ...
|
84
|
+
...
|
85
|
+
end
|
86
|
+
|
87
|
+
@user.update_attributes({ ... :pictures => [...] })
|
88
|
+
```
|
89
|
+
|
90
|
+
|
91
|
+
There you go
|
92
|
+
------------
|
93
|
+
|
94
|
+
Quite a lot of people have been looking for a solution to use Paperclip with Mongoid so I hope this helps!
|
95
|
+
|
96
|
+
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.
|
97
|
+
|
98
|
+
|
99
|
+
License
|
100
|
+
-------
|
101
|
+
|
102
|
+
Mongoid::Paperclip is released under the MIT license. See LICENSE for more information.
|
103
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid-paperclip/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'glebtv-mongoid-paperclip'
|
8
|
+
gem.version = Mongoid::Paperclip::VERSION
|
9
|
+
gem.platform = Gem::Platform::RUBY
|
10
|
+
gem.authors = ['GlebTV', 'Michael van Rooijen']
|
11
|
+
gem.email = 'meskyanichi@gmail.com'
|
12
|
+
gem.homepage = 'https://github.com/glebtv/mongoid-paperclip'
|
13
|
+
gem.summary = 'Fork of Mongoid::Paperclip'
|
14
|
+
gem.description = 'Mongoid::Paperclip enables you to use Paperclip with the Mongoid ODM for MongoDB.'
|
15
|
+
|
16
|
+
gem.files = %x[git ls-files].split("\n")
|
17
|
+
gem.test_files = %x[git ls-files -- {spec}/*].split("\n")
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_dependency 'paperclip', ['>= 2.3.6']
|
21
|
+
gem.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "mongoid-paperclip/version"
|
4
|
+
|
5
|
+
begin
|
6
|
+
require "paperclip"
|
7
|
+
rescue LoadError
|
8
|
+
puts "Mongoid::Paperclip requires that you install the Paperclip gem."
|
9
|
+
exit
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# the id of mongoid is not integer, correct the id_partitioin.
|
14
|
+
Paperclip.interpolates :id_partition do |attachment, style|
|
15
|
+
attachment.instance.id.to_s.scan(/.{4}/).join("/")
|
16
|
+
end
|
17
|
+
|
18
|
+
##
|
19
|
+
# mongoid criteria uses a different syntax.
|
20
|
+
module Paperclip
|
21
|
+
module Helpers
|
22
|
+
def each_instance_with_attachment(klass, name)
|
23
|
+
class_for(klass).unscoped.where("#{name}_file_name".to_sym.ne => nil).each do |instance|
|
24
|
+
yield(instance)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
# The Mongoid::Paperclip extension
|
32
|
+
# Makes Paperclip play nice with the Mongoid ODM
|
33
|
+
#
|
34
|
+
# Example:
|
35
|
+
#
|
36
|
+
# class User
|
37
|
+
# include Mongoid::Document
|
38
|
+
# include Mongoid::Paperclip
|
39
|
+
#
|
40
|
+
# has_mongoid_attached_file :avatar
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# The above example is all you need to do. This will load the Paperclip library into the User model
|
44
|
+
# and add the "has_mongoid_attached_file" class method. Provide this method with the same values as you would
|
45
|
+
# when using "vanilla Paperclip". The first parameter is a symbol [:field] and the second parameter is a hash of options [options = {}].
|
46
|
+
#
|
47
|
+
# Unlike Paperclip for ActiveRecord, since MongoDB does not use "schema" or "migrations", Mongoid::Paperclip automatically adds the neccesary "fields"
|
48
|
+
# to your Model (MongoDB collection) when you invoke the "#has_mongoid_attached_file" method. When you invoke "has_mongoid_attached_file :avatar" it will
|
49
|
+
# automatially add the following fields:
|
50
|
+
#
|
51
|
+
# field :avatar_file_name, :type => String
|
52
|
+
# field :avatar_content_type, :type => String
|
53
|
+
# field :avatar_file_size, :type => Integer
|
54
|
+
# field :avatar_updated_at, :type => DateTime
|
55
|
+
# field :avatar_fingerprint, :type => String
|
56
|
+
#
|
57
|
+
module Mongoid
|
58
|
+
module Paperclip
|
59
|
+
|
60
|
+
##
|
61
|
+
# Extends the model with the defined Class methods
|
62
|
+
def self.included(base)
|
63
|
+
base.extend(ClassMethods)
|
64
|
+
end
|
65
|
+
|
66
|
+
module ClassMethods
|
67
|
+
|
68
|
+
##
|
69
|
+
# Adds Mongoid::Paperclip's "#has_mongoid_attached_file" class method to the model
|
70
|
+
# which includes Paperclip and Paperclip::Glue in to the model. Additionally
|
71
|
+
# it'll also add the required fields for Paperclip since MongoDB is schemaless and doesn't
|
72
|
+
# have migrations.
|
73
|
+
def has_mongoid_attached_file(field, options = {})
|
74
|
+
|
75
|
+
##
|
76
|
+
# Include Paperclip and Paperclip::Glue for compatibility
|
77
|
+
unless self.ancestors.include?(::Paperclip)
|
78
|
+
include ::Paperclip
|
79
|
+
include ::Paperclip::Glue
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# Invoke Paperclip's #has_attached_file method and passes in the
|
84
|
+
# arguments specified by the user that invoked Mongoid::Paperclip#has_mongoid_attached_file
|
85
|
+
has_attached_file(field, options)
|
86
|
+
|
87
|
+
##
|
88
|
+
# Define the necessary collection fields in Mongoid for Paperclip
|
89
|
+
field(:"#{field}_file_name", :type => String)
|
90
|
+
field(:"#{field}_content_type", :type => String)
|
91
|
+
field(:"#{field}_file_size", :type => Integer)
|
92
|
+
field(:"#{field}_updated_at", :type => DateTime)
|
93
|
+
field(:"#{field}_fingerprint", :type => String)
|
94
|
+
end
|
95
|
+
|
96
|
+
##
|
97
|
+
# This method is deprecated
|
98
|
+
def has_attached_file(field, options = {})
|
99
|
+
raise "Mongoid::Paperclip#has_attached_file is deprecated, " +
|
100
|
+
"Use 'has_mongoid_attached_file' instead"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glebtv-mongoid-paperclip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- GlebTV
|
8
|
+
- Michael van Rooijen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: paperclip
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ! '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 2.3.6
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ! '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 2.3.6
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.3'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.3'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: Mongoid::Paperclip enables you to use Paperclip with the Mongoid ODM
|
57
|
+
for MongoDB.
|
58
|
+
email: meskyanichi@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- .ruby-gemset
|
65
|
+
- .ruby-version
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- glebtv-mongoid-paperclip.gemspec
|
72
|
+
- lib/glebtv-mongoid-paperclip.rb
|
73
|
+
- lib/mongoid-paperclip/version.rb
|
74
|
+
homepage: https://github.com/glebtv/mongoid-paperclip
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.0.6
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Fork of Mongoid::Paperclip
|
97
|
+
test_files: []
|