carrierwave_backgrounder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in carrierwave_backgrounder.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,69 @@
1
+ = CarrierWave Backgrounder
2
+
3
+ I like CarrierWave. That being said, I don't like tying up app instances waiting for images to process and upload to S3. Depending on the size of the file, this could take a while sometimes timing out Heroku. This gem addresses that. After the file has been uploaded and CarrierWave caches the base file, it halts the process and queues a job.
4
+
5
+ == Installation
6
+
7
+ These instructions assume you have previously set up {CarrierWave}[https://github.com/jnicklas/carrierwave] and {DelayedJob}[https://github.com/collectiveidea/delayed_job]
8
+
9
+ In Rails, add the following your Gemfile:
10
+
11
+ gem 'carrierwave_backgrounder'
12
+
13
+ == Getting Started
14
+
15
+ Add a column to the model you wan to background which will store the temp file location:
16
+
17
+ add_column :users, :avatar_tmp, :string
18
+
19
+ In your CarrierWave uploader file:
20
+
21
+ class AvatarUploader < CarrierWave::Uploader::Base
22
+ include ::CarrierWave::Backgrounder::DelayStorage
23
+
24
+ #ect...
25
+ end
26
+
27
+ In your model:
28
+
29
+ mount_uploader :avatar, AvatarUploader
30
+ store_in_background :avatar
31
+
32
+ Currently ActiveRecord is the default orm and I have not tested this with others but it should work by adding the following to your carrierwave initializer:
33
+
34
+ DataMapper::Model.send(:include, ::CarrierWave::Backgrounder::ORM)
35
+ # or
36
+ Mongoid::Document::ClassMethods.send(:include, ::CarrierWave::Backgrounder::ORM)
37
+ # or
38
+ Sequel::Model.send(:extend, ::CarrierWave::Backgrounder::ORM)
39
+
40
+ == Word Of Caution
41
+
42
+ Temp files are stored by default in the tmp directory. They are not guaranteed to be available when your workers process them! For instance, on Heroku, they get blown away on every deploy. If image upload is the main function of your app, I would store the tmp files in a non-volatile directory if you have the option.
43
+
44
+ == TODO
45
+
46
+ Transfer tests from rails app.
47
+
48
+ == License
49
+
50
+ Copyright (c) 2011 Larry Sprock
51
+
52
+ Permission is hereby granted, free of charge, to any person obtaining
53
+ a copy of this software and associated documentation files (the
54
+ "Software"), to deal in the Software without restriction, including
55
+ without limitation the rights to use, copy, modify, merge, publish,
56
+ distribute, sublicense, and/or sell copies of the Software, and to
57
+ permit persons to whom the Software is furnished to do so, subject to
58
+ the following conditions:
59
+
60
+ The above copyright notice and this permission notice shall be
61
+ included in all copies or substantial portions of the Software.
62
+
63
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
64
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
65
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
66
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
67
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
68
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
69
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "backgrounder/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "carrierwave_backgrounder"
7
+ s.version = CarrierwaveBackgrounder::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Larry Sprock"]
10
+ s.email = ["larry@lucidbleu.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Offload CarrierWave's image processing and storage to a background process using Delayed Job}
13
+
14
+ s.rubyforge_project = "carrierwave_backgrounder"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,15 @@
1
+ module CarrierWave
2
+ module Backgrounder
3
+
4
+ module DelayStorage
5
+ def cache_versions!(new_file)
6
+ super(new_file) if proceed_with_versioning?
7
+ end
8
+
9
+ def proceed_with_versioning?
10
+ !model.respond_to?(:process_upload) || model.process_upload
11
+ end
12
+ end # DelayStorage
13
+
14
+ end # Backgrounder
15
+ end # CarrierWave
@@ -0,0 +1,38 @@
1
+ require 'backgrounder/workers/store_asset'
2
+
3
+ module CarrierWave
4
+ module Backgrounder
5
+
6
+ module ORM
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+
11
+ def store_in_background(column, version=false)
12
+ send :after_save, :"enqueue_#{column}_storage"
13
+
14
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
15
+ attr_accessor :process_upload
16
+
17
+ def write_#{column}_identifier
18
+ super() and return if process_upload
19
+ self.#{column}_tmp = _mounter(:#{column}).cache_name
20
+ end
21
+
22
+ def store_#{column}!
23
+ super() if process_upload
24
+ end
25
+
26
+ def enqueue_#{column}_storage
27
+ if !process_upload && #{column}_tmp
28
+ ::Delayed::Job.enqueue ::CarrierWave::Workers::StoreAsset.new(self.class, id, #{column}.mounted_as)
29
+ end
30
+ end
31
+ RUBY
32
+ end
33
+
34
+ end # ClassMethods
35
+ end # ActiveRecord
36
+
37
+ end #Backgrounder
38
+ end #CarrierWave
@@ -0,0 +1,3 @@
1
+ module CarrierwaveBackgrounder
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ module CarrierWave
2
+ module Workers
3
+
4
+ class StoreAsset < Struct.new(:klass, :id, :column)
5
+
6
+ def perform
7
+ record = klass.find id
8
+ if record.send :"#{column}_tmp"
9
+ cache_dir = record.send(:"#{column}").root.join(record.send(:"#{column}").cache_dir)
10
+ cache_path = cache_dir.join record.send(:"#{column}_tmp")
11
+
12
+ record.send :"process_upload=", true
13
+ record.send :"#{column}=", File.open(cache_path)
14
+ record.send :"#{column}_tmp=", nil
15
+ if record.save!
16
+ FileUtils.rm(cache_path)
17
+ end
18
+ end
19
+ end
20
+
21
+ end # StoreAsset
22
+
23
+ end # Workers
24
+ end # Backgrounder
@@ -0,0 +1,20 @@
1
+ module CarrierWave
2
+ module Backgrounder
3
+ autoload :DelayStorage, 'backgrounder/delay_storage'
4
+ autoload :ORM, 'backgrounder/orm'
5
+ end
6
+ end
7
+
8
+ if defined?(Rails)
9
+ module CarrierWave
10
+ module Backgrounder
11
+ class Railtie < Rails::Railtie
12
+ initializer "carrierwave_backgrounder.active_record" do
13
+ ActiveSupport.on_load :active_record do
14
+ ::ActiveRecord::Base.send :include, ::CarrierWave::Backgrounder::ORM
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
File without changes
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave_backgrounder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Larry Sprock
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-25 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ - larry@lucidbleu.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - Gemfile
29
+ - README.rdoc
30
+ - Rakefile
31
+ - carrierwave_backgrounder.gemspec
32
+ - lib/backgrounder/delay_storage.rb
33
+ - lib/backgrounder/orm.rb
34
+ - lib/backgrounder/version.rb
35
+ - lib/backgrounder/workers/store_asset.rb
36
+ - lib/carrierwave_backgrounder.rb
37
+ - spec/spec_helper.rb
38
+ has_rdoc: true
39
+ homepage: ""
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project: carrierwave_backgrounder
62
+ rubygems_version: 1.5.0
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Offload CarrierWave's image processing and storage to a background process using Delayed Job
66
+ test_files:
67
+ - spec/spec_helper.rb