has_attachable 0.0.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 97e43af2f9f77bf6a8969996b4ca3d1981c86d33
4
- data.tar.gz: 1e2eda1b3e2679a86d93ae0ec8faa94b3456ff98
3
+ metadata.gz: ea0572bb3ba8eaf54004181bd351c5fc2d48da99
4
+ data.tar.gz: 1161f64906a00b44ebecefd45d8852caeffc46f8
5
5
  SHA512:
6
- metadata.gz: dd9d3d43243fed145d0155ff2eba3b6d875e3631245f0d3c96becf50fbc41e2b53f866d554f8e0f7dcbaddb3133407fad62cb96ea7c4afa94c8ec5784821722f
7
- data.tar.gz: 491227dec20ea95eaa48e9be20ac9090e5553327361f0d4672dead7b3c975cb563a5e22cf037f53d30d9df7dcabdcb1f6a4fbadf31c864e02113e923955db9cc
6
+ metadata.gz: 08a2b3ce42eb0141939ea82866bedd400f88afc4afce426067d8072d79a4515fd53c2817b8517d636adb456b6b433152a3666cea8737b288d2af1f226e43a047
7
+ data.tar.gz: 5b5130f7dba9efba4e799b0c0c711014b844a117527208a3fddb6f6fc9a076b8d18ddfc6f7896816d1ac291f9f232c08ebd4e442c497c878342b6625dba0f008
@@ -20,4 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency 'sidekiq'
25
+ spec.add_dependency 'sidekiq-status'
26
+ spec.add_dependency 'carrierwave'
27
+ spec.add_dependency 'rails', '~> 4.0.0'
23
28
  end
@@ -1,5 +1,6 @@
1
1
  require "has_attachable/version"
2
2
  require "has_attachable/processing"
3
+ require "has_attachable/worker"
3
4
 
4
5
  module HasAttachable
5
6
  extend ActiveSupport::Concern
@@ -11,15 +12,10 @@ module HasAttachable
11
12
  end
12
13
 
13
14
  included do
14
- after_update -> {
15
- AttachableWorker.
16
- perform_async('process',
17
- processing_options) if process_attachable?
18
- AttachableWorker.
19
- perform_async('remove',
20
- processing_options) if remove_attachable?
21
- }
22
- include Attachable::Processing
15
+ after_update :run_process_attachable, if: :process_attachable?
16
+ after_update :run_remove_attachable, if: :remove_attachable?
17
+
18
+ include HasAttachable::Processing
23
19
  end
24
20
 
25
21
  module ClassMethods
@@ -46,9 +42,16 @@ module HasAttachable
46
42
 
47
43
 
48
44
  class_eval do
49
- attr_accessible "async_remove_#{field}".to_sym, "#{field}_name".to_sym
50
45
  attr_accessor "async_remove_#{field}".to_sym
51
46
 
47
+ define_method("#{field}_processing?") {
48
+ if self.send("#{field}_job_id").present?
49
+ not Sidekiq::Status::complete? self.send("#{field}_job_id")
50
+ else
51
+ false
52
+ end
53
+ }
54
+
52
55
  mount_uploader field,
53
56
  attachable_options[field][:uploader],
54
57
  mount_on: "#{field}_name"
@@ -2,11 +2,6 @@ module HasAttachable
2
2
  module Processing
3
3
  extend ActiveSupport::Concern
4
4
 
5
- def attachable_job
6
- JobStatus.new(AttachableWorker.sidekiq_options["queue"].to_s,
7
- self.class.name.downcase, self.id)
8
- end
9
-
10
5
  def process_attachable?
11
6
  field = get_changed_attachable
12
7
  return false if field.nil?
@@ -19,6 +14,30 @@ module HasAttachable
19
14
  end
20
15
  end
21
16
 
17
+ def run_process_attachable
18
+ job_id = HasAttachable::Worker.perform_async('process', processing_options)
19
+ track_job_id(job_id) if self.respond_to?("#{attachable_field}_job_id")
20
+ end
21
+
22
+ def run_remove_attachable
23
+ self.update_attribute("#{get_remove_attachable}_name", nil)
24
+ job_id = HasAttachable::Worker.perform_async('remove', processing_options)
25
+ track_job_id(job_id) if self.respond_to?("#{attachable_field}_job_id")
26
+ end
27
+
28
+ def track_job_id(job_id)
29
+ self.update_column "#{attachable_field}_job_id", job_id
30
+ end
31
+
32
+ def untrack_job_id(context)
33
+ self.update_column "#{context}_job_id", nil
34
+ end
35
+
36
+ def attachable_field
37
+ get_changed_attachable || get_remove_attachable
38
+ end
39
+
40
+
22
41
  def remove_attachable?
23
42
  field = get_remove_attachable
24
43
  return false if field.nil?
@@ -43,9 +62,6 @@ module HasAttachable
43
62
  remove_attachable
44
63
  end
45
64
 
46
- def attachable_status_path
47
- "/monitor/#{AttachableWorker.sidekiq_options['queue'].to_s}/#{self.class.name.downcase}/#{self.id}"
48
- end
49
65
 
50
66
  def processing_options
51
67
  {
@@ -1,3 +1,3 @@
1
1
  module HasAttachable
2
- VERSION = "0.0.1"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -0,0 +1,29 @@
1
+ require 'sidekiq'
2
+ require 'sidekiq-status'
3
+
4
+ module HasAttachable
5
+ class Worker
6
+ include Sidekiq::Worker
7
+ include Sidekiq::Status::Worker
8
+
9
+ sidekiq_options queue: :has_attachable
10
+
11
+ def perform(method, options)
12
+ send("#{method}", Hash[options.map{ |k, v| [k.to_sym, v] }])
13
+ end
14
+
15
+ def process(options)
16
+ object = options[:klass].classify.constantize.unscoped.where(id: options[:id]).first
17
+ object.send(options[:context]).cache_stored_file!
18
+ object.send(options[:context]).retrieve_from_cache!(object.send(options[:context]).cache_name)
19
+ object.send(options[:context]).recreate_versions!
20
+ object.untrack_job_id(options[:context])
21
+ end
22
+
23
+ def remove(options)
24
+ object = options[:klass].classify.constantize.unscoped.where(id: options[:id]).first
25
+ object.send("remove_#{options[:context]}!")
26
+ object.untrack_job_id(options[:context])
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_attachable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zack Siri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-14 00:00:00.000000000 Z
11
+ date: 2013-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,62 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sidekiq-status
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: carrierwave
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 4.0.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 4.0.0
41
97
  description: gem for managing async uploading to S3 and background processing with
42
98
  sidekiq
43
99
  email:
@@ -56,6 +112,7 @@ files:
56
112
  - lib/has_attachable.rb
57
113
  - lib/has_attachable/processing.rb
58
114
  - lib/has_attachable/version.rb
115
+ - lib/has_attachable/worker.rb
59
116
  homepage: ''
60
117
  licenses:
61
118
  - MIT