Moved recent viewed feature to own model.

This commit is contained in:
Martin Edenhofer 2013-05-12 01:33:37 +02:00
parent 44bef26d92
commit 08eee6f86c
9 changed files with 35 additions and 80 deletions

View file

@ -24,7 +24,7 @@ class App.TicketZoom extends App.Controller
@article_id = params.article_id @article_id = params.article_id
@signature = undefined @signature = undefined
@signature_used = undefined @signature_used = undefined
@doNotLog = 0 @doNotLog = params['doNotLog'] || 0
@key = 'ticket::' + @ticket_id @key = 'ticket::' + @ticket_id
cache = App.Store.get( @key ) cache = App.Store.get( @key )

View file

@ -288,7 +288,7 @@ class App.Navigation extends App.Controller
NavBarRight['RecendViewed::' + ticket.id + '-' + prio ] = { NavBarRight['RecendViewed::' + ticket.id + '-' + prio ] = {
prio: prio, prio: prio,
parent: '#current_user', parent: '#current_user',
name: item.history_object + ' (' + ticket.title + ')', name: item.recent_view_object + ' (' + ticket.title + ')',
target: '#ticket/zoom/' + ticket.id, target: '#ticket/zoom/' + ticket.id,
divider: divider, divider: divider,
navheader: navheader navheader: navheader

View file

@ -99,6 +99,8 @@ class _Singleton extends App.Controller
params_app = _.clone(params) params_app = _.clone(params)
params_app['el'] = $('#content_permanent_' + @task_count ) params_app['el'] = $('#content_permanent_' + @task_count )
params_app['task_key'] = @task_count params_app['task_key'] = @task_count
if to_not_show
params_app['doNotLog'] = 1
a = new App[callback]( params_app ) a = new App[callback]( params_app )
# remember new controller / prepare for task storage # remember new controller / prepare for task storage

View file

@ -192,7 +192,7 @@ class ApplicationController < ActionController::Base
end end
def log_view (object) def log_view (object)
History.log_view( object, current_user ) RecentView.log( object, current_user )
end end
def config_frontend def config_frontend

View file

@ -17,7 +17,7 @@ curl http://localhost/api/recent_viewed.json -v -u #{login}:#{password} -H "Cont
=end =end
def recent_viewed def recent_viewed
recent_viewed = History.recent_viewed_fulldata( current_user, 10 ) recent_viewed = RecentView.list_fulldata( current_user, 10 )
# return result # return result
render :json => recent_viewed render :json => recent_viewed

View file

@ -294,7 +294,7 @@ class TicketsController < ApplicationController
end end
# log object as viewed # log object as viewed
if !params[:do_not_log] if !params[:do_not_log] || params[:do_not_log].to_i == 0
log_view( ticket ) log_view( ticket )
end end

View file

@ -192,79 +192,6 @@ class History < ApplicationModel
} }
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 )
# 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").
where( :history_object_id => history_object.id ).
where( :history_type_id => history_type.id ).
where( :created_by_id => user.id ).
order('created_at DESC, id ASC').
limit(limit)
datas = []
stream.each do |item|
data = item.attributes
data['history_object'] = self.history_object_lookup_id( data['history_object_id'] ).name
data['history_type'] = self.history_type_lookup_id( data['history_type_id'] ).name
datas.push data
# item['history_attribute'] = item.history_attribute
end
# puts 'pppppppppp'
# puts datas.inspect
return datas
end
def self.recent_viewed_fulldata( user, limit = 10 )
recent_viewed = History.recent_viewed( user, limit )
# get related users
users = {}
tickets = []
recent_viewed.each {|item|
# load article ids
# if item.history_object == 'Ticket'
ticket = Ticket.find( item['o_id'] ).attributes
tickets.push ticket
# end
# if item.history_object 'Ticket::Article'
# tickets.push Ticket::Article.find(item.o_id)
# end
# if item.history_object 'User'
# tickets.push User.find(item.o_id)
# end
# load users
if !users[ ticket['owner_id'] ]
users[ ticket['owner_id'] ] = User.user_data_full( ticket['owner_id'] )
end
if !users[ ticket['created_by_id'] ]
users[ ticket['created_by_id'] ] = User.user_data_full( ticket['created_by_id'] )
end
if !users[ item['created_by_id'] ]
users[ item['created_by_id'] ] = User.user_data_full( item['created_by_id'] )
end
}
return {
:recent_viewed => recent_viewed,
:tickets => tickets,
:users => users,
}
end
private private
def self.history_type_lookup_id( id ) def self.history_type_lookup_id( id )

View file

@ -0,0 +1,26 @@
class CreateRecentViewed < ActiveRecord::Migration
def up
create_table :recent_views do |t|
t.references :recent_view_object, :null => false
t.column :o_id, :integer, :null => false
t.column :created_by_id, :integer, :null => false
t.timestamps
end
add_index :recent_views, [:o_id]
add_index :recent_views, [:created_by_id]
add_index :recent_views, [:created_at]
add_index :recent_views, [:recent_view_object_id]
create_table :recent_view_objects do |t|
t.column :name, :string, :limit => 250, :null => false
t.column :note, :string, :limit => 250, :null => true
t.timestamps
end
add_index :recent_view_objects, [:name], :unique => true
end
def down
drop_table :recent_views
drop_table :recent_view_objects
end
end

View file

@ -352,13 +352,13 @@ class UserState
# recent viewed # recent viewed
cache_key = @cache_key + '_recent_viewed' cache_key = @cache_key + '_recent_viewed'
if CacheIn.expired(cache_key) if CacheIn.expired(cache_key)
recent_viewed = History.recent_viewed( user ) recent_viewed = RecentView.list_fulldata( user, 10 )
recent_viewed_cache = CacheIn.get( cache_key, { :re_expire => true } ) recent_viewed_cache = CacheIn.get( cache_key, { :re_expire => true } )
self.log 'notice', 'fetch recent_viewed - ' + cache_key self.log 'notice', 'fetch recent_viewed - ' + cache_key
if recent_viewed != recent_viewed_cache if recent_viewed != recent_viewed_cache
self.log 'notify', 'fetch recent_viewed changed - ' + cache_key self.log 'notify', 'fetch recent_viewed changed - ' + cache_key
recent_viewed_full = History.recent_viewed_fulldata( user ) recent_viewed_full = RecentView.list_fulldata( user, 10 )
CacheIn.set( cache_key, recent_viewed, { :expires_in => 5.seconds } ) CacheIn.set( cache_key, recent_viewed, { :expires_in => 5.seconds } )
CacheIn.set( cache_key + '_push', recent_viewed_full ) CacheIn.set( cache_key + '_push', recent_viewed_full )
end end