Moved attachment support to any model (object.attachments & object.attachments=).
This commit is contained in:
parent
e11d9cada5
commit
edecfc5bda
5 changed files with 70 additions and 32 deletions
|
@ -110,7 +110,7 @@ class TicketArticlesController < ApplicationController
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
list = Store.list( :object => 'Ticket::Article', :o_id => params[:article_id] ) || []
|
list = article.attachments || []
|
||||||
access = false
|
access = false
|
||||||
list.each {|item|
|
list.each {|item|
|
||||||
if item.id.to_i == params[:id].to_i
|
if item.id.to_i == params[:id].to_i
|
||||||
|
|
|
@ -16,6 +16,9 @@ class ApplicationModel < ActiveRecord::Base
|
||||||
after_update :cache_delete
|
after_update :cache_delete
|
||||||
after_destroy :cache_delete
|
after_destroy :cache_delete
|
||||||
|
|
||||||
|
after_create :attachments_buffer_check
|
||||||
|
after_update :attachments_buffer_check
|
||||||
|
|
||||||
after_create :activity_stream_create
|
after_create :activity_stream_create
|
||||||
after_update :activity_stream_update
|
after_update :activity_stream_update
|
||||||
after_destroy :activity_stream_destroy
|
after_destroy :activity_stream_destroy
|
||||||
|
@ -752,8 +755,70 @@ delete object history, will be executed automatically
|
||||||
History.remove( self.class.to_s, self.id )
|
History.remove( self.class.to_s, self.id )
|
||||||
end
|
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
|
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
|
=begin
|
||||||
|
|
||||||
check string/varchar size and cut them if needed
|
check string/varchar size and cut them if needed
|
||||||
|
|
|
@ -12,7 +12,6 @@ class Ticket::Article < ApplicationModel
|
||||||
belongs_to :ticket_article_type, :class_name => 'Ticket::Article::Type'
|
belongs_to :ticket_article_type, :class_name => 'Ticket::Article::Type'
|
||||||
belongs_to :ticket_article_sender, :class_name => 'Ticket::Article::Sender'
|
belongs_to :ticket_article_sender, :class_name => 'Ticket::Article::Sender'
|
||||||
belongs_to :created_by, :class_name => 'User'
|
belongs_to :created_by, :class_name => 'User'
|
||||||
after_create :attachment_check
|
|
||||||
after_create :notify_clients_after_create
|
after_create :notify_clients_after_create
|
||||||
after_update :notify_clients_after_update
|
after_update :notify_clients_after_update
|
||||||
after_destroy :notify_clients_after_destroy
|
after_destroy :notify_clients_after_destroy
|
||||||
|
@ -27,30 +26,6 @@ class Ticket::Article < ApplicationModel
|
||||||
:ticket_article_sender_id => true,
|
: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
|
class Flag < ApplicationModel
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ returns
|
||||||
data[ Ticket::Article.to_app_model ][ self.id ] = self.attributes
|
data[ Ticket::Article.to_app_model ][ self.id ] = self.attributes
|
||||||
|
|
||||||
# add attachment list to article
|
# 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
|
end
|
||||||
|
|
||||||
if !data[ User.to_app_model ]
|
if !data[ User.to_app_model ]
|
||||||
|
|
|
@ -54,15 +54,13 @@ returns
|
||||||
article_attributes = search_index_attribute_lookup( article_attributes, article )
|
article_attributes = search_index_attribute_lookup( article_attributes, article )
|
||||||
|
|
||||||
# lookup attachments
|
# lookup attachments
|
||||||
attachments = Store.list( :object => 'Ticket::Article', :o_id => article.id )
|
article.attachments.each {|attachment|
|
||||||
attachments.each {|attachment|
|
|
||||||
if !article_attributes['attachments']
|
if !article_attributes['attachments']
|
||||||
article_attributes['attachments'] = []
|
article_attributes['attachments'] = []
|
||||||
end
|
end
|
||||||
file = Store.find( attachment.id )
|
|
||||||
data = {
|
data = {
|
||||||
"_name" => file.filename,
|
"_name" => attachment.filename,
|
||||||
"content" => Base64.encode64( file.store_file.data )
|
"content" => Base64.encode64( attachment.store_file.data )
|
||||||
}
|
}
|
||||||
article_attributes['attachments'].push data
|
article_attributes['attachments'].push data
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue