diff --git a/app/controllers/concerns/creates_ticket_articles.rb b/app/controllers/concerns/creates_ticket_articles.rb index 6026fdc48..58912fcc2 100644 --- a/app/controllers/concerns/creates_ticket_articles.rb +++ b/app/controllers/concerns/creates_ticket_articles.rb @@ -56,24 +56,18 @@ module CreatesTicketArticles end # find attachments in upload cache + attachments = [] if form_id - article.attachments = UploadCache.new(form_id).attachments + attachments += UploadCache.new(form_id).attachments end - # set subtype of present - article.preferences[:subtype] = subtype if subtype.present? - - article.save! - # store inline attachments attachments_inline.each do |attachment| - Store.add( - object: 'Ticket::Article', - o_id: article.id, + attachments << { data: attachment[:data], filename: attachment[:filename], preferences: attachment[:preferences], - ) + } end # add attachments as param @@ -104,16 +98,21 @@ module CreatesTicketArticles raise Exceptions::UnprocessableEntity, "Invalid base64 for attachment with index '#{index}'" end - Store.add( - object: 'Ticket::Article', - o_id: article.id, + attachments << { data: attachment_data, filename: attachment[:filename], preferences: preferences, - ) + } end end + article.attachments = attachments + + # set subtype of present + article.preferences[:subtype] = subtype if subtype.present? + + article.save! + # account time if time_unit.present? Ticket::TimeAccounting.create!( diff --git a/app/models/application_model/has_attachments.rb b/app/models/application_model/has_attachments.rb index c59339eef..d3d01a0bf 100644 --- a/app/models/application_model/has_attachments.rb +++ b/app/models/application_model/has_attachments.rb @@ -44,10 +44,18 @@ returns =begin -store attachments for this object +store attachments for this object with store objects or hashes item = Model.find(123) - item.attachments = [ Store-Object1, Store-Object2 ] + item.attachments = [ + Store-Object1, + Store-Object2, + { + filename: 'test.txt', + data: 'test', + preferences: {}, + } + ] =end @@ -118,14 +126,22 @@ For use in #search_index_attribute_lookup # store attachments article_store = [] attachments_buffer.each do |attachment| - article_store.push Store.add( + data = { object: self.class.to_s, o_id: id, - data: attachment.content, - filename: attachment.filename, - preferences: attachment.preferences, created_by_id: created_by_id, - ) + } + if attachment.is_a?(Store) + data[:data] = attachment.content + data[:filename] = attachment.filename + data[:preferences] = attachment.preferences + else + data[:data] = attachment[:data] + data[:filename] = attachment[:filename] + data[:preferences] = attachment[:preferences] + end + + article_store.push Store.add(data) end end end