activestorage-delayed 0.2.0.pre.pre → 0.3.1

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: 02b3779f97c4d12be774399968c3ef1b3e0c006bf4c39da9ecf5fd583bdeadf4
4
- data.tar.gz: c9e4338a68b08f564d2b6e8c0c6f608d21dc70feea8688afbe22f1566b88a7ca
3
+ metadata.gz: e3a8aa804d991375a89011a3001a4329eb2510f151690ec92f01c1b18d1cf9a6
4
+ data.tar.gz: 51fb74d4c3449071aa9b94af9a61f2d17b619a5114d334cd878860cb9e2492f6
5
5
  SHA512:
6
- metadata.gz: 88ebc7439257498bd71450e9958856102fb0a948bc1c3b3d0b26fabc43aae6094599187f3336dac6a125f8c3be320a5648f66c57cc9aa299d2ce540dcf4ec77e
7
- data.tar.gz: cea2b0e318299d762121f285a270e72d11457b742537b6515fb95232de48222c52db8ea7d2af0b5281402d22b905de6fc869333de03daba393519214c88c3743
6
+ metadata.gz: 38a995e047e1b344d62325293abfd29f8d23ddd299adcd8235044aa5556b5fb3454d03351eb29b1834bd348bc57be607831b616d3efbbba1e72e0fc72a89d706
7
+ data.tar.gz: cf66e127b9123bf19c56899c67a5b3f04cd97fdc77b05f2175e1f2f2e46d94ca1334b3e552b937a64c9e180f598b2fd95717c4fe788506ee01db7434b10ce727
@@ -1,43 +1,11 @@
1
+ name: Create release from tag
1
2
  on:
2
3
  push:
3
- tags: # triggered once a git tag is published
4
+ tags: # Create a release once a git tag is published
4
5
  - '*'
5
6
 
6
- name: Create Release
7
-
8
7
  jobs:
9
- build:
10
- name: Create Release
11
- runs-on: ubuntu-latest
12
- steps:
13
- - name: Checkout code
14
- uses: actions/checkout@v2
15
- with:
16
- fetch-depth: 0
17
-
18
- # Changelog action adaptations
19
- - name: Create required package.json
20
- run: test -f package.json || echo '{}' >package.json
21
- - name: Detect Previous Tag (action not detecting very well)
22
- run: echo "::set-output name=previous_tag::$(git describe --abbrev=0 --tags `git rev-list --tags --skip=1 --max-count=1`)"
23
- id: tag_checker
24
-
25
- - name: Generate Changelog
26
- uses: scottbrenner/generate-changelog-action@master
27
- id: Changelog
28
- with:
29
- from-tag: ${{steps.tag_checker.outputs.previous_tag}}
30
- to-tag: HEAD
31
-
32
- - name: Create Release
33
- id: create_release
34
- uses: actions/create-release@latest
35
- env:
36
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
37
- with:
38
- tag_name: ${{ github.ref }}
39
- release_name: Release ${{ github.ref }}
40
- body: |
41
- ${{ steps.Changelog.outputs.changelog }}
42
- draft: false
43
- prerelease: false
8
+ release-builder:
9
+ uses: owen2345/reusable-ci-cd-actions/.github/workflows/release_builder_from_tag.yml@main
10
+ with:
11
+ commit_mode: true
@@ -1,31 +1,16 @@
1
1
  name: App tests
2
-
3
2
  on:
4
3
  push:
5
4
  branches:
6
5
  - main
7
- - staging
6
+ - master
8
7
  pull_request:
9
8
 
10
9
  jobs:
11
- build:
12
- runs-on: ubuntu-latest
13
-
14
- steps:
15
- - name: Checkout repository
16
- uses: actions/checkout@v2
17
- - name: Cancel previous Workflow Actions
18
- uses: styfle/cancel-workflow-action@0.6.0
19
- with:
20
- access_token: ${{ github.token }}
21
- - name: download docker-compose cache
22
- run: docker-compose pull
23
- - uses: satackey/action-docker-layer-caching@v0.0.11 # Cache or restore docker cache
24
- continue-on-error: true # Ignore the failure of a step and avoid terminating the job.
25
- with:
26
- key: app-cache-${{ hashFiles('Dockerfile') }}
27
-
28
- - name: Run rspec test
29
- run: docker-compose run test /bin/sh -c "bundle install && rspec"
30
- - name: Check codestyle
31
- run: docker-compose run test /bin/sh -c "rubocop"
10
+ app-tests:
11
+ uses: owen2345/reusable-ci-cd-actions/.github/workflows/tests.yml@main
12
+ with:
13
+ prepare_db_cmd: null
14
+ copy_env: false
15
+ run_rubocop: true
16
+ rspec_cmd: 'bundle install and bundle exec rspec'
@@ -10,40 +10,63 @@
10
10
  <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
11
  <excludeFolder url="file://$MODULE_DIR$/spec/dummy/tmp/cache" />
12
12
  </content>
13
- <orderEntry type="jdk" jdkName="Remote: ruby-2.5.9-p229" jdkType="RUBY_SDK" />
13
+ <orderEntry type="jdk" jdkName="Remote: ruby-3.1.1-p18" jdkType="RUBY_SDK" />
14
14
  <orderEntry type="sourceFolder" forTests="false" />
15
- <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.2, Remote: ruby-2.5.9-p229) [gem]" level="application" />
16
- <orderEntry type="library" scope="PROVIDED" name="builder (v3.2.4, Remote: ruby-2.5.9-p229) [gem]" level="application" />
17
- <orderEntry type="library" scope="PROVIDED" name="bundler (v2.3.10, Remote: ruby-2.5.9-p229) [gem]" level="application" />
18
- <orderEntry type="library" scope="PROVIDED" name="concurrent-ruby (v1.1.10, Remote: ruby-2.5.9-p229) [gem]" level="application" />
19
- <orderEntry type="library" scope="PROVIDED" name="crass (v1.0.6, Remote: ruby-2.5.9-p229) [gem]" level="application" />
20
- <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.5.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
21
- <orderEntry type="library" scope="PROVIDED" name="erubi (v1.10.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
22
- <orderEntry type="library" scope="PROVIDED" name="factory_bot (v6.2.1, Remote: ruby-2.5.9-p229) [gem]" level="application" />
23
- <orderEntry type="library" scope="PROVIDED" name="factory_bot_rails (v6.2.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
24
- <orderEntry type="library" scope="PROVIDED" name="globalid (v1.0.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
25
- <orderEntry type="library" scope="PROVIDED" name="i18n (v1.10.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
26
- <orderEntry type="library" scope="PROVIDED" name="mail (v2.7.1, Remote: ruby-2.5.9-p229) [gem]" level="application" />
27
- <orderEntry type="library" scope="PROVIDED" name="marcel (v1.0.2, Remote: ruby-2.5.9-p229) [gem]" level="application" />
28
- <orderEntry type="library" scope="PROVIDED" name="method_source (v1.0.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
29
- <orderEntry type="library" scope="PROVIDED" name="mini_mime (v1.1.2, Remote: ruby-2.5.9-p229) [gem]" level="application" />
30
- <orderEntry type="library" scope="PROVIDED" name="minitest (v5.15.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
31
- <orderEntry type="library" scope="PROVIDED" name="nio4r (v2.5.8, Remote: ruby-2.5.9-p229) [gem]" level="application" />
32
- <orderEntry type="library" scope="PROVIDED" name="racc (v1.6.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
33
- <orderEntry type="library" scope="PROVIDED" name="rack (v2.2.3, Remote: ruby-2.5.9-p229) [gem]" level="application" />
34
- <orderEntry type="library" scope="PROVIDED" name="rack-test (v1.1.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
35
- <orderEntry type="library" scope="PROVIDED" name="rails-dom-testing (v2.0.3, Remote: ruby-2.5.9-p229) [gem]" level="application" />
36
- <orderEntry type="library" scope="PROVIDED" name="rails-html-sanitizer (v1.4.2, Remote: ruby-2.5.9-p229) [gem]" level="application" />
37
- <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.1.1, Remote: ruby-2.5.9-p229) [gem]" level="application" />
38
- <orderEntry type="library" scope="PROVIDED" name="rake (v13.0.6, Remote: ruby-2.5.9-p229) [gem]" level="application" />
39
- <orderEntry type="library" scope="PROVIDED" name="rexml (v3.2.5, Remote: ruby-2.5.9-p229) [gem]" level="application" />
40
- <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.11.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
41
- <orderEntry type="library" scope="PROVIDED" name="thor (v1.2.1, Remote: ruby-2.5.9-p229) [gem]" level="application" />
42
- <orderEntry type="library" scope="PROVIDED" name="tzinfo (v2.0.4, Remote: ruby-2.5.9-p229) [gem]" level="application" />
43
- <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v2.1.0, Remote: ruby-2.5.9-p229) [gem]" level="application" />
44
- <orderEntry type="library" scope="PROVIDED" name="websocket-driver (v0.7.5, Remote: ruby-2.5.9-p229) [gem]" level="application" />
45
- <orderEntry type="library" scope="PROVIDED" name="websocket-extensions (v0.1.5, Remote: ruby-2.5.9-p229) [gem]" level="application" />
46
- <orderEntry type="library" scope="PROVIDED" name="zeitwerk (v2.5.4, Remote: ruby-2.5.9-p229) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="actioncable (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="actionmailbox (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="actionmailer (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="actionpack (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="actiontext (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="actionview (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="activejob (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="activemodel (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="activerecord (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="activestorage (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="activesupport (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="addressable (v2.8.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.2, Remote: ruby-3.1.1-p18) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="builder (v3.2.4, Remote: ruby-3.1.1-p18) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="byebug (v11.1.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="capybara (v3.36.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="concurrent-ruby (v1.1.10, Remote: ruby-3.1.1-p18) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="crass (v1.0.6, Remote: ruby-3.1.1-p18) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="database_cleaner-active_record (v2.0.1, Remote: ruby-3.1.1-p18) [gem]" level="application" />
34
+ <orderEntry type="library" scope="PROVIDED" name="database_cleaner-core (v2.0.1, Remote: ruby-3.1.1-p18) [gem]" level="application" />
35
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.5.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
36
+ <orderEntry type="library" scope="PROVIDED" name="digest (v3.1.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
37
+ <orderEntry type="library" scope="PROVIDED" name="erubi (v1.10.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
38
+ <orderEntry type="library" scope="PROVIDED" name="factory_bot_rails (v6.2.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
39
+ <orderEntry type="library" scope="PROVIDED" name="globalid (v1.0.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
40
+ <orderEntry type="library" scope="PROVIDED" name="i18n (v1.10.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
41
+ <orderEntry type="library" scope="PROVIDED" name="mail (v2.7.1, Remote: ruby-3.1.1-p18) [gem]" level="application" />
42
+ <orderEntry type="library" scope="PROVIDED" name="marcel (v1.0.2, Remote: ruby-3.1.1-p18) [gem]" level="application" />
43
+ <orderEntry type="library" scope="PROVIDED" name="matrix (v0.4.2, Remote: ruby-3.1.1-p18) [gem]" level="application" />
44
+ <orderEntry type="library" scope="PROVIDED" name="method_source (v1.0.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
45
+ <orderEntry type="library" scope="PROVIDED" name="mini_mime (v1.1.2, Remote: ruby-3.1.1-p18) [gem]" level="application" />
46
+ <orderEntry type="library" scope="PROVIDED" name="minitest (v5.15.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
47
+ <orderEntry type="library" scope="PROVIDED" name="net-imap (v0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
48
+ <orderEntry type="library" scope="PROVIDED" name="net-pop (v0.1.1, Remote: ruby-3.1.1-p18) [gem]" level="application" />
49
+ <orderEntry type="library" scope="PROVIDED" name="net-smtp (v0.3.1, Remote: ruby-3.1.1-p18) [gem]" level="application" />
50
+ <orderEntry type="library" scope="PROVIDED" name="nio4r (v2.5.8, Remote: ruby-3.1.1-p18) [gem]" level="application" />
51
+ <orderEntry type="library" scope="PROVIDED" name="racc (v1.6.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
52
+ <orderEntry type="library" scope="PROVIDED" name="rack (v2.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
53
+ <orderEntry type="library" scope="PROVIDED" name="rack-test (v1.1.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
54
+ <orderEntry type="library" scope="PROVIDED" name="rails (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
55
+ <orderEntry type="library" scope="PROVIDED" name="rails-dom-testing (v2.0.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
56
+ <orderEntry type="library" scope="PROVIDED" name="rails-html-sanitizer (v1.4.2, Remote: ruby-3.1.1-p18) [gem]" level="application" />
57
+ <orderEntry type="library" scope="PROVIDED" name="railties (v7.0.2.3, Remote: ruby-3.1.1-p18) [gem]" level="application" />
58
+ <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.1.1, Remote: ruby-3.1.1-p18) [gem]" level="application" />
59
+ <orderEntry type="library" scope="PROVIDED" name="rake (v13.0.6, Remote: ruby-3.1.1-p18) [gem]" level="application" />
60
+ <orderEntry type="library" scope="PROVIDED" name="rbs (v2.1.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
61
+ <orderEntry type="library" scope="PROVIDED" name="rexml (v3.2.5, Remote: ruby-3.1.1-p18) [gem]" level="application" />
62
+ <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.11.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
63
+ <orderEntry type="library" scope="PROVIDED" name="thor (v1.2.1, Remote: ruby-3.1.1-p18) [gem]" level="application" />
64
+ <orderEntry type="library" scope="PROVIDED" name="tzinfo (v2.0.4, Remote: ruby-3.1.1-p18) [gem]" level="application" />
65
+ <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v2.1.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
66
+ <orderEntry type="library" scope="PROVIDED" name="websocket-driver (v0.7.5, Remote: ruby-3.1.1-p18) [gem]" level="application" />
67
+ <orderEntry type="library" scope="PROVIDED" name="websocket-extensions (v0.1.5, Remote: ruby-3.1.1-p18) [gem]" level="application" />
68
+ <orderEntry type="library" scope="PROVIDED" name="xpath (v3.2.0, Remote: ruby-3.1.1-p18) [gem]" level="application" />
69
+ <orderEntry type="library" scope="PROVIDED" name="zeitwerk (v2.5.4, Remote: ruby-3.1.1-p18) [gem]" level="application" />
47
70
  </component>
48
71
  <component name="RakeTasksCache">
49
72
  <option name="myRootTask">
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activestorage-delayed (0.2.0.pre.pre)
4
+ activestorage-delayed (0.2.0)
5
5
  activestorage
6
6
  rails
7
7
 
data/README.md CHANGED
@@ -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, required: true, use_filename: true
42
+ has_one_attached :photo, required: true, use_filename: true, variant_info: { resize_to_fit: [400, 400], convert: 'jpg' }
43
43
  delayed_attach :photo
44
44
 
45
45
  has_many_attached :certificates
@@ -50,6 +50,7 @@ end
50
50
  ### `delayed_attach` accepts an optional hash with the following options:
51
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
+ - `variant_info`: (Hash) Variant information to be performed before uploading the file.
53
54
 
54
55
  ### Examples to upload files in background
55
56
  - Upload a single file
@@ -90,7 +91,7 @@ end
90
91
  User.first.update(certificates_tmp: { clean_before: true, files: [File.open('my_file.png')] })
91
92
  ```
92
93
 
93
- - Upload files with custom names (requires `use_filename: true`)
94
+ - Upload files with custom names (requires `use_filename: true`): `<attr_name>_filename`
94
95
  ```ruby
95
96
  class User < ApplicationRecord
96
97
  def photo_filename(filename)
@@ -101,42 +102,34 @@ end
101
102
  When `<attr_name>_filename` is defined, then it is called to fetch the uploaded file name.
102
103
  Note: Check [this](https://gist.github.com/owen2345/33730a452d73b6b292326bb602b0ee6b) if you want to rename an already uploaded file (remote file)
103
104
 
104
- - Capture event when file upload has failed
105
+ - Capture event when file upload has failed: `<attr_name>_error_upload`
105
106
  ```ruby
106
107
  class User < ApplicationRecord
107
- def ast_delayed_on_error(attr_name, error)
108
- puts "Failed #{attr_name} with #{error}"
108
+ # @param error [StandardError]
109
+ # @param file_data [Hash<'filename'>]
110
+ def photo_error_upload(error, file_data)
111
+ puts "Failed uploading photo #{file_data['filename']}: #{error.message}"
109
112
  end
110
113
  end
111
114
  ```
112
115
 
113
- - Capture event when file has been uploaded
116
+ - Capture event when file has been uploaded: `<attr_name>_after_upload`
114
117
  ```ruby
115
118
  class User < ApplicationRecord
116
- after_save_commit do
117
- puts 'Photo has been uploaded' if photo.blob.present?
118
- puts 'No pending enqueued photo uploads' unless photo_delayed_uploads.any?
119
+ # @param file_data [Hash<'filename'>]
120
+ def photo_after_upload(file_data)
121
+ puts "Photo #{file_data['filename']} has been uploaded"
119
122
  end
120
-
121
- before_save do
122
- puts "current assigned tmp photo: #{photo_tmp.inspect}"
123
+
124
+ def photo_after_upload_all
125
+ puts "All photos have been uploaded"
123
126
  end
124
127
  end
125
128
  ```
129
+
130
+ Note:
126
131
  `<attr_name>_delayed_uploads` is a `has_many` association that contains the list of scheduled uploads for the corresponding attribute.
127
132
 
128
-
129
- ## Preprocessing original files before uploading (Rails 7+)
130
- ```ruby
131
- class User < ApplicationRecord
132
- has_one_attached :photo do |attachable|
133
- attachable.variant :default, strip: true, quality: 70, resize_to_fill: [200, 200]
134
- end
135
- end
136
- ```
137
- `:default` variant will be used to pre-preprocess the original file before uploading (if defined). By this way you have your desired image size and quality as the original image.
138
-
139
-
140
133
 
141
134
  ## Contributing
142
135
  Bug reports and pull requests are welcome on https://github.com/owen2345/activestorage-delayed. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -40,11 +40,16 @@ module ActivestorageDelayed
40
40
  "#{send(:id)}-#{name}#{File.extname(filename)}"
41
41
  end
42
42
 
43
- define_method "#{attr_name}_after_upload" do
43
+ # @param _file_data [Hash<'filename'>]
44
+ define_method "#{attr_name}_after_upload" do |_file_data|
45
+ end
46
+
47
+ define_method "#{attr_name}_after_upload_all" do
44
48
  end
45
49
 
46
50
  # @param _error (Exception)
47
- define_method "#{attr_name}_error_upload" do |_error|
51
+ # @param _file_data [Hash<'filename'>]
52
+ define_method "#{attr_name}_error_upload" do |_error, _file_data|
48
53
  end
49
54
  end
50
55
  end
@@ -13,36 +13,43 @@ module ActivestorageDelayed
13
13
  return unless delayed_upload
14
14
 
15
15
  remove_files
16
- save_changes if upload_photos
16
+ upload_photos
17
+ save_changes
17
18
  end
18
19
 
19
20
  private
20
21
 
21
- # TODO: check the ability to delete io with save or upload method
22
- # file_data['io'].close
23
- def upload_photos # rubocop:disable Metrics/AbcSize
24
- tmp_files_data.each do |file_data|
22
+ def upload_photos
23
+ tmp_files_data.each(&method(:upload_photo))
24
+ end
25
+
26
+ def upload_photo(file_data)
27
+ parse_file_io(file_data) do |io|
28
+ file_data['io'] = io
25
29
  model.send(attr_name).attach(file_data.transform_keys(&:to_sym))
30
+ model.send("#{attr_name}_after_upload", file_data)
31
+ rescue => e # rubocop:disable Style/RescueStandardError
32
+ print_failure(e, file_data)
26
33
  end
27
- model.send("#{attr_name}_after_upload")
28
- true
29
- rescue => e # rubocop:disable Style/RescueStandardError
30
- Rails.logger.error("********* #{self.class.name} -> Failed uploading files: #{e.message}. #{e.backtrace[0..20]}")
31
- model.send("#{attr_name}_error_upload", e)
32
- false
34
+ end
35
+
36
+ def print_failure(error, file_data = {})
37
+ details = "#{error.message}. #{error.backtrace[0..20]}"
38
+ Rails.logger.error("***#{self.class.name} -> Failed uploading file (#{file_data['filename']}): #{details}")
39
+ model.send("#{attr_name}_error_upload", error, file_data)
33
40
  end
34
41
 
35
42
  def save_changes
36
43
  model.save!
37
44
  delayed_upload.destroy!
45
+ model.send("#{attr_name}_after_upload_all")
38
46
  end
39
47
 
40
- # @return [Array<Hash<io: StringIO, filename: String, content_type: String>]
48
+ # @return [Array<Hash<io: StringIO, filename: String, content_type: String>>]
41
49
  def tmp_files_data
42
50
  @tmp_files_data ||= begin
43
51
  files = JSON.parse(delayed_upload.files || '[]')
44
52
  files.each do |file_data|
45
- file_data['io'] = base64_to_file(file_data)
46
53
  if attr_settings[:use_filename]
47
54
  file_data['key'] = filename_for(file_data['filename'])
48
55
  file_data['filename'] = file_data['key']
@@ -51,9 +58,22 @@ module ActivestorageDelayed
51
58
  end
52
59
  end
53
60
 
54
- def base64_to_file(file_data)
55
- io = StringIO.new(Base64.decode64(file_data['io']))
56
- apply_variant(io, attr_settings[:variant_info]) { |io2| return io2 }
61
+ def parse_file_io(file_data, &block)
62
+ tempfile = Tempfile.new(file_data['filename'], binmode: true)
63
+ tempfile.write Base64.decode64(file_data['io'])
64
+ tempfile.rewind
65
+ transform_variation(tempfile, attr_settings[:variant_info]) do |io|
66
+ block.call(io)
67
+ tempfile.close
68
+ end
69
+ end
70
+
71
+ # @param io [StringIO, File]
72
+ # @param variant_info [Hash, Nil] ActiveStorage variant info. Sample: { resize_to_fit: [400, 400], convert: 'jpg' }
73
+ def transform_variation(io, variant_info, &block)
74
+ return block.call(io) unless variant_info
75
+
76
+ ActiveStorage::Variation.wrap(variant_info).transform(io, &block)
57
77
  end
58
78
 
59
79
  def model
@@ -84,13 +104,5 @@ module ActivestorageDelayed
84
104
  def attr_settings
85
105
  model.class.instance_variable_get(:@ast_delayed_settings)[attr_name]
86
106
  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
95
107
  end
96
108
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActivestorageDelayed
4
- VERSION = '0.2.0-pre'
4
+ VERSION = '0.3.1'
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.2.0.pre.pre
4
+ version: 0.3.1
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-05 00:00:00.000000000 Z
11
+ date: 2022-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activestorage
@@ -88,9 +88,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  requirements:
91
- - - ">"
91
+ - - ">="
92
92
  - !ruby/object:Gem::Version
93
- version: 1.3.1
93
+ version: '0'
94
94
  requirements: []
95
95
  rubygems_version: 3.1.2
96
96
  signing_key: