activestorage-delayed 0.1.2 → 0.2.0.pre.pre

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
  SHA256:
3
- metadata.gz: 153ae98174a2430ab5b0ddfb3a2bb17ffc6b0d568306ba9b2ee55886d4b16581
4
- data.tar.gz: a75a0db4f5a200a84a627bdf74771ee63d39260b2df769a94ecc1a93a8f75546
3
+ metadata.gz: 02b3779f97c4d12be774399968c3ef1b3e0c006bf4c39da9ecf5fd583bdeadf4
4
+ data.tar.gz: c9e4338a68b08f564d2b6e8c0c6f608d21dc70feea8688afbe22f1566b88a7ca
5
5
  SHA512:
6
- metadata.gz: 76a311ff87cc7ecf0046443834e1940563d2ed69bafc50f8d5cabbd0d7643c72f1827fb8ab89a86adf60f3e20856192d3412d9cfc8704aed3e8204f8b5379a21
7
- data.tar.gz: 87dc98a976a1c8cbcc0a64bbda6acd3d04e3bbd42d388640ad168627e1a89e1719a17ba07735292a715ed733000e8f11b08570a5415e9bd3ca24560f311e6342
6
+ metadata.gz: 88ebc7439257498bd71450e9958856102fb0a948bc1c3b3d0b26fabc43aae6094599187f3336dac6a125f8c3be320a5648f66c57cc9aa299d2ce540dcf4ec77e
7
+ data.tar.gz: cea2b0e318299d762121f285a270e72d11457b742537b6515fb95232de48222c52db8ea7d2af0b5281402d22b905de6fc869333de03daba393519214c88c3743
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activestorage-delayed (0.1.2)
4
+ activestorage-delayed (0.2.0.pre.pre)
5
5
  activestorage
6
6
  rails
7
7
 
data/README.md CHANGED
@@ -13,7 +13,7 @@ Note: This gem assumes that the app has already configured activestorage.
13
13
  ## Installation
14
14
  - Add this line to your application's Gemfile:
15
15
  ```ruby
16
- gem 'activestorage-delayed', '>= 0.1.1'
16
+ gem 'activestorage-delayed', '>= 0.1.3'
17
17
  ```
18
18
  - And then execute: `bundle install`
19
19
  - Generate the migration: `rails g migration add_activestorage_delayed`
@@ -39,7 +39,7 @@ Note: This gem assumes that the app has already configured activestorage.
39
39
  class User < ApplicationRecord
40
40
  include ActivestorageDelayed::DelayedConcern
41
41
 
42
- has_one_attached :photo, require: true, use_filename: true
42
+ has_one_attached :photo, required: true, use_filename: true
43
43
  delayed_attach :photo
44
44
 
45
45
  has_many_attached :certificates
@@ -48,7 +48,7 @@ end
48
48
 
49
49
  ```
50
50
  ### `delayed_attach` accepts an optional hash with the following options:
51
- - `require`: If set to `true`, the `photo` or the `photo_tmp` will be required before saving.
51
+ - `required`: If set to `true`, the `photo` or the `photo_tmp` will be required before saving.
52
52
  - `use_filename`: If set to `true`, the image filename will be used as the name of uploaded file instead of the hash-key used by `activestorage`
53
53
 
54
54
  ### Examples to upload files in background
@@ -3,10 +3,14 @@
3
3
  module ActivestorageDelayed
4
4
  module DelayedConcern
5
5
  extend ActiveSupport::Concern
6
- included do
6
+ included do # rubocop:disable Metrics/BlockLength
7
7
  @ast_delayed_settings = {}
8
- def self.delayed_attach(attr_name, required: false, use_filename: false) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
9
- @ast_delayed_settings[attr_name] = { use_filename: use_filename }
8
+
9
+ # @param settings (Hash)
10
+ # use_filename: (Boolean)
11
+ # variant_info: (Hash) Sample: { resize_to_fit: [400, 400], convert: 'jpg' }
12
+ def self.delayed_attach(attr_name, required: false, **settings) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
13
+ @ast_delayed_settings[attr_name] = settings
10
14
  tmp_attr_name = :"#{attr_name}_tmp"
11
15
  has_many_attr = :"#{attr_name}_delayed_uploads"
12
16
  attr_accessor tmp_attr_name
@@ -27,11 +31,22 @@ module ActivestorageDelayed
27
31
  delayed_data[:attr_name] = attr_name
28
32
  send(has_many_attr) << send(has_many_attr).new(delayed_data)
29
33
  end
34
+
35
+ # @param filename (String) File name
36
+ define_method "#{attr_name}_filename" do |filename|
37
+ name = File.basename(filename, '.*').parameterize
38
+ is_multiple = send(attr_name).class.name.include?('Many')
39
+ name = "#{SecureRandom.uuid}-#{name}" if is_multiple
40
+ "#{send(:id)}-#{name}#{File.extname(filename)}"
41
+ end
42
+
43
+ define_method "#{attr_name}_after_upload" do
44
+ end
45
+
46
+ # @param _error (Exception)
47
+ define_method "#{attr_name}_error_upload" do |_error|
48
+ end
30
49
  end
31
50
  end
32
-
33
- # @param _attr_name (String)
34
- # @param _error (Exception)
35
- def ast_delayed_on_error(_attr_name, _error); end
36
51
  end
37
52
  end
@@ -24,10 +24,11 @@ module ActivestorageDelayed
24
24
  tmp_files_data.each do |file_data|
25
25
  model.send(attr_name).attach(file_data.transform_keys(&:to_sym))
26
26
  end
27
+ model.send("#{attr_name}_after_upload")
27
28
  true
28
29
  rescue => e # rubocop:disable Style/RescueStandardError
29
30
  Rails.logger.error("********* #{self.class.name} -> Failed uploading files: #{e.message}. #{e.backtrace[0..20]}")
30
- model.ast_delayed_on_error(attr_name, e)
31
+ model.send("#{attr_name}_error_upload", e)
31
32
  false
32
33
  end
33
34
 
@@ -51,11 +52,8 @@ module ActivestorageDelayed
51
52
  end
52
53
 
53
54
  def base64_to_file(file_data)
54
- tempfile = Tempfile.new(file_data['filename'])
55
- tempfile.binmode
56
- tempfile.write Base64.decode64(file_data['io'])
57
- tempfile.rewind
58
- tempfile
55
+ io = StringIO.new(Base64.decode64(file_data['io']))
56
+ apply_variant(io, attr_settings[:variant_info]) { |io2| return io2 }
59
57
  end
60
58
 
61
59
  def model
@@ -64,11 +62,7 @@ module ActivestorageDelayed
64
62
 
65
63
  def filename_for(filename)
66
64
  method_name = "#{attr_name}_filename".to_sym
67
- return model.send(method_name, filename) if model.respond_to?(method_name)
68
-
69
- name = File.basename(filename, '.*').parameterize
70
- name = "#{SecureRandom.uuid}-#{name}" if support_multiple?
71
- "#{model.id}-#{name}#{File.extname(filename)}"
65
+ model.send(method_name, filename)
72
66
  end
73
67
 
74
68
  def remove_files
@@ -90,5 +84,13 @@ module ActivestorageDelayed
90
84
  def attr_settings
91
85
  model.class.instance_variable_get(:@ast_delayed_settings)[attr_name]
92
86
  end
87
+
88
+ # @param io [StringIO, File]
89
+ # @param variant_info [Hash, Nil] ActiveStorage variant info. Sample: { resize_to_fit: [400, 400], convert: 'jpg' }
90
+ def apply_variant(io, variant_info, &block)
91
+ return block.call(io) unless variant_info
92
+
93
+ ActiveStorage::Variation.wrap(variant_info).transform(io, &block)
94
+ end
93
95
  end
94
96
  end
@@ -10,7 +10,7 @@ module ActivestorageDelayed
10
10
  self.table_name = 'activestorage_delayed_uploads'
11
11
  attr_accessor :tmp_files
12
12
 
13
- belongs_to :uploadable, polymorphic: true
13
+ belongs_to :uploadable, polymorphic: true, touch: true
14
14
 
15
15
  before_save :parse_tmp_files
16
16
  after_create_commit do
@@ -4,9 +4,5 @@ require 'rails'
4
4
  module ActivestorageDelayed
5
5
  class Railtie < ::Rails::Railtie
6
6
  railtie_name :activestorage_delayed
7
-
8
- config.after_initialize do |_app|
9
- require_relative '../../initializers/upload_default_variation'
10
- end
11
7
  end
12
8
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActivestorageDelayed
4
- VERSION = '0.1.2'
4
+ VERSION = '0.2.0-pre'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activestorage-delayed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0.pre.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Owen Peredo Diaz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-27 00:00:00.000000000 Z
11
+ date: 2022-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activestorage
@@ -64,7 +64,6 @@ files:
64
64
  - bin/test
65
65
  - db/test.sqlite3
66
66
  - docker-compose.yaml
67
- - initializers/upload_default_variation.rb
68
67
  - lib/activestorage-delayed.rb
69
68
  - lib/activestorage-delayed/delayed_concern.rb
70
69
  - lib/activestorage-delayed/delayed_uploader.rb
@@ -89,9 +88,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
88
  version: '0'
90
89
  required_rubygems_version: !ruby/object:Gem::Requirement
91
90
  requirements:
92
- - - ">="
91
+ - - ">"
93
92
  - !ruby/object:Gem::Version
94
- version: '0'
93
+ version: 1.3.1
95
94
  requirements: []
96
95
  rubygems_version: 3.1.2
97
96
  signing_key:
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Ability to auto apply :default variant before uploading original image
4
- module ActivetoragePreprocessDefaultVariation
5
- def self.prepended(base)
6
- base.extend(ClassMethods)
7
- end
8
-
9
- def upload_without_unfurling(io)
10
- variant = attachments.first.try(:send, :variants)
11
- default_variant = variant ? variant[:default] : nil
12
- if default_variant && self.class.enabled_default_variant?
13
- ActiveStorage::Variation.wrap(default_variant).transform(io) do |output|
14
- unfurl output, identify: identify
15
- super(output)
16
- end
17
- else
18
- super(io)
19
- end
20
- end
21
-
22
- module ClassMethods
23
- # To improve testing performance, we don't want to preprocess images in test environment
24
- def enabled_default_variant?
25
- !Rails.env.test?
26
- end
27
- end
28
- end
29
-
30
- ActiveStorage::Blob.prepend ActivetoragePreprocessDefaultVariation