active_shrine 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/.rspec +1 -0
  4. data/CHANGELOG.md +5 -0
  5. data/CODE_OF_CONDUCT.md +84 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +42 -14
  8. data/Rakefile +2 -8
  9. data/config.ru +9 -0
  10. data/lib/.DS_Store +0 -0
  11. data/lib/active_shrine/attached/base.rb +24 -0
  12. data/lib/active_shrine/attached/changes/create_many.rb +45 -0
  13. data/lib/active_shrine/attached/changes/create_one.rb +51 -0
  14. data/lib/active_shrine/attached/changes/create_one_of_many.rb +17 -0
  15. data/lib/active_shrine/attached/changes/delete_many.rb +28 -0
  16. data/lib/active_shrine/attached/changes/delete_one.rb +24 -0
  17. data/lib/active_shrine/attached/changes/detach_many.rb +24 -0
  18. data/lib/active_shrine/attached/changes/detach_one.rb +31 -0
  19. data/lib/active_shrine/attached/changes/purge_many.rb +34 -0
  20. data/lib/active_shrine/attached/changes/purge_one.rb +34 -0
  21. data/lib/active_shrine/attached/changes.rb +24 -0
  22. data/lib/active_shrine/attached/many.rb +78 -0
  23. data/lib/active_shrine/attached/one.rb +90 -0
  24. data/lib/active_shrine/attached.rb +14 -0
  25. data/lib/active_shrine/attachment.rb +111 -0
  26. data/lib/active_shrine/job/destroy_shrine_attachment.rb +18 -0
  27. data/lib/active_shrine/job/promote_shrine_attachment.rb +21 -0
  28. data/lib/active_shrine/job.rb +12 -0
  29. data/lib/active_shrine/model.rb +200 -0
  30. data/lib/active_shrine/railtie.rb +11 -0
  31. data/lib/active_shrine/reflection.rb +75 -0
  32. data/lib/active_shrine/version.rb +5 -0
  33. data/lib/active_shrine.rb +18 -0
  34. data/lib/generators/.DS_Store +0 -0
  35. data/lib/generators/active_shrine/install/install_generator.rb +29 -0
  36. data/lib/generators/active_shrine/install/templates/app/jobs/destroy_shrine_attachment_job.rb +5 -0
  37. data/lib/generators/active_shrine/install/templates/app/jobs/promote_shrine_attachment_job.rb +5 -0
  38. data/lib/generators/active_shrine/install/templates/config/initializers/shrine.rb +51 -0
  39. data/lib/generators/active_shrine/install/templates/db/migrate/create_active_shrine_attachments.rb +28 -0
  40. data/sig/active_shrine.rbs +4 -0
  41. metadata +120 -47
  42. data/MIT-LICENSE +0 -20
  43. data/app/assets/config/api_base_manifest.js +0 -1
  44. data/app/assets/stylesheets/api_base/application.css +0 -15
  45. data/app/controllers/api_base/application_controller.rb +0 -6
  46. data/app/helpers/api_base/application_helper.rb +0 -6
  47. data/app/jobs/api_base/application_job.rb +0 -6
  48. data/app/mailers/api_base/application_mailer.rb +0 -8
  49. data/app/models/api_base/api_log.rb +0 -108
  50. data/app/models/api_base/application_record.rb +0 -7
  51. data/app/views/layouts/api_base/application.html.erb +0 -15
  52. data/config/routes.rb +0 -4
  53. data/db/migrate/20220612165032_create_api_logs.rb +0 -22
  54. data/lib/api_base/behaviours/get_json.rb +0 -26
  55. data/lib/api_base/behaviours/post_json.rb +0 -27
  56. data/lib/api_base/behaviours/shared.rb +0 -51
  57. data/lib/api_base/concerns/filterer.rb +0 -47
  58. data/lib/api_base/concerns/traceable.rb +0 -25
  59. data/lib/api_base/connection.rb +0 -24
  60. data/lib/api_base/endpoint.rb +0 -65
  61. data/lib/api_base/engine.rb +0 -7
  62. data/lib/api_base/errors/api_error.rb +0 -8
  63. data/lib/api_base/errors/processing_error.rb +0 -8
  64. data/lib/api_base/service.rb +0 -17
  65. data/lib/api_base/version.rb +0 -5
  66. data/lib/api_base.rb +0 -14
  67. data/lib/tasks/api_base_tasks.rake +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 652735c064853b593b5e1f0b2016910ef2bb88b9dccaac1304bb0772fc03bd22
4
- data.tar.gz: 1c0f1e913e38184e3cdd207adc554b7aaa1f3d1158764452437d689024b14f69
3
+ metadata.gz: 34fb0603d069fcc810d0e968121f5c5ff8b9cb710561c82c299285878fe3b696
4
+ data.tar.gz: 9b13ad8c45bb663d09bfa78bb22160ad287aa1d7d735f9f54b6ae52932cb3226
5
5
  SHA512:
6
- metadata.gz: 4b18fe7297b58553d242567f4f01e38ea8f6935bbcb290fcb8a482b6e7c0a4e02e8faed18a9ec374d6633ff26094f36b0c06cc4d155a1a77407a3c5853661d3c
7
- data.tar.gz: cca152859107cb988e34d41c82e18db82e09fdd351bfc93d4255f1858324095a4e02acc8306180ea9f7e6dad8511cc9f1081f36970d4865068464f94e2f867f2
6
+ metadata.gz: 1462e915e1e433d9e89f7bae30a404f251c970cf2452e97e43e5c7ec2f2956705a4c5e6e1e1d9edbefbcdd7234bece819aceaa8e2c8f709b20fb8d8b030618c4
7
+ data.tar.gz: 1b9be1fdf5bbb5cca252c322a73b7045aeeabb4e6cade989876adcc81c0a452864aebd92ea1a38d5018a04bdd20c981e349f06c3e0f224ab757ee7dda21b51d9
data/.DS_Store ADDED
Binary file
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-12-01
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at starletgh@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Radioactive Labs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,28 +1,56 @@
1
- # ApiBase
2
- Short description and motivation.
1
+ # ActiveShrine
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_shrine`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Quick Start
6
8
 
7
- ## Installation
8
- Add this line to your application's Gemfile:
9
+ Get ActiveShrine up and running in your Rails application with these simple steps:
10
+
11
+ 1. **Add Plutonium to your Gemfile:**
9
12
 
10
13
  ```ruby
11
- gem "api_base"
14
+ gem "active_shrine"
12
15
  ```
13
16
 
14
- And then execute:
15
- ```bash
16
- $ bundle
17
+ 2. **Bundle Install:**
18
+
19
+ ```shell
20
+ bundle
17
21
  ```
18
22
 
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install api_base
23
+ 3. **Install ActiveShrine:**
24
+
25
+ ```shell
26
+ rails g active_shrine:install
22
27
  ```
23
28
 
29
+ ## Usage
30
+
31
+ ```ruby
32
+ class Blog < ApplicationRecord
33
+ include ActiveShrine::Model
34
+
35
+ has_one_attached :avatar
36
+ has_many_attached :documents
37
+ end
38
+ ```
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
24
46
  ## Contributing
25
- Contribution directions go here.
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_shrine. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/active_shrine/blob/main/CODE_OF_CONDUCT.md).
26
49
 
27
50
  ## License
51
+
28
52
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
53
+
54
+ ## Code of Conduct
55
+
56
+ Everyone interacting in the ActiveShrine project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/active_shrine/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,10 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bundler/setup'
4
-
5
- APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
6
- load 'rails/tasks/engine.rake'
7
-
8
- load 'rails/tasks/statistics.rake'
9
-
10
- require 'bundler/gem_tasks'
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/config.ru ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubygems"
4
+ require "bundler"
5
+
6
+ Bundler.require :default, :development
7
+
8
+ Combustion.initialize! :all
9
+ run Combustion::Application
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ # = ActiveShrine\Attached\Base
6
+ #
7
+ # Abstract base class for the concrete ActiveShrine::Attached::One and ActiveShrine::Attached::Many
8
+ # classes that both provide proxy access to the attachment association for a record.
9
+ class Base
10
+ attr_reader :name, :record
11
+
12
+ def initialize(name, record)
13
+ @name = name
14
+ @record = record
15
+ end
16
+
17
+ private
18
+
19
+ def change
20
+ record.shrine_attachment_changes[name]
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ module Changes
6
+ class CreateMany # :nodoc:
7
+ attr_reader :name, :record, :attachables, :pending_uploads
8
+
9
+ def initialize(name, record, attachables, pending_uploads: [])
10
+ @name = name
11
+ @record = record
12
+ @attachables = Array(attachables)
13
+ @pending_uploads = Array(pending_uploads) + subchanges
14
+ attachments
15
+ end
16
+
17
+ def attachments
18
+ @attachments ||= subchanges.collect(&:attachment)
19
+ end
20
+
21
+ def save
22
+ assign_associated_attachments
23
+ end
24
+
25
+ private
26
+
27
+ def subchanges
28
+ @subchanges ||= attachables.collect { |attachable| build_subchange_from(attachable) }
29
+ end
30
+
31
+ def build_subchange_from(attachable)
32
+ Attached::Changes::CreateOneOfMany.new(name, record, attachable)
33
+ end
34
+
35
+ def assign_associated_attachments
36
+ record.public_send(:"#{name}_attachments=", persisted_or_new_attachments)
37
+ end
38
+
39
+ def persisted_or_new_attachments
40
+ attachments.select { |attachment| attachment.persisted? || attachment.new_record? }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_dispatch"
4
+ require "action_dispatch/http/upload"
5
+
6
+ module ActiveShrine
7
+ module Attached
8
+ module Changes
9
+ class CreateOne # :nodoc:
10
+ attr_reader :name, :record, :attachable
11
+
12
+ def initialize(name, record, attachable)
13
+ @name = name
14
+ @record = record
15
+ @attachable = attachable
16
+
17
+ attach
18
+ end
19
+
20
+ def attachment
21
+ @attachment ||= find_or_build_attachment
22
+ end
23
+
24
+ def save
25
+ record.public_send(:"#{name}_attachment=", attachment)
26
+ end
27
+
28
+ private
29
+
30
+ def attach
31
+ attachment.file = attachable
32
+ attachment
33
+ end
34
+
35
+ def find_or_build_attachment
36
+ find_attachment || build_attachment
37
+ end
38
+
39
+ def find_attachment
40
+ attachment = record.public_send(:"#{name}_attachment")
41
+
42
+ attachment if attachable.is_a?(String) && attachment&.signed_id == attachable
43
+ end
44
+
45
+ def build_attachment
46
+ ActiveShrine::Attachment.new(record:, name:)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ module Changes
6
+ class CreateOneOfMany < Attached::Changes::CreateOne # :nodoc:
7
+ private
8
+
9
+ def find_attachment
10
+ record.public_send(:"#{name}_attachments").detect do |attachment|
11
+ attachable.is_a?(String) && attachment.signed_id == attachable
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ module Changes
6
+ class DeleteMany # :nodoc:
7
+ attr_reader :name, :record
8
+
9
+ def initialize(name, record)
10
+ @name = name
11
+ @record = record
12
+ end
13
+
14
+ def attachables
15
+ []
16
+ end
17
+
18
+ def attachments
19
+ ActiveShrine::Attachment.none
20
+ end
21
+
22
+ def save
23
+ record.public_send(:"#{name}_attachments=", [])
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ module Changes
6
+ class DeleteOne # :nodoc:
7
+ attr_reader :name, :record
8
+
9
+ def initialize(name, record)
10
+ @name = name
11
+ @record = record
12
+ end
13
+
14
+ def attachment
15
+ nil
16
+ end
17
+
18
+ def save
19
+ record.public_send(:"#{name}_attachment=", nil)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ module Changes
6
+ class DetachMany # :nodoc:
7
+ attr_reader :name, :record, :attachments
8
+
9
+ def initialize(name, record, attachments)
10
+ @name = name
11
+ @record = record
12
+ @attachments = attachments
13
+ end
14
+
15
+ def detach
16
+ return unless attachments.any?
17
+
18
+ attachments.delete_all if attachments.respond_to?(:delete_all)
19
+ record.shrine_attachment_changes.delete(name)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ module Changes
6
+ class DetachOne # :nodoc:
7
+ attr_reader :name, :record, :attachment
8
+
9
+ def initialize(name, record, attachment)
10
+ @name = name
11
+ @record = record
12
+ @attachment = attachment
13
+ end
14
+
15
+ def detach
16
+ return unless attachment.present?
17
+
18
+ attachment.delete
19
+ reset
20
+ end
21
+
22
+ private
23
+
24
+ def reset
25
+ record.shrine_attachment_changes.delete(name)
26
+ record.public_send(:"#{name}_attachment=", nil)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ module Changes
6
+ class PurgeMany # :nodoc:
7
+ attr_reader :name, :record, :attachments
8
+
9
+ def initialize(name, record, attachments)
10
+ @name = name
11
+ @record = record
12
+ @attachments = attachments
13
+ end
14
+
15
+ def purge
16
+ attachments.each(&:purge)
17
+ reset
18
+ end
19
+
20
+ def purge_later
21
+ attachments.each(&:purge_later)
22
+ reset
23
+ end
24
+
25
+ private
26
+
27
+ def reset
28
+ record.shrine_attachment_changes.delete(name)
29
+ record.public_send(:"#{name}_attachments").reset
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ module Changes
6
+ class PurgeOne # :nodoc:
7
+ attr_reader :name, :record, :attachment
8
+
9
+ def initialize(name, record, attachment)
10
+ @name = name
11
+ @record = record
12
+ @attachment = attachment
13
+ end
14
+
15
+ def purge
16
+ attachment&.purge
17
+ reset
18
+ end
19
+
20
+ def purge_later
21
+ attachment&.purge_later
22
+ reset
23
+ end
24
+
25
+ private
26
+
27
+ def reset
28
+ record.shrine_attachment_changes.delete(name)
29
+ record.public_send(:"#{name}_attachment=", nil)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ module Changes # :nodoc:
6
+ extend ActiveSupport::Autoload
7
+
8
+ eager_autoload do
9
+ autoload :CreateOne
10
+ autoload :CreateMany
11
+ autoload :CreateOneOfMany
12
+
13
+ autoload :DeleteOne
14
+ autoload :DeleteMany
15
+
16
+ autoload :DetachOne
17
+ autoload :DetachMany
18
+
19
+ autoload :PurgeOne
20
+ autoload :PurgeMany
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShrine
4
+ module Attached
5
+ # = ActiveShrine\Attached\Many
6
+ #
7
+ # Decorated proxy object representing of multiple attachments to a model.
8
+ class Many < Base
9
+ ##
10
+ # :method: purge
11
+ #
12
+ # Directly purges each associated attachment (i.e. destroys the blobs and
13
+ # attachments and deletes the files on the service).
14
+ delegate :purge, to: :purge_many
15
+
16
+ ##
17
+ # :method: purge_later
18
+ #
19
+ # Purges each associated attachment through the queuing system.
20
+ delegate :purge_later, to: :purge_many
21
+
22
+ ##
23
+ # :method: detach
24
+ #
25
+ # Deletes associated attachments without purging them, leaving their respective blobs in place.
26
+ delegate :detach, to: :detach_many
27
+
28
+ delegate_missing_to :attachments
29
+
30
+ # Returns all the associated attachment records.
31
+ #
32
+ # All methods called on this proxy object that aren't listed here will automatically be delegated to +attachments+.
33
+ def attachments
34
+ change.present? ? change.attachments : record.public_send(:"#{name}_attachments")
35
+ end
36
+
37
+ # Attaches one or more +attachables+ to the record.
38
+ #
39
+ # If the record is persisted and unchanged, the attachments are saved to
40
+ # the database immediately. Otherwise, they'll be saved to the DB when the
41
+ # record is next saved.
42
+ #
43
+ # document.images.attach(params[:images]) # Array of ActionDispatch::Http::UploadedFile objects
44
+ # person.avatar.attach(params[:signed_id]) # Signed reference to attachment
45
+ #
46
+ # See https://shrinerb.com/docs/attacher#attaching for more
47
+ def attach(*attachables)
48
+ record.public_send(:"#{name}=", attachments.map(&:signed_id) + attachables.flatten)
49
+ if record.persisted? && !record.changed?
50
+ return if !record.save
51
+ end
52
+
53
+ record.public_send(:"#{name}")
54
+ end
55
+
56
+ # Returns true if any attachments have been made.
57
+ #
58
+ # class Gallery < ApplicationRecord
59
+ # has_many_attached :photos
60
+ # end
61
+ #
62
+ # Gallery.new.photos.attached? # => false
63
+ def attached?
64
+ attachments.any?
65
+ end
66
+
67
+ private
68
+
69
+ def purge_many
70
+ Attached::Changes::PurgeMany.new(name, record, attachments)
71
+ end
72
+
73
+ def detach_many
74
+ Attached::Changes::DetachMany.new(name, record, attachments)
75
+ end
76
+ end
77
+ end
78
+ end