refile-backgrounder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d319c8d55dbe2cc6f9bdd23351115e825a1625c2
4
+ data.tar.gz: c0eea32d332c6cb0c942845bfba6f1ee572d3965
5
+ SHA512:
6
+ metadata.gz: 52bff925e34468964a5450db7348db940c48fbd09ffc6526751d7dbe2f43042ba7ba5cc6ce0f600e6ea6dae0d074d19bc745ffec4759c9c762e257d74e9b8faa
7
+ data.tar.gz: 494b211ae5b48eccc542b9020d863a5c6ed1ac21aa0b321907276e579c3414660ec4a8037d0c4ef04ef540956b56d343eb557a7ef1cd373944e5e69e15de034c
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in refile-backgrounder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Logan Serman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Refile::Backgrounder
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'refile-backgrounder'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install refile-backgrounder
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/refile-backgrounder/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,20 @@
1
+ module Refile
2
+ module Backgrounder
3
+ module ActiveRecord
4
+
5
+ def attachment(name, background: false, worker: Refile::Backgrounder::StoreWorker, **options)
6
+ super name, **options
7
+ return unless background
8
+
9
+ attr_accessor :_skip_refile_backgrounder
10
+ after_save(unless: :_skip_refile_backgrounder) do |record|
11
+ worker.perform_later record, name.to_s
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+
19
+ require 'refile/attachment/active_record'
20
+ ::ActiveRecord::Base.extend Refile::Backgrounder::ActiveRecord
@@ -0,0 +1,27 @@
1
+ module Refile
2
+ module Backgrounder
3
+ module Attacher
4
+
5
+ def background?
6
+ record.respond_to? "#{name}_cache_id"
7
+ end
8
+
9
+ def cache_id
10
+ super || (background? and record.send("#{name}_cache_id"))
11
+ end
12
+
13
+ def cache!(*)
14
+ super
15
+ record.send "#{name}_cache_id=", cache_id if background?
16
+ end
17
+
18
+ def store!
19
+ super unless background?
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+
26
+ require 'refile/attacher'
27
+ ::Refile::Attacher.send :prepend, Refile::Backgrounder::Attacher
@@ -0,0 +1,46 @@
1
+ module Refile
2
+ module Backgrounder
3
+ class StoreWorker < ::ActiveJob::Base
4
+ attr_reader :record, :attachment_name
5
+ queue_as :refile
6
+
7
+ def perform(record, attachment_name)
8
+ @record = record
9
+ @attachment_name = attachment_name
10
+ return unless attachment_cached?
11
+
12
+ file = attacher.get
13
+ upload file
14
+ cleanup_cache!
15
+ update_record_attachment(file) if file
16
+ yield if block_given?
17
+ end
18
+
19
+ private
20
+
21
+ def attacher
22
+ @_attacher ||= record.send("#{attachment_name}_attacher")
23
+ end
24
+
25
+ def attachment_cached?
26
+ attacher.cache_id
27
+ end
28
+
29
+ def upload(file)
30
+ attacher.store.upload(file)
31
+ end
32
+
33
+ def cleanup_cache!
34
+ attacher.delete!
35
+ end
36
+
37
+ def update_record_attachment(file)
38
+ record._skip_refile_backgrounder = true
39
+ record.send "#{attachment_name}_cache_id=", nil if respond_to? "#{attachment_name}_cache_id="
40
+ record.send "#{attachment_name}_id=", nil if respond_to? "#{attachment_name}_id="
41
+ record.save
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module Refile
2
+ module Backgrounder
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'refile/rails'
2
+ require 'rails'
3
+
4
+ require 'refile/backgrounder/version'
5
+ require 'refile/backgrounder/store_worker'
6
+ require 'refile/backgrounder/attacher'
7
+ require 'refile/backgrounder/activerecord'
8
+
9
+ module Refile
10
+ module Backgrounder
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'refile/backgrounder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'refile-backgrounder'
8
+ spec.version = Refile::Backgrounder::VERSION
9
+ spec.authors = ['Logan Serman']
10
+ spec.email = ['loganserman@gmail.com']
11
+ spec.summary = 'Asyncronous cache-to-store uploads with Refile.'
12
+ spec.description = spec.description
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.required_ruby_version = '>= 2.1.0'
22
+
23
+ spec.add_dependency 'rails', '>= 4.1.0'
24
+ spec.add_dependency 'refile', '>= 0.5.0'
25
+ spec.add_dependency 'activejob', '>= 4.2.0'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.7'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: refile-backgrounder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Logan Serman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: refile
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activejob
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 4.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 4.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: ''
84
+ email:
85
+ - loganserman@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/refile-backgrounder.rb
96
+ - lib/refile/backgrounder/activerecord.rb
97
+ - lib/refile/backgrounder/attacher.rb
98
+ - lib/refile/backgrounder/store_worker.rb
99
+ - lib/refile/backgrounder/version.rb
100
+ - refile-backgrounder.gemspec
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 2.1.0
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Asyncronous cache-to-store uploads with Refile.
125
+ test_files: []
126
+ has_rdoc: