From e8682c5bf051517b0b265e446aa1a7eccfd47bf7 Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Sat, 7 Jul 2018 23:25:33 -0400 Subject: Store newly-uploaded files on save rather than assignment --- .../active_storage/attached/changes/create_many.rb | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 activestorage/lib/active_storage/attached/changes/create_many.rb (limited to 'activestorage/lib/active_storage/attached/changes/create_many.rb') diff --git a/activestorage/lib/active_storage/attached/changes/create_many.rb b/activestorage/lib/active_storage/attached/changes/create_many.rb new file mode 100644 index 0000000000..3f7ca6a25f --- /dev/null +++ b/activestorage/lib/active_storage/attached/changes/create_many.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module ActiveStorage + class Attached::Changes::CreateMany #:nodoc: + attr_reader :name, :record, :attachables + + def initialize(name, record, attachables) + @name, @record, @attachables = name, record, Array(attachables) + end + + def attachments + @attachments ||= subchanges.collect(&:attachment) + end + + def upload + subchanges.each(&:upload) + end + + def save + record.public_send("#{name}_attachments=", attachments) + end + + private + def subchanges + @subchanges ||= attachables.collect { |attachable| build_subchange_from(attachable) } + end + + def build_subchange_from(attachable) + ActiveStorage::Attached::Changes::CreateOneOfMany.new(name, record, attachable) + end + end +end -- cgit v1.2.3