Moved attachment support to any model (object.attachments & object.attachments=).

This commit is contained in:
Martin Edenhofer 2014-02-05 13:22:14 +01:00
parent e11d9cada5
commit edecfc5bda
5 changed files with 70 additions and 32 deletions

View file

@ -110,7 +110,7 @@ class TicketArticlesController < ApplicationController
return
end
list = Store.list( :object => 'Ticket::Article', :o_id => params[:article_id] ) || []
list = article.attachments || []
access = false
list.each {|item|
if item.id.to_i == params[:id].to_i

View file

@ -16,6 +16,9 @@ class ApplicationModel < ActiveRecord::Base
after_update :cache_delete
after_destroy :cache_delete
after_create :attachments_buffer_check
after_update :attachments_buffer_check
after_create :activity_stream_create
after_update :activity_stream_update
after_destroy :activity_stream_destroy
@ -752,8 +755,70 @@ delete object history, will be executed automatically
History.remove( self.class.to_s, self.id )
end
=begin
get list of attachments of this object
item = Model.find(123)
list = item.attachments
returns
# array with Store model objects
=end
def attachments
Store.list( :object => self.class.to_s, :o_id => self.id )
end
=begin
store attachments for this object
item = Model.find(123)
item.attachments = [ Store-Object1, Store-Object2 ]
=end
def attachments=(attachments)
self.attachments_buffer = attachments
# update if object already exists
if self.id && self.id != 0
attachments_buffer_check
end
end
private
def attachments_buffer
@attachments_buffer_data
end
def attachments_buffer=(attachments)
@attachments_buffer_data = attachments
end
def attachments_buffer_check
# do nothing if no attachment exists
return 1 if attachments_buffer == nil
# store attachments
article_store = []
attachments_buffer.each do |attachment|
article_store.push Store.add(
:object => self.class.to_s,
:o_id => self.id,
:data => attachment.store_file.data,
:filename => attachment.filename,
:preferences => attachment.preferences,
:created_by_id => self.created_by_id,
)
end
attachments_buffer = nil
end
=begin
check string/varchar size and cut them if needed

View file

@ -12,7 +12,6 @@ class Ticket::Article < ApplicationModel
belongs_to :ticket_article_type, :class_name => 'Ticket::Article::Type'
belongs_to :ticket_article_sender, :class_name => 'Ticket::Article::Sender'
belongs_to :created_by, :class_name => 'User'
after_create :attachment_check
after_create :notify_clients_after_create
after_update :notify_clients_after_update
after_destroy :notify_clients_after_destroy
@ -27,30 +26,6 @@ class Ticket::Article < ApplicationModel
:ticket_article_sender_id => true,
}
attr_accessor :attachments
private
def attachment_check
# do nothing if no attachment exists
return 1 if self.attachments == nil
# store attachments
article_store = []
self.attachments.each do |attachment|
article_store.push Store.add(
:object => 'Ticket::Article',
:o_id => self.id,
:data => attachment.store_file.data,
:filename => attachment.filename,
:preferences => attachment.preferences,
:created_by_id => self.created_by_id,
)
end
self.attachments = article_store
end
class Flag < ApplicationModel
end

View file

@ -38,7 +38,7 @@ returns
data[ Ticket::Article.to_app_model ][ self.id ] = self.attributes
# add attachment list to article
data[ Ticket::Article.to_app_model ][ self.id ]['attachments'] = Store.list( :object => 'Ticket::Article', :o_id => self.id )
data[ Ticket::Article.to_app_model ][ self.id ]['attachments'] = self.attachments
end
if !data[ User.to_app_model ]

View file

@ -54,15 +54,13 @@ returns
article_attributes = search_index_attribute_lookup( article_attributes, article )
# lookup attachments
attachments = Store.list( :object => 'Ticket::Article', :o_id => article.id )
attachments.each {|attachment|
article.attachments.each {|attachment|
if !article_attributes['attachments']
article_attributes['attachments'] = []
end
file = Store.find( attachment.id )
data = {
"_name" => file.filename,
"content" => Base64.encode64( file.store_file.data )
"_name" => attachment.filename,
"content" => Base64.encode64( attachment.store_file.data )
}
article_attributes['attachments'].push data
}