carrierwave_backgrounder 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # CarrierWave Backgrounder
1
+ # CarrierWave Backgrounder [![Build Status](https://secure.travis-ci.org/lardawge/carrierwave_backgrounder.png)](http://travis-ci.org/lardawge/carrierwave_backgrounder)
2
2
 
3
3
  I like CarrierWave. That being said, I don't like tying up app instances waiting for images to process.
4
4
 
5
5
  This gem addresses that issue by disabling processing until a background process initiates it.
6
- It supports both Delayed Job and Resque.
6
+ It supports Delayed Job, Resque and Girl Friday.
7
7
 
8
8
  ## Background options
9
9
 
@@ -33,6 +33,16 @@ gem 'carrierwave_backgrounder'
33
33
 
34
34
  ## Getting Started
35
35
 
36
+ In your CarrierWave uploader file:
37
+
38
+ ```ruby
39
+ class AvatarUploader < CarrierWave::Uploader::Base
40
+ include ::CarrierWave::Backgrounder::Delay
41
+
42
+ #etc...
43
+ end
44
+ ```
45
+
36
46
  ### To use process_in_background
37
47
 
38
48
  In your model:
@@ -42,6 +52,12 @@ mount_uploader :avatar, AvatarUploader
42
52
  process_in_background :avatar
43
53
  ```
44
54
 
55
+ Optionally you can add a column to the database which will be set to nil when the background processing is complete.
56
+
57
+ ```ruby
58
+ add_column :users, :avatar_processing, :boolean
59
+ ```
60
+
45
61
  ### To use store_in_background
46
62
 
47
63
  In your model:
@@ -57,16 +73,6 @@ Add a column to the model you want to background which will store the temp file
57
73
  add_column :users, :avatar_tmp, :string
58
74
  ```
59
75
 
60
- In your CarrierWave uploader file:
61
-
62
- ```ruby
63
- class AvatarUploader < CarrierWave::Uploader::Base
64
- include ::CarrierWave::Backgrounder::DelayStorage
65
-
66
- #ect...
67
- end
68
- ```
69
-
70
76
  ## Usage Tips
71
77
 
72
78
  If you need to process/store the upload immediately:
@@ -75,6 +81,13 @@ If you need to process/store the upload immediately:
75
81
  @user.process_<column>_upload = true
76
82
  ```
77
83
 
84
+ To overide the worker in cases where additional methods need to be called or you have app specific requirements, pass the worker class as the
85
+ second argument:
86
+
87
+ ```ruby
88
+ process_in_background :avatar, MyAppsAwesomeProcessingWorker
89
+ ```
90
+
78
91
  ## ORM
79
92
 
80
93
  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:
@@ -22,4 +22,5 @@ Gem::Specification.new do |s|
22
22
 
23
23
  s.add_development_dependency "rspec", ["2.5.0"]
24
24
  s.add_development_dependency "mocha", ["~> 0.9"]
25
+ s.add_development_dependency "rake"
25
26
  end
@@ -2,13 +2,20 @@ module CarrierWave
2
2
  module Backgrounder
3
3
 
4
4
  module DelayStorage
5
+ def self.included(klass)
6
+ klass.send :include, Delay
7
+ Logger.warn "Carrierwave::Backgrounder::DelayStorage is deprecated, use Carrierwave::Backgrounder::Delay instead"
8
+ end
9
+ end
10
+
11
+ module Delay
5
12
 
6
13
  ##
7
- #Intercept carrierwave#cache_versions! so we can process versions later.
14
+ # Intercept carrierwave#cache_versions! so we can process versions later.
8
15
  def cache_versions!(new_file)
9
16
  super(new_file) if proceed_with_versioning?
10
17
  end
11
-
18
+
12
19
  def process!(new_file=nil)
13
20
  super(new_file) if proceed_with_versioning?
14
21
  end
@@ -18,7 +25,7 @@ module CarrierWave
18
25
  def proceed_with_versioning?
19
26
  !model.respond_to?(:"process_#{mounted_as}_upload") || model.send(:"process_#{mounted_as}_upload")
20
27
  end
21
- end # DelayStorage
28
+ end # Delay
22
29
 
23
30
  end # Backgrounder
24
31
  end # CarrierWave
@@ -0,0 +1,75 @@
1
+ module CarrierWave
2
+ module Backgrounder
3
+ module ORM
4
+
5
+ module DataMapper
6
+ include CarrierWave::Backgrounder::ORM::Base
7
+
8
+ def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
9
+ before :save, :"set_#{column}_processing"
10
+ after :save, :"enqueue_#{column}_background_job"
11
+
12
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
13
+ attr_accessor :process_#{column}_upload
14
+ attr_reader :#{column}_changed
15
+
16
+ def set_#{column}_processing
17
+ @#{column}_changed = attribute_dirty?(:#{column})
18
+ self.#{column}_processing = true if respond_to?(:#{column}_processing)
19
+ end
20
+
21
+ def enqueue_#{column}_background_job
22
+ if trigger_#{column}_background_processing?
23
+ ::Delayed::Job.enqueue #{worker}.new(self.class, id, #{column}.mounted_as)
24
+ @#{column}_changed = false
25
+ end
26
+ end
27
+
28
+ def trigger_#{column}_background_processing?
29
+ process_#{column}_upload != true && #{column}_changed
30
+ end
31
+
32
+ RUBY
33
+ end
34
+
35
+ def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
36
+ before :save, :"set_#{column}_changed"
37
+ after :save, :"enqueue_#{column}_background_job"
38
+
39
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
40
+ attr_accessor :process_#{column}_upload
41
+ attr_reader :#{column}_changed
42
+
43
+ def set_#{column}_changed
44
+ @#{column}_changed = attribute_dirty?(:#{column})
45
+ end
46
+
47
+ def write_#{column}_identifier
48
+ super() and return if process_#{column}_upload
49
+ self.#{column}_tmp = _mounter(:#{column}).cache_name
50
+ end
51
+
52
+ def store_#{column}!
53
+ super() if process_#{column}_upload
54
+ end
55
+
56
+ def enqueue_#{column}_background_job
57
+ if trigger_#{column}_background_storage?
58
+ ::Delayed::Job.enqueue #{worker}.new(self.class, id, #{column}.mounted_as)
59
+ @#{column}_changed = false
60
+ end
61
+ end
62
+
63
+ def trigger_#{column}_background_storage?
64
+ process_#{column}_upload != true && #{column}_changed
65
+ end
66
+
67
+ RUBY
68
+ end
69
+ end # DataMapper
70
+
71
+ end #ORM
72
+ end #Backgrounder
73
+ end #CarrierWave
74
+
75
+ DataMapper::Model.append_extensions ::CarrierWave::Backgrounder::ORM::DataMapper
@@ -0,0 +1,33 @@
1
+ module CarrierWave
2
+ module Backgrounder
3
+ module ORM
4
+
5
+ module Mongoid
6
+ include CarrierWave::Backgrounder::ORM::Base
7
+
8
+ def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
9
+ super
10
+
11
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
12
+ def trigger_#{column}_background_processing?
13
+ process_#{column}_upload != true && #{column}_changed?
14
+ end
15
+ RUBY
16
+ end
17
+
18
+ def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
19
+ super
20
+
21
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
22
+ def trigger_#{column}_background_storage?
23
+ process_#{column}_upload != true && #{column}_changed?
24
+ end
25
+ RUBY
26
+ end
27
+ end # ActiveRecord
28
+
29
+ end # ORM
30
+ end # Backgrounder
31
+ end # CarrierWave
32
+
33
+ Mongoid::Document::ClassMethods.send(:include, ::CarrierWave::Backgrounder::ORM::Mongoid)
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module Backgrounder
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
  end
5
5
  end
@@ -12,13 +12,16 @@ module CarrierWave
12
12
  def perform
13
13
  resource = klass.is_a?(String) ? klass.constantize : klass
14
14
  record = resource.find id
15
- record.send(:"process_#{column}_upload=", true)
16
- if record.send(:"#{column}").recreate_versions! && record.respond_to?(:"#{column}_processing")
17
- record.update_attribute :"#{column}_processing", nil
15
+
16
+ if record
17
+ record.send(:"process_#{column}_upload=", true)
18
+ if record.send(:"#{column}").recreate_versions! && record.respond_to?(:"#{column}_processing")
19
+ record.update_attribute :"#{column}_processing", nil
20
+ end
18
21
  end
19
22
  end
20
-
23
+
21
24
  end # ProcessAsset
22
-
25
+
23
26
  end # Workers
24
27
  end # Backgrounder
@@ -1,6 +1,9 @@
1
1
  module CarrierWave
2
2
  module Backgrounder
3
- autoload :DelayStorage, 'backgrounder/delay_storage'
3
+ Logger = Logger.new(STDOUT)
4
+
5
+ autoload :Delay, 'backgrounder/delay'
6
+ autoload :DelayStorage, 'backgrounder/delay'
4
7
 
5
8
  module ORM
6
9
  autoload :Base, 'backgrounder/orm/base'
@@ -12,11 +15,21 @@ if defined?(Rails)
12
15
  module CarrierWave
13
16
  module Backgrounder
14
17
  class Railtie < Rails::Railtie
18
+
15
19
  initializer "carrierwave_backgrounder.active_record" do
16
20
  ActiveSupport.on_load :active_record do
17
21
  require 'backgrounder/orm/activerecord'
18
22
  end
19
23
  end
24
+
25
+ initializer "carrierwave_backgrounder.data_mapper", :before =>"data_mapper.add_to_prepare" do
26
+ require 'backgrounder/orm/data_mapper' if defined?(DataMapper)
27
+ end
28
+
29
+ initializer "carrierwave_backgrounder.mongoid" do
30
+ require 'backgrounder/orm/mongoid' if defined?(Mongoid)
31
+ end
32
+
20
33
  end
21
34
  end
22
35
  end
metadata CHANGED
@@ -1,70 +1,98 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: carrierwave_backgrounder
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
4
5
  prerelease:
5
- version: 0.0.6
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Larry Sprock
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-12-07 00:00:00 -08:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: carrierwave
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
20
17
  none: false
21
- requirements:
18
+ requirements:
22
19
  - - ~>
23
- - !ruby/object:Gem::Version
24
- version: "0.5"
20
+ - !ruby/object:Gem::Version
21
+ version: '0.5'
25
22
  type: :runtime
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
23
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
31
25
  none: false
32
- requirements:
33
- - - "="
34
- - !ruby/object:Gem::Version
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
35
37
  version: 2.5.0
36
38
  type: :development
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.5.0
46
+ - !ruby/object:Gem::Dependency
39
47
  name: mocha
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.9'
54
+ type: :development
40
55
  prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
42
57
  none: false
43
- requirements:
58
+ requirements:
44
59
  - - ~>
45
- - !ruby/object:Gem::Version
46
- version: "0.9"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
47
70
  type: :development
48
- version_requirements: *id003
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
49
78
  description:
50
- email:
79
+ email:
51
80
  - larry@lucidbleu.com
52
81
  executables: []
53
-
54
82
  extensions: []
55
-
56
83
  extra_rdoc_files: []
57
-
58
- files:
84
+ files:
59
85
  - .gitignore
60
86
  - .rspec
61
87
  - Gemfile
62
88
  - README.md
63
89
  - Rakefile
64
90
  - carrierwave_backgrounder.gemspec
65
- - lib/backgrounder/delay_storage.rb
91
+ - lib/backgrounder/delay.rb
66
92
  - lib/backgrounder/orm/activerecord.rb
67
93
  - lib/backgrounder/orm/base.rb
94
+ - lib/backgrounder/orm/data_mapper.rb
95
+ - lib/backgrounder/orm/mongoid.rb
68
96
  - lib/backgrounder/version.rb
69
97
  - lib/backgrounder/workers.rb
70
98
  - lib/backgrounder/workers/process_asset.rb
@@ -74,35 +102,32 @@ files:
74
102
  - spec/workers/fixtures/test.jpg
75
103
  - spec/workers/process_asset_spec.rb
76
104
  - spec/workers/store_asset_spec.rb
77
- has_rdoc: true
78
- homepage: ""
105
+ homepage: ''
79
106
  licenses: []
80
-
81
107
  post_install_message:
82
108
  rdoc_options: []
83
-
84
- require_paths:
109
+ require_paths:
85
110
  - lib
86
- required_ruby_version: !ruby/object:Gem::Requirement
111
+ required_ruby_version: !ruby/object:Gem::Requirement
87
112
  none: false
88
- requirements:
89
- - - ">="
90
- - !ruby/object:Gem::Version
91
- version: "0"
92
- required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
118
  none: false
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- version: "0"
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
98
123
  requirements: []
99
-
100
124
  rubyforge_project: carrierwave_backgrounder
101
- rubygems_version: 1.5.0
125
+ rubygems_version: 1.8.21
102
126
  signing_key:
103
127
  specification_version: 3
104
- summary: Offload CarrierWave's image processing and storage to a background process using Delayed Job
105
- test_files:
128
+ summary: Offload CarrierWave's image processing and storage to a background process
129
+ using Delayed Job
130
+ test_files:
106
131
  - spec/spec_helper.rb
107
132
  - spec/workers/fixtures/test.jpg
108
133
  - spec/workers/process_asset_spec.rb