mongoid-paperclip 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +12 -12
- data/lib/mongoid_paperclip.rb +25 -6
- data/mongoid-paperclip.gemspec +3 -3
- metadata +4 -16
data/README.md
CHANGED
@@ -3,7 +3,7 @@ Mongoid::Paperclip - Making Paperclip play nice with Mongoid ODM
|
|
3
3
|
|
4
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
5
|
|
6
|
-
This is actually easier and faster to set up than when using Paperclip the ActiveRecord ORM.
|
6
|
+
This is actually easier and faster to set up than when using Paperclip and the ActiveRecord ORM.
|
7
7
|
This example assumes you are using **Ruby on Rails 3** and **Bundler**. However it doesn't require either.
|
8
8
|
|
9
9
|
|
@@ -16,42 +16,42 @@ Simply define the `mongoid-paperclip` gem inside your `Gemfile`. Additionally, y
|
|
16
16
|
|
17
17
|
gem "mongoid-paperclip", :require => "mongoid_paperclip"
|
18
18
|
gem "aws-s3", :require => "aws/s3"
|
19
|
-
|
19
|
+
|
20
20
|
Next let's assume we have a User model and we want to allow our users to upload an avatar.
|
21
21
|
|
22
|
-
**Rails.root/app/models/user.rb - include the Mongoid::Paperclip and invoke the class method**
|
22
|
+
**Rails.root/app/models/user.rb - include the Mongoid::Paperclip module and invoke the provided class method**
|
23
23
|
|
24
24
|
class User
|
25
25
|
include Mongoid::Document
|
26
26
|
include Mongoid::Paperclip
|
27
|
-
|
28
|
-
|
27
|
+
|
28
|
+
has_mongoid_attached_file :avatar
|
29
29
|
end
|
30
30
|
|
31
31
|
|
32
32
|
That's it
|
33
33
|
--------
|
34
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 `
|
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
36
|
|
37
37
|
|
38
38
|
A more complex example
|
39
39
|
----------------------
|
40
40
|
|
41
|
-
Just like Paperclip, Mongoid::Paperclip takes a second argument (hash of options) for the `
|
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
42
|
|
43
43
|
class User
|
44
44
|
include Mongoid::Document
|
45
45
|
embeds_many :pictures
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
class Picture
|
49
49
|
include Mongoid::Document
|
50
50
|
include Mongoid::Paperclip
|
51
|
-
|
51
|
+
|
52
52
|
embedded_in :user, :inverse_of => :pictures
|
53
|
-
|
54
|
-
|
53
|
+
|
54
|
+
has_mongoid_attached_file :attachment,
|
55
55
|
:path => ':attachment/:id/:style.:extension',
|
56
56
|
:storage => :s3,
|
57
57
|
:url => ':s3_alias_url',
|
@@ -65,7 +65,7 @@ Just like Paperclip, Mongoid::Paperclip takes a second argument (hash of options
|
|
65
65
|
},
|
66
66
|
:convert_options => { :all => '-background white -flatten +matte' }
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
@user.pictures.each do |picture|
|
70
70
|
<%= picture.attachment.url %>
|
71
71
|
end
|
data/lib/mongoid_paperclip.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
begin
|
2
4
|
require "paperclip"
|
3
5
|
rescue LoadError
|
@@ -39,15 +41,15 @@ end
|
|
39
41
|
# include Mongoid::Document
|
40
42
|
# include Mongoid::Paperclip
|
41
43
|
#
|
42
|
-
#
|
44
|
+
# has_mongoid_attached_file :avatar
|
43
45
|
# end
|
44
46
|
#
|
45
47
|
# The above example is all you need to do. This will load the Paperclip library into the User model
|
46
|
-
# and add the "
|
48
|
+
# and add the "has_mongoid_attached_file" class method. Provide this method with the same values as you would
|
47
49
|
# when using "vanilla Paperclip". The first parameter is a symbol [:field] and the second parameter is a hash of options [options = {}].
|
48
50
|
#
|
49
51
|
# Unlike Paperclip for ActiveRecord, since MongoDB does not use "schema" or "migrations", Mongoid::Paperclip automatically adds the neccesary "fields"
|
50
|
-
# to your Model (MongoDB collection) when you invoke the "#
|
52
|
+
# to your Model (MongoDB collection) when you invoke the "#has_mongoid_attached_file" method. When you invoke "has_mongoid_attached_file :avatar" it will
|
51
53
|
# automatially add the following fields:
|
52
54
|
#
|
53
55
|
# field :avatar_file_name, :type => String
|
@@ -67,20 +69,37 @@ module Mongoid
|
|
67
69
|
module ClassMethods
|
68
70
|
|
69
71
|
##
|
70
|
-
# Adds Mongoid::Paperclip's "#
|
72
|
+
# Adds Mongoid::Paperclip's "#has_mongoid_attached_file" class method to the model
|
71
73
|
# which includes Paperclip and Paperclip::Glue in to the model. Additionally
|
72
74
|
# it'll also add the required fields for Paperclip since MongoDB is schemaless and doesn't
|
73
75
|
# have migrations.
|
74
|
-
def
|
76
|
+
def has_mongoid_attached_file(field, options = {})
|
77
|
+
|
78
|
+
##
|
79
|
+
# Include Paperclip and Paperclip::Glue for compatibility
|
75
80
|
include ::Paperclip
|
76
81
|
include ::Paperclip::Glue
|
82
|
+
|
83
|
+
##
|
84
|
+
# Invoke Paperclip's #has_attached_file method and passes in the
|
85
|
+
# arguments specified by the user that invoked Mongoid::Paperclip#has_mongoid_attached_file
|
77
86
|
has_attached_file(field, options)
|
87
|
+
|
88
|
+
##
|
89
|
+
# Define the necessary collection fields in Mongoid for Paperclip
|
78
90
|
field(:"#{field}_file_name", :type => String)
|
79
91
|
field(:"#{field}_content_type", :type => String)
|
80
92
|
field(:"#{field}_file_size", :type => Integer)
|
81
93
|
field(:"#{field}_updated_at", :type => DateTime)
|
82
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
|
83
102
|
end
|
84
103
|
|
85
104
|
end
|
86
|
-
end
|
105
|
+
end
|
data/mongoid-paperclip.gemspec
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
Gem::Specification.new do |gem|
|
4
4
|
|
5
5
|
gem.name = 'mongoid-paperclip'
|
6
|
-
gem.version = '0.0.
|
6
|
+
gem.version = '0.0.3'
|
7
7
|
gem.platform = Gem::Platform::RUBY
|
8
8
|
gem.authors = 'Michael van Rooijen'
|
9
9
|
gem.email = 'meskyanichi@gmail.com'
|
@@ -14,7 +14,7 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.files = %x[git ls-files].split("\n")
|
15
15
|
gem.test_files = %x[git ls-files -- {spec}/*].split("\n")
|
16
16
|
gem.require_path = 'lib'
|
17
|
-
|
17
|
+
|
18
18
|
gem.add_dependency 'paperclip', ['~> 2.3.6']
|
19
19
|
|
20
|
-
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-paperclip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
version: 0.0.2
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.3
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Michael van Rooijen
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
13
|
+
date: 2011-02-08 00:00:00 +01:00
|
18
14
|
default_executable:
|
19
15
|
dependencies:
|
20
16
|
- !ruby/object:Gem::Dependency
|
@@ -25,10 +21,6 @@ dependencies:
|
|
25
21
|
requirements:
|
26
22
|
- - ~>
|
27
23
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 2
|
30
|
-
- 3
|
31
|
-
- 6
|
32
24
|
version: 2.3.6
|
33
25
|
type: :runtime
|
34
26
|
version_requirements: *id001
|
@@ -59,21 +51,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
51
|
requirements:
|
60
52
|
- - ">="
|
61
53
|
- !ruby/object:Gem::Version
|
62
|
-
segments:
|
63
|
-
- 0
|
64
54
|
version: "0"
|
65
55
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
56
|
none: false
|
67
57
|
requirements:
|
68
58
|
- - ">="
|
69
59
|
- !ruby/object:Gem::Version
|
70
|
-
segments:
|
71
|
-
- 0
|
72
60
|
version: "0"
|
73
61
|
requirements: []
|
74
62
|
|
75
63
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.
|
64
|
+
rubygems_version: 1.5.0
|
77
65
|
signing_key:
|
78
66
|
specification_version: 3
|
79
67
|
summary: Mongoid::Paperclip enables you to use Paperclip with the Mongoid ODM for MongoDB.
|