From 08eee6f86cfbd7d53ef31f018585b59519b8b7a1 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 12 May 2013 01:33:37 +0200 Subject: [PATCH] Moved recent viewed feature to own model. --- .../controllers/agent_ticket_zoom.js.coffee | 2 +- .../app/controllers/navigation.js.coffee | 2 +- .../app/lib/app_post/task_manager.js.coffee | 2 + app/controllers/application_controller.rb | 2 +- app/controllers/recent_viewed_controller.rb | 2 +- app/controllers/tickets_controller.rb | 2 +- app/models/history.rb | 73 ------------------- .../20130512000001_create_recent_viewed.rb | 26 +++++++ lib/session.rb | 4 +- 9 files changed, 35 insertions(+), 80 deletions(-) create mode 100644 db/migrate/20130512000001_create_recent_viewed.rb diff --git a/app/assets/javascripts/app/controllers/agent_ticket_zoom.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_zoom.js.coffee index 0b87575d6..93399684e 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_zoom.js.coffee @@ -24,7 +24,7 @@ class App.TicketZoom extends App.Controller @article_id = params.article_id @signature = undefined @signature_used = undefined - @doNotLog = 0 + @doNotLog = params['doNotLog'] || 0 @key = 'ticket::' + @ticket_id cache = App.Store.get( @key ) diff --git a/app/assets/javascripts/app/controllers/navigation.js.coffee b/app/assets/javascripts/app/controllers/navigation.js.coffee index 3335d49b8..ff4b2541d 100644 --- a/app/assets/javascripts/app/controllers/navigation.js.coffee +++ b/app/assets/javascripts/app/controllers/navigation.js.coffee @@ -288,7 +288,7 @@ class App.Navigation extends App.Controller NavBarRight['RecendViewed::' + ticket.id + '-' + prio ] = { prio: prio, parent: '#current_user', - name: item.history_object + ' (' + ticket.title + ')', + name: item.recent_view_object + ' (' + ticket.title + ')', target: '#ticket/zoom/' + ticket.id, divider: divider, navheader: navheader diff --git a/app/assets/javascripts/app/lib/app_post/task_manager.js.coffee b/app/assets/javascripts/app/lib/app_post/task_manager.js.coffee index c2ff041e0..892170bf2 100644 --- a/app/assets/javascripts/app/lib/app_post/task_manager.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/task_manager.js.coffee @@ -99,6 +99,8 @@ class _Singleton extends App.Controller params_app = _.clone(params) params_app['el'] = $('#content_permanent_' + @task_count ) params_app['task_key'] = @task_count + if to_not_show + params_app['doNotLog'] = 1 a = new App[callback]( params_app ) # remember new controller / prepare for task storage diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 54e87d9bd..791616002 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -192,7 +192,7 @@ class ApplicationController < ActionController::Base end def log_view (object) - History.log_view( object, current_user ) + RecentView.log( object, current_user ) end def config_frontend diff --git a/app/controllers/recent_viewed_controller.rb b/app/controllers/recent_viewed_controller.rb index 813622891..d1d7c3dfc 100644 --- a/app/controllers/recent_viewed_controller.rb +++ b/app/controllers/recent_viewed_controller.rb @@ -17,7 +17,7 @@ curl http://localhost/api/recent_viewed.json -v -u #{login}:#{password} -H "Cont =end def recent_viewed - recent_viewed = History.recent_viewed_fulldata( current_user, 10 ) + recent_viewed = RecentView.list_fulldata( current_user, 10 ) # return result render :json => recent_viewed diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index f6cff72a2..afdc06d52 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -294,7 +294,7 @@ class TicketsController < ApplicationController end # log object as viewed - if !params[:do_not_log] + if !params[:do_not_log] || params[:do_not_log].to_i == 0 log_view( ticket ) end diff --git a/app/models/history.rb b/app/models/history.rb index 84ed180e0..8ee634f0d 100644 --- a/app/models/history.rb +++ b/app/models/history.rb @@ -192,79 +192,6 @@ class History < ApplicationModel } 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 def self.history_type_lookup_id( id ) diff --git a/db/migrate/20130512000001_create_recent_viewed.rb b/db/migrate/20130512000001_create_recent_viewed.rb new file mode 100644 index 000000000..6e467bc1b --- /dev/null +++ b/db/migrate/20130512000001_create_recent_viewed.rb @@ -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 diff --git a/lib/session.rb b/lib/session.rb index cdd417bdd..d7f8e7697 100644 --- a/lib/session.rb +++ b/lib/session.rb @@ -352,13 +352,13 @@ class UserState # recent viewed cache_key = @cache_key + '_recent_viewed' 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 } ) self.log 'notice', 'fetch recent_viewed - ' + cache_key if recent_viewed != recent_viewed_cache 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 + '_push', recent_viewed_full ) end