Reduced count of sql queries.
This commit is contained in:
parent
a25588c1c3
commit
4af01de102
6 changed files with 134 additions and 86 deletions
|
@ -16,7 +16,7 @@ class App.LinkInfo extends App.Controller
|
||||||
App.Com.ajax(
|
App.Com.ajax(
|
||||||
id: 'links_' + @object.id + '_' + @object_type,
|
id: 'links_' + @object.id + '_' + @object_type,
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
url: '/links',
|
url: '/api/links',
|
||||||
data: {
|
data: {
|
||||||
link_object: @object_type,
|
link_object: @object_type,
|
||||||
link_object_value: @object.id,
|
link_object_value: @object.id,
|
||||||
|
@ -85,7 +85,7 @@ class App.LinkInfo extends App.Controller
|
||||||
App.Com.ajax(
|
App.Com.ajax(
|
||||||
id: 'links_remove_' + @object.id + '_' + @object_type,
|
id: 'links_remove_' + @object.id + '_' + @object_type,
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
url: '/links/remove',
|
url: '/api/links/remove',
|
||||||
data: {
|
data: {
|
||||||
link_type: link_type,
|
link_type: link_type,
|
||||||
link_object_source: link_object_source,
|
link_object_source: link_object_source,
|
||||||
|
@ -129,7 +129,7 @@ class App.LinkAdd extends App.ControllerModal
|
||||||
App.Com.ajax(
|
App.Com.ajax(
|
||||||
id: 'links_add_' + @object.id + '_' + @object_type,
|
id: 'links_add_' + @object.id + '_' + @object_type,
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
url: '/links/add',
|
url: '/api/links/add',
|
||||||
data: {
|
data: {
|
||||||
link_type: params['link_type'],
|
link_type: params['link_type'],
|
||||||
link_object_target: 'Ticket',
|
link_object_target: 'Ticket',
|
||||||
|
|
|
@ -171,25 +171,7 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_view (object)
|
def log_view (object)
|
||||||
history_type = History::Type.where( :name => 'viewed' ).first
|
History.log_view( object, current_user )
|
||||||
if !history_type || !history_type.id
|
|
||||||
history_type = History::Type.create(
|
|
||||||
:name => 'viewed'
|
|
||||||
)
|
|
||||||
end
|
|
||||||
history_object = History::Object.where( :name => object.class.name ).first
|
|
||||||
if !history_object || !history_object.id
|
|
||||||
history_object = History::Object.create(
|
|
||||||
:name => object.class.name
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
History.create(
|
|
||||||
:o_id => object.id,
|
|
||||||
:history_type_id => history_type.id,
|
|
||||||
:history_object_id => history_object.id,
|
|
||||||
:created_by_id => current_user.id
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def config_frontend
|
def config_frontend
|
||||||
|
|
|
@ -6,39 +6,27 @@ class History < ActiveRecord::Base
|
||||||
# before_validation :check_type, :check_object
|
# before_validation :check_type, :check_object
|
||||||
# attr_writer :history_type, :history_object
|
# attr_writer :history_type, :history_object
|
||||||
|
|
||||||
|
@@cache_type = {}
|
||||||
|
@@cache_object = {}
|
||||||
|
@@cache_attribute = {}
|
||||||
|
|
||||||
def self.history_create(data)
|
def self.history_create(data)
|
||||||
|
|
||||||
# lookups
|
# lookups
|
||||||
history_type = History::Type.where( :name => data[:history_type] ).first
|
if data[:history_type]
|
||||||
if !history_type || !history_type.id
|
history_type = self.history_type_lookup( data[:history_type] )
|
||||||
history_type = History::Type.create(
|
|
||||||
:name => data[:history_type]
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
history_object = History::Object.where( :name => data[:history_object] ).first
|
if data[:history_object]
|
||||||
if !history_object || !history_object.id
|
history_object = self.history_object_lookup( data[:history_object] )
|
||||||
history_object = History::Object.create(
|
|
||||||
:name => data[:history_object]
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
related_history_object_id = nil
|
related_history_object_id = nil
|
||||||
if data[:related_history_object]
|
if data[:related_history_object]
|
||||||
related_history_object = History::Object.where( :name => data[:related_history_object] ).first
|
related_history_object = self.history_object_lookup( data[:related_history_object] )
|
||||||
if !related_history_object || !related_history_object.id
|
|
||||||
related_history_object = History::Object.create(
|
|
||||||
:name => data[:related_history_object]
|
|
||||||
)
|
|
||||||
end
|
|
||||||
related_history_object_id = related_history_object.id
|
related_history_object_id = related_history_object.id
|
||||||
end
|
end
|
||||||
history_attribute_id = nil
|
history_attribute_id = nil
|
||||||
if data[:history_attribute]
|
if data[:history_attribute]
|
||||||
history_attribute = History::Attribute.where( :name => data[:history_attribute] ).first
|
history_attribute = self.history_attribute_lookup( data[:history_attribute] )
|
||||||
if !history_attribute || !history_attribute.object_id
|
|
||||||
history_attribute = History::Attribute.create(
|
|
||||||
:name => data[:history_attribute]
|
|
||||||
)
|
|
||||||
end
|
|
||||||
history_attribute_id = history_attribute.id
|
history_attribute_id = history_attribute.id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -59,24 +47,27 @@ class History < ActiveRecord::Base
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.history_destroy(requested_object, requested_object_id)
|
def self.history_destroy( requested_object, requested_object_id )
|
||||||
History.where( :history_object_id => History::Object.where( :name => requested_object ) ).
|
History.where( :history_object_id => History::Object.where( :name => requested_object ) ).
|
||||||
where( :o_id => requested_object_id ).
|
where( :o_id => requested_object_id ).
|
||||||
destroy_all
|
destroy_all
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.history_list(requested_object, requested_object_id, related_history_object = nil)
|
def self.history_list( requested_object, requested_object_id, related_history_object = nil )
|
||||||
if !related_history_object
|
if !related_history_object
|
||||||
history = History.where( :history_object_id => History::Object.where( :name => requested_object ) ).
|
history_object = self.history_object_lookup( requested_object )
|
||||||
|
history = History.where( :history_object_id => history_object.id ).
|
||||||
where( :o_id => requested_object_id ).
|
where( :o_id => requested_object_id ).
|
||||||
where( :history_type_id => History::Type.where( :name => ['created', 'updated', 'notification', 'email'] ) ).
|
where( :history_type_id => History::Type.where( :name => ['created', 'updated', 'notification', 'email'] ) ).
|
||||||
order('created_at ASC, id ASC')
|
order('created_at ASC, id ASC')
|
||||||
else
|
else
|
||||||
|
history_object_requested = self.history_object_lookup( requested_object )
|
||||||
|
history_object_related = self.history_object_lookup( related_history_object )
|
||||||
history = History.where(
|
history = History.where(
|
||||||
'((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? )) AND history_type_id IN (?)',
|
'((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? )) AND history_type_id IN (?)',
|
||||||
History::Object.where( :name => requested_object ).first.id,
|
history_object_requested.id,
|
||||||
requested_object_id,
|
requested_object_id,
|
||||||
History::Object.where( :name => related_history_object ).first.id,
|
history_object_related.id,
|
||||||
requested_object_id,
|
requested_object_id,
|
||||||
History::Type.where( :name => ['created', 'updated', 'notification', 'email'] )
|
History::Type.where( :name => ['created', 'updated', 'notification', 'email'] )
|
||||||
).
|
).
|
||||||
|
@ -127,8 +118,8 @@ class History < ActiveRecord::Base
|
||||||
datas = []
|
datas = []
|
||||||
stream.each do |item|
|
stream.each do |item|
|
||||||
data = item.attributes
|
data = item.attributes
|
||||||
data['history_object'] = item.history_object.name
|
data['history_object'] = self.history_object_lookup_id( data['history_object_id'] )
|
||||||
data['history_type'] = item.history_type.name
|
data['history_type'] = self.history_type_lookup_id( data['history_type_id'] )
|
||||||
data.delete('history_object_id')
|
data.delete('history_object_id')
|
||||||
data.delete('history_type_id')
|
data.delete('history_type_id')
|
||||||
datas.push data
|
datas.push data
|
||||||
|
@ -136,6 +127,7 @@ class History < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
return datas
|
return datas
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.activity_stream_fulldata( user, limit = 10 )
|
def self.activity_stream_fulldata( user, limit = 10 )
|
||||||
activity_stream = History.activity_stream( user, limit )
|
activity_stream = History.activity_stream( user, limit )
|
||||||
|
|
||||||
|
@ -188,19 +180,33 @@ class History < ActiveRecord::Base
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.log_view ( object, current_user )
|
||||||
|
history_type = self.history_type_lookup( 'viewed' )
|
||||||
|
history_object = self.history_object_lookup( object.class.name )
|
||||||
|
History.create(
|
||||||
|
:o_id => object.id,
|
||||||
|
:history_type_id => history_type.id,
|
||||||
|
:history_object_id => history_object.id,
|
||||||
|
:created_by_id => current_user.id
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
def self.recent_viewed( user, limit = 10 )
|
def self.recent_viewed( user, limit = 10 )
|
||||||
# g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
|
# g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
|
||||||
|
history_type = self.history_type_lookup( 'viewed' )
|
||||||
|
history_object = self.history_object_lookup( 'Ticket' )
|
||||||
|
|
||||||
stream = History.select("distinct(o_id), created_by_id, history_type_id, history_object_id, created_at").
|
stream = History.select("distinct(o_id), created_by_id, history_type_id, history_object_id, created_at").
|
||||||
where( :history_object_id => History::Object.where( :name => 'Ticket').first.id ).
|
where( :history_object_id => history_object.id ).
|
||||||
where( :history_type_id => History::Type.where( :name => ['viewed'] ) ).
|
where( :history_type_id => history_type.id ).
|
||||||
where( :created_by_id => user.id ).
|
where( :created_by_id => user.id ).
|
||||||
order('created_at DESC, id ASC').
|
order('created_at DESC, id ASC').
|
||||||
limit(limit)
|
limit(limit)
|
||||||
datas = []
|
datas = []
|
||||||
stream.each do |item|
|
stream.each do |item|
|
||||||
data = item.attributes
|
data = item.attributes
|
||||||
data['history_object'] = item.history_object
|
data['history_object'] = self.history_object_lookup_id( data['history_object_id'] )
|
||||||
data['history_type'] = item.history_type
|
data['history_type'] = self.history_type_lookup_id( data['history_type_id'] )
|
||||||
datas.push data
|
datas.push data
|
||||||
# item['history_attribute'] = item.history_attribute
|
# item['history_attribute'] = item.history_attribute
|
||||||
end
|
end
|
||||||
|
@ -248,27 +254,87 @@ class History < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def check_type
|
|
||||||
puts '--------------'
|
def self.history_type_lookup_id( id )
|
||||||
puts self.inspect
|
|
||||||
history_type = History::Type.where( :name => self.history_type ).first
|
# use cache
|
||||||
if !history_type || !history_type.id
|
return @@cache_type[ id ] if @@cache_type[ id ]
|
||||||
history_type = History::Type.create(
|
|
||||||
:name => self.history_type,
|
# lookup
|
||||||
:active => true
|
history_type = History::Type.find(id)
|
||||||
)
|
@@cache_type[ id ] = history_type
|
||||||
end
|
return history_type
|
||||||
self.history_type_id = history_type.id
|
|
||||||
end
|
end
|
||||||
def check_object
|
|
||||||
history_object = History::Object.where( :name => self.history_object ).first
|
def self.history_type_lookup( name )
|
||||||
if !history_object || !history_object.id
|
|
||||||
history_object = History::Object.create(
|
# use cache
|
||||||
:name => self.history_object,
|
return @@cache_type[ name ] if @@cache_type[ name ]
|
||||||
:active => true
|
|
||||||
)
|
# lookup
|
||||||
|
history_type = History::Type.where( :name => name ).first
|
||||||
|
if history_type
|
||||||
|
@@cache_type[ name ] = history_type
|
||||||
|
return history_type
|
||||||
end
|
end
|
||||||
self.history_object_id = history_object.id
|
|
||||||
|
# create
|
||||||
|
history_type = History::Type.create(
|
||||||
|
:name => name
|
||||||
|
)
|
||||||
|
@@cache_type[ name ] = history_type
|
||||||
|
return history_type
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.history_object_lookup_id( id )
|
||||||
|
|
||||||
|
# use cache
|
||||||
|
return @@cache_object[ id ] if @@cache_object[ id ]
|
||||||
|
|
||||||
|
# lookup
|
||||||
|
history_object = History::Object.find(id)
|
||||||
|
@@cache_object[ id ] = history_object
|
||||||
|
return history_object
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.history_object_lookup( name )
|
||||||
|
|
||||||
|
# use cache
|
||||||
|
return @@cache_object[ name ] if @@cache_object[ name ]
|
||||||
|
|
||||||
|
# lookup
|
||||||
|
history_object = History::Object.where( :name => name ).first
|
||||||
|
if history_object
|
||||||
|
@@cache_object[ name ] = history_object
|
||||||
|
return history_object
|
||||||
|
end
|
||||||
|
|
||||||
|
# create
|
||||||
|
history_object = History::Object.create(
|
||||||
|
:name => name
|
||||||
|
)
|
||||||
|
@@cache_object[ name ] = history_object
|
||||||
|
return history_object
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.history_attribute_lookup( name )
|
||||||
|
|
||||||
|
# use cache
|
||||||
|
return @@cache_attribute[ name ] if @@cache_attribute[ name ]
|
||||||
|
|
||||||
|
# lookup
|
||||||
|
history_attribute = History::Attribute.where( :name => name ).first
|
||||||
|
if history_attribute
|
||||||
|
@@cache_attribute[ name ] = history_attribute
|
||||||
|
return history_attribute
|
||||||
|
end
|
||||||
|
|
||||||
|
# create
|
||||||
|
history_attribute = History::Attribute.create(
|
||||||
|
:name => name
|
||||||
|
)
|
||||||
|
@@cache_attribute[ name ] = history_attribute
|
||||||
|
return history_attribute
|
||||||
end
|
end
|
||||||
|
|
||||||
class Object < ActiveRecord::Base
|
class Object < ActiveRecord::Base
|
||||||
|
|
|
@ -10,7 +10,7 @@ class Link < ActiveRecord::Base
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
Link.list(
|
links = Link.list(
|
||||||
:link_object => 'Ticket',
|
:link_object => 'Ticket',
|
||||||
:link_object_value => 1
|
:link_object_value => 1
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,9 +2,9 @@ module ExtraRoutes
|
||||||
def add(map)
|
def add(map)
|
||||||
|
|
||||||
# links
|
# links
|
||||||
map.match '/links', :to => 'links#index'
|
map.match '/api/links', :to => 'links#index'
|
||||||
map.match '/links/add', :to => 'links#add'
|
map.match '/api/links/add', :to => 'links#add'
|
||||||
map.match '/links/remove', :to => 'links#remove'
|
map.match '/api/links/remove', :to => 'links#remove'
|
||||||
|
|
||||||
end
|
end
|
||||||
module_function :add
|
module_function :add
|
||||||
|
|
Loading…
Reference in a new issue