mongoid_paperclip_queue 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.project +17 -0
- data/.rvmrc +1 -0
- data/CONTRIBUTING +12 -0
- data/Gemfile +3 -0
- data/LICENSE +23 -0
- data/README.textile +57 -0
- data/lib/mongoid_paperclip_queue.rb +105 -0
- data/mongoid_paperclip_queue.gemspec +20 -0
- data/rails/init.rb +1 -0
- metadata +99 -0
data/.gitignore
ADDED
data/.project
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<projectDescription>
|
3
|
+
<name>delayed_paperclip</name>
|
4
|
+
<comment></comment>
|
5
|
+
<projects>
|
6
|
+
</projects>
|
7
|
+
<buildSpec>
|
8
|
+
<buildCommand>
|
9
|
+
<name>com.aptana.ide.core.unifiedBuilder</name>
|
10
|
+
<arguments>
|
11
|
+
</arguments>
|
12
|
+
</buildCommand>
|
13
|
+
</buildSpec>
|
14
|
+
<natures>
|
15
|
+
<nature>com.aptana.ruby.core.rubynature</nature>
|
16
|
+
</natures>
|
17
|
+
</projectDescription>
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm gemset use mongoid_paperclip_queue --create
|
data/CONTRIBUTING
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Contributor Policy
|
2
|
+
=================
|
3
|
+
|
4
|
+
Commit bit. If you have a commit accepted into the project then you get full git access to the repo. If I don't give you this in a timely manner just send me a message.
|
5
|
+
|
6
|
+
|
7
|
+
Versioning
|
8
|
+
=========
|
9
|
+
|
10
|
+
Don't bump the version in any changes you make or pull in to the project. I'll maintain rights to push the gem to rubygems.org and make releases when appropriate.
|
11
|
+
|
12
|
+
And please keep the README up to date. Thank you!
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
LICENSE
|
2
|
+
|
3
|
+
The MIT License
|
4
|
+
|
5
|
+
Copyright (c) 2011 Kelly Martin and Fully Brand LLC
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
9
|
+
in the Software without restriction, including without limitation the rights
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
12
|
+
furnished to do so, subject to the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be included in
|
15
|
+
all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23
|
+
THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
h1. Mongoid::PaperclipQueue
|
2
|
+
|
3
|
+
Mongoid::PaperclipQueue is a complete rewrite of "Delayed_Paperclip":http://github.com/jstorimer/delayed_paperclip and "Mongoid_Paperclip":http://github.com/meskyanichi/mongoid-paperclip to allow those of us using Mongoid to process "Paperclip":http://github.com/thoughtbot/paperclip attachments in the background using "Resque":http://github.com/defunkt/resque.
|
4
|
+
|
5
|
+
h2. Why?
|
6
|
+
|
7
|
+
We all know how important it is to keep our page load times down, so this allows us to dump all that processing to Resque to perform in the background.
|
8
|
+
|
9
|
+
h2. Installation
|
10
|
+
|
11
|
+
Install the gem:
|
12
|
+
|
13
|
+
<pre><code>sudo gem install mongoid_paperclip_queue</code></pre>
|
14
|
+
|
15
|
+
Or for Rails 3, to your Gemfile:
|
16
|
+
|
17
|
+
<pre><code>
|
18
|
+
gem 'mongoid_paperclip_queue'
|
19
|
+
</code></pre>
|
20
|
+
|
21
|
+
h3. Dependencies:
|
22
|
+
* Mongoid
|
23
|
+
* Paperclip
|
24
|
+
* Resque
|
25
|
+
|
26
|
+
h2. Usage
|
27
|
+
|
28
|
+
In your model:
|
29
|
+
|
30
|
+
<pre><code>
|
31
|
+
class User
|
32
|
+
extend Mongoid::PaperclipQueue
|
33
|
+
|
34
|
+
has_queued_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
|
35
|
+
|
36
|
+
end
|
37
|
+
</code></pre>
|
38
|
+
|
39
|
+
Paperclip will behave exactly "like they describe":http://github.com/thoughtbot/paperclip.
|
40
|
+
|
41
|
+
h3. Resque
|
42
|
+
|
43
|
+
Make sure that you have "Resque":http://github.com/defunkt/resque up and running. The jobs will be dispatched to the <code>:paperclip</code> queue, so you can correctly dispatch your worker. Configure resque and your workers exactly as you would otherwise.
|
44
|
+
|
45
|
+
h3. Detect the processing state
|
46
|
+
|
47
|
+
Processing? detection is built in. We take advantage of Redis since it should already be running, and we can keep our <code>#{attachment_name}_processing</code> field out of our MongoDB and into a more temporary key store. The temporary image url isn't saved anywhere, so you'll have to specify that on your own, but it's easy to use:
|
48
|
+
|
49
|
+
<pre><code>
|
50
|
+
@user = User.find(1)
|
51
|
+
url = @user.avatar.processing? ? "/images/missing.png" : @user.avatar.url
|
52
|
+
|
53
|
+
</code></pre>
|
54
|
+
|
55
|
+
h2. Contributing
|
56
|
+
|
57
|
+
Checkout out CONTRIBUTING for more info.
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module Mongoid::PaperclipQueue
|
2
|
+
|
3
|
+
class Queue
|
4
|
+
|
5
|
+
@queue = :paperclip
|
6
|
+
|
7
|
+
def self.enqueue(klass,field,id)
|
8
|
+
::Resque.enqueue(self,klass,field,id)
|
9
|
+
end
|
10
|
+
def self.perform(klass,field,id)
|
11
|
+
klass.constantize.find(id).do_reprocessing_on field
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
module Redis
|
17
|
+
def server=(srv)
|
18
|
+
case srv
|
19
|
+
when String
|
20
|
+
if srv =~ /redis\:\/\//
|
21
|
+
server = ::Redis.connect(:url => srv, :thread_safe => true)
|
22
|
+
else
|
23
|
+
srv, namespace = srv.split('/', 2)
|
24
|
+
host, port, db = srv.split(':')
|
25
|
+
server = ::Redis.new(:host => host, :port => port,
|
26
|
+
:thread_safe => true, :db => db)
|
27
|
+
end
|
28
|
+
namespace ||= :delayed
|
29
|
+
|
30
|
+
@server = ::Redis::Namespace.new(namespace, :redis => redis)
|
31
|
+
when ::Redis::Namespace
|
32
|
+
@server = srv
|
33
|
+
else
|
34
|
+
@server = ::Redis::Namespace.new(:delayed, :redis => srv)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def server
|
39
|
+
return @server if @server
|
40
|
+
self.server = ::Redis.respond_to?(:connect) ? ::Redis.connect : "localhost:6379"
|
41
|
+
self.server
|
42
|
+
end
|
43
|
+
extend self
|
44
|
+
end
|
45
|
+
|
46
|
+
def has_queued_attached_file(field, options = {})
|
47
|
+
|
48
|
+
|
49
|
+
# Include Paperclip and Paperclip::Glue for compatibility
|
50
|
+
unless self.ancestors.include?(::Paperclip)
|
51
|
+
include ::Paperclip
|
52
|
+
include ::Paperclip::Glue
|
53
|
+
end
|
54
|
+
|
55
|
+
include InstanceMethods
|
56
|
+
|
57
|
+
|
58
|
+
# Invoke Paperclip's #has_attached_file method and passes in the
|
59
|
+
# arguments specified by the user that invoked Mongoid::Paperclip#has_mongoid_attached_file
|
60
|
+
if options[:logger].nil? && Mongoid::Config.logger.present?
|
61
|
+
options[:logger] = Mongoid::Config.logger
|
62
|
+
end
|
63
|
+
has_attached_file(field, options)
|
64
|
+
|
65
|
+
# halt processing initially, but allow override for reprocess!
|
66
|
+
self.send :"before_#{field}_post_process", :halt_processing
|
67
|
+
|
68
|
+
self.send :after_save do
|
69
|
+
# add a Redis key for the application to check if we're still processing
|
70
|
+
# we don't need it for the processing, it's just a helpful tool
|
71
|
+
Mongoid::PaperclipQueue::Redis.server.sadd(self.class.name, "#{field}:#{self.id}")
|
72
|
+
|
73
|
+
# then queue up our processing
|
74
|
+
Mongoid::PaperclipQueue::Queue.enqueue(self.class.name, field, self.id)
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# Define the necessary collection fields in Mongoid for Paperclip
|
79
|
+
field(:"#{field}_file_name", :type => String)
|
80
|
+
field(:"#{field}_content_type", :type => String)
|
81
|
+
field(:"#{field}_file_size", :type => Integer)
|
82
|
+
field(:"#{field}_updated_at", :type => DateTime)
|
83
|
+
end
|
84
|
+
|
85
|
+
module InstanceMethods
|
86
|
+
|
87
|
+
def halt_processing
|
88
|
+
@is_processing || false
|
89
|
+
end
|
90
|
+
|
91
|
+
def do_reprocessing_on(field)
|
92
|
+
@is_processing=true
|
93
|
+
self.send(field.to_sym).reprocess!
|
94
|
+
Mongoid::PaperclipQueue::Redis.server.srem(self.class.name, "#{field}:#{self.id}")
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
module Paperclip
|
100
|
+
class Attachment
|
101
|
+
def processing?
|
102
|
+
@instance.new_record? || Mongoid::PaperclipQueue::Redis.server.sismember(@instance.class.name, "#{@name}:#{@instance.id}")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{mongoid_paperclip_queue}
|
3
|
+
s.version = "0.0.3"
|
4
|
+
|
5
|
+
s.authors = ["Kelly Martin"]
|
6
|
+
s.summary = %q{Process your Paperclip attachments in the background using Mongoid and Resque.}
|
7
|
+
s.description = %q{Process your Paperclip attachments in the background using Mongoid and Resque. Loosely based on delayed_paperclip and mongoid-paperclip.}
|
8
|
+
s.email = %q{kelly@fullybrand.com}
|
9
|
+
s.homepage = %q{http://github.com/kellym/mongoid_paperclip_queue}
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
13
|
+
|
14
|
+
s.add_runtime_dependency 'paperclip', ["~> 2.3.0"]
|
15
|
+
s.add_runtime_dependency 'redis-namespace'
|
16
|
+
s.add_runtime_dependency 'mongoid'
|
17
|
+
s.add_runtime_dependency 'resque'
|
18
|
+
|
19
|
+
end
|
20
|
+
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'mongoid_paperclip_queue'
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_paperclip_queue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kelly Martin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-23 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: paperclip
|
16
|
+
requirement: &70191682934820 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.3.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70191682934820
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: redis-namespace
|
27
|
+
requirement: &70191682934400 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70191682934400
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: mongoid
|
38
|
+
requirement: &70191682933940 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70191682933940
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: resque
|
49
|
+
requirement: &70191682933520 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70191682933520
|
58
|
+
description: Process your Paperclip attachments in the background using Mongoid and
|
59
|
+
Resque. Loosely based on delayed_paperclip and mongoid-paperclip.
|
60
|
+
email: kelly@fullybrand.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- .project
|
67
|
+
- .rvmrc
|
68
|
+
- CONTRIBUTING
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE
|
71
|
+
- README.textile
|
72
|
+
- lib/mongoid_paperclip_queue.rb
|
73
|
+
- mongoid_paperclip_queue.gemspec
|
74
|
+
- rails/init.rb
|
75
|
+
homepage: http://github.com/kellym/mongoid_paperclip_queue
|
76
|
+
licenses: []
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.8
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Process your Paperclip attachments in the background using Mongoid and Resque.
|
99
|
+
test_files: []
|