activestorage-delayed 0.1.4 → 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: 76f19f1856294d83f52a92dbbc1ea9eb52c216c021726437ff513727426c65e4
4
- data.tar.gz: 923f537ec4dbbfd797c72157f46b58a1a1f20f04127f53034f444fd6a2cd8ac2
3
+ metadata.gz: 02b3779f97c4d12be774399968c3ef1b3e0c006bf4c39da9ecf5fd583bdeadf4
4
+ data.tar.gz: c9e4338a68b08f564d2b6e8c0c6f608d21dc70feea8688afbe22f1566b88a7ca
5
5
  SHA512:
6
- metadata.gz: ea37e642bef6ef9ba231b0bc0b2c8c1676b569679be5373bd45cd5f6cbd7f8aff4d1ac3642e1f6853fddf8e46f55f19293a38cf9fee4a60061b671c5cb27a3b0
7
- data.tar.gz: 80d2123dec2953c7c8778e0f7fa9449e73b7f25625863e05f0edaf5c07e3c6782c3e4b44f824c14ab61bb3dc03a834ec94c6eebc340b298fb7061d11f02208fe
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.3)
4
+ activestorage-delayed (0.2.0.pre.pre)
5
5
  activestorage
6
6
  rails
7
7
 
@@ -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
@@ -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.4'
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.4
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-05-04 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,31 +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) # rubocop:disable Metrics/MethodLength
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
- save! if id.present? # update new unfurl information
16
- super(output)
17
- end
18
- else
19
- super(io)
20
- end
21
- end
22
-
23
- module ClassMethods
24
- # To improve testing performance, we don't want to preprocess images in test environment
25
- def enabled_default_variant?
26
- !Rails.env.test?
27
- end
28
- end
29
- end
30
-
31
- ActiveStorage::Blob.prepend ActivetoragePreprocessDefaultVariation