diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-05 16:09:41 +0200 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-05 16:09:41 +0200 |
commit | b7cc003aa0aada594cb18ab80c13c13c75bcd389 (patch) | |
tree | e42658bfa9441c788d3c0c88680d2e92e0159469 /lib/active_vault | |
parent | aaf841518866b34d769d9a951a389d1eef70d6e7 (diff) | |
download | rails-b7cc003aa0aada594cb18ab80c13c13c75bcd389.tar.gz rails-b7cc003aa0aada594cb18ab80c13c13c75bcd389.tar.bz2 rails-b7cc003aa0aada594cb18ab80c13c13c75bcd389.zip |
Attached one and many
Diffstat (limited to 'lib/active_vault')
-rw-r--r-- | lib/active_vault/attached.rb | 34 | ||||
-rw-r--r-- | lib/active_vault/attached/macros.rb | 15 | ||||
-rw-r--r-- | lib/active_vault/attached/many.rb | 22 | ||||
-rw-r--r-- | lib/active_vault/attached/one.rb | 24 | ||||
-rw-r--r-- | lib/active_vault/attachment.rb | 1 | ||||
-rw-r--r-- | lib/active_vault/attachments.rb | 30 | ||||
-rw-r--r-- | lib/active_vault/railtie.rb | 6 |
7 files changed, 98 insertions, 34 deletions
diff --git a/lib/active_vault/attached.rb b/lib/active_vault/attached.rb new file mode 100644 index 0000000000..a968f3500d --- /dev/null +++ b/lib/active_vault/attached.rb @@ -0,0 +1,34 @@ +require "active_vault/blob" +require "active_vault/attachment" + +require "action_dispatch/http/upload" +require "active_support/core_ext/module/delegation" + +class ActiveVault::Attached + attr_reader :name, :record + + def initialize(name, record) + @name, @record = name, record + end + + private + def create_blob_from(attachable) + case attachable + when ActiveVault::Blob + attachable + when ActionDispatch::Http::UploadedFile + ActiveVault::Blob.create_after_upload! \ + io: attachable.open, + filename: attachable.original_filename, + content_type: attachable.content_type + when Hash + ActiveVault::Blob.create_after_upload!(attachable) + else + nil + end + end +end + +require "active_vault/attached/one" +require "active_vault/attached/many" +require "active_vault/attached/macros" diff --git a/lib/active_vault/attached/macros.rb b/lib/active_vault/attached/macros.rb new file mode 100644 index 0000000000..8cc84a95a9 --- /dev/null +++ b/lib/active_vault/attached/macros.rb @@ -0,0 +1,15 @@ +module ActiveVault::Attached::Macros + def has_one_attached(name) + define_method(name) do + instance_variable_get("@active_vault_attached_#{name}") || + instance_variable_set("@active_vault_attached_#{name}", ActiveVault::Attached::One.new(name, self)) + end + end + + def has_many_attached(name) + define_method(name) do + instance_variable_get("@active_vault_attached_#{name}") || + instance_variable_set("@active_vault_attached_#{name}", ActiveVault::Attached::Many.new(name, self)) + end + end +end diff --git a/lib/active_vault/attached/many.rb b/lib/active_vault/attached/many.rb new file mode 100644 index 0000000000..9f5f14ee85 --- /dev/null +++ b/lib/active_vault/attached/many.rb @@ -0,0 +1,22 @@ +class ActiveVault::Attached::Many < ActiveVault::Attached + delegate_missing_to :attachments + + def attachments + @attachments ||= ActiveVault::Attachment.where(record_gid: record.to_gid.to_s, name: name) + end + + def attach(*attachables) + @attachments = attachments + Array(attachables).collect do |attachable| + ActiveVault::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable)) + end + end + + def attached? + attachments.any? + end + + def purge + attachments.each(&:purge) + @attachments = nil + end +end diff --git a/lib/active_vault/attached/one.rb b/lib/active_vault/attached/one.rb new file mode 100644 index 0000000000..5566c1b971 --- /dev/null +++ b/lib/active_vault/attached/one.rb @@ -0,0 +1,24 @@ +class ActiveVault::Attached::One < ActiveVault::Attached + delegate_missing_to :attachment + + def attachment + @attachment ||= ActiveVault::Attachment.find_by(record_gid: record.to_gid.to_s, name: name) + end + + def attach(attachable) + if @attachment + # FIXME: Have options to declare dependent: :purge to clean up + end + + @attachment = ActiveVault::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable)) + end + + def attached? + attachment.present? + end + + def purge + attachment.purge + @attachment = nil + end +end diff --git a/lib/active_vault/attachment.rb b/lib/active_vault/attachment.rb index eb108e9cbb..1c96dabe31 100644 --- a/lib/active_vault/attachment.rb +++ b/lib/active_vault/attachment.rb @@ -22,6 +22,5 @@ class ActiveVault::Attachment < ActiveRecord::Base def purge blob.purge destroy - record.public_send "#{name}=", nil end end diff --git a/lib/active_vault/attachments.rb b/lib/active_vault/attachments.rb deleted file mode 100644 index c66c142650..0000000000 --- a/lib/active_vault/attachments.rb +++ /dev/null @@ -1,30 +0,0 @@ -require "active_vault/attachment" -require "action_dispatch/http/upload" - -module ActiveVault::Attachments - def has_file(name) - define_method(name) do - (@active_vault_attachments ||= {})[name] ||= - ActiveVault::Attachment.find_by(record_gid: to_gid.to_s, name: name)&.tap { |a| a.record = self } - end - - define_method(:"#{name}=") do |attachable| - case attachable - when ActiveVault::Blob - blob = attachable - when ActionDispatch::Http::UploadedFile - blob = ActiveVault::Blob.create_after_upload! \ - io: attachable.open, - filename: attachable.original_filename, - content_type: attachable.content_type - when Hash - blob = ActiveVault::Blob.create_after_upload!(attachable) - when NilClass - blob = nil - end - - (@active_vault_attachments ||= {})[name] = blob ? - ActiveVault::Attachment.create!(record_gid: to_gid.to_s, name: name, blob: blob)&.tap { |a| a.record = self } : nil - end - end -end diff --git a/lib/active_vault/railtie.rb b/lib/active_vault/railtie.rb index f1c5740aa5..a0789f708f 100644 --- a/lib/active_vault/railtie.rb +++ b/lib/active_vault/railtie.rb @@ -16,11 +16,11 @@ module ActiveVault end end - initializer "action_file.attachments" do - require "active_vault/attachments" + initializer "action_file.attached" do + require "active_vault/attached" ActiveSupport.on_load(:active_record) do - extend ActiveVault::Attachments + extend ActiveVault::Attached::Macros end end end |