From 3cb4bb72cd25e968efa63a80a50216583c4f4898 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 24 Aug 2014 00:29:04 +0200 Subject: [PATCH] Moved object lookup to generic model. --- app/models/application_model.rb | 57 +++++++++----- app/models/object_lookup.rb | 38 ++++++++++ app/models/recent_view.rb | 76 +++++-------------- ...date_recent_viewed_create_object_lookup.rb | 22 ++++++ test/unit/object_lookup_test.rb | 34 +++++++++ test/unit/recent_view_test.rb | 14 ++-- 6 files changed, 154 insertions(+), 87 deletions(-) create mode 100644 app/models/object_lookup.rb create mode 100644 db/migrate/20140823000001_update_recent_viewed_create_object_lookup.rb create mode 100644 test/unit/object_lookup_test.rb diff --git a/app/models/application_model.rb b/app/models/application_model.rb index aaaf4e568..6fd36e31e 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -31,6 +31,8 @@ class ApplicationModel < ActiveRecord::Base after_update :search_index_update after_destroy :search_index_destroy + after_destroy :recent_view_destroy + # create instance accessor class << self attr_accessor :activity_stream_support_config, :history_support_config, :search_index_support_config @@ -817,6 +819,27 @@ store attachments for this object end end +=begin + +return object and assets + + data = Model.full(123) + data = { + :id => 123, + :assets => assets, + } + +=end + + def self.full(id) + object = self.find(id) + assets = object.assets({}) + { + :id => id, + :assets => assets, + } + end + private def attachments_buffer @@ -848,6 +871,19 @@ store attachments for this object =begin +delete object recent viewed list, will be executed automatically + + model = Model.find(123) + model.recent_view_destroy + +=end + + def recent_view_destroy + RecentView.log_destroy( self.class.to_s, self.id ) + end + +=begin + check string/varchar size and cut them if needed =end @@ -883,25 +919,4 @@ destory object dependencies, will be executed automatically def destroy_dependencies end -=begin - -return object and assets - - data = Model.full(123) - data = { - :id => 123, - :assets => assets, - } - -=end - - def self.full(id) - object = self.find(id) - assets = object.assets({}) - { - :id => id, - :assets => assets, - } - end - end diff --git a/app/models/object_lookup.rb b/app/models/object_lookup.rb new file mode 100644 index 000000000..f32f477a4 --- /dev/null +++ b/app/models/object_lookup.rb @@ -0,0 +1,38 @@ +# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ + +class ObjectLookup < ApplicationModel + @@cache_object = {} + + def self.by_id( id ) + + # use cache + return @@cache_object[ id ] if @@cache_object[ id ] + + # lookup + object_lookup = ObjectLookup.lookup( :id => id ) + return if !object_lookup + @@cache_object[ id ] = object_lookup.name + object_lookup.name + end + + def self.by_name( name ) + + # use cache + return @@cache_object[ name ] if @@cache_object[ name ] + + # lookup + object_lookup = ObjectLookup.lookup( :name => name ) + if object_lookup + @@cache_object[ name ] = object_lookup.id + return object_lookup.id + end + + # create + object_lookup = ObjectLookup.create( + :name => name + ) + @@cache_object[ name ] = object_lookup.id + object_lookup.id + end + +end diff --git a/app/models/recent_view.rb b/app/models/recent_view.rb index c3a37ed15..963c441a9 100644 --- a/app/models/recent_view.rb +++ b/app/models/recent_view.rb @@ -1,30 +1,32 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ class RecentView < ApplicationModel - belongs_to :recent_view_object, :class_name => 'RecentView::Object' - - @@cache_object = {} + belongs_to :object_lookup, :class_name => 'ObjectLookup' def self.log( object, user ) # lookups - recent_view_object = self.object_lookup( object.class.to_s ) + object_lookup_id = ObjectLookup.by_name( object.class.to_s ) # create entry record = { - :o_id => object.id, - :recent_view_object_id => recent_view_object.id, - :created_by_id => user.id, + :o_id => object.id, + :object_lookup_id => object_lookup_id.to_i, + :created_by_id => user.id, } RecentView.create(record) end def self.log_destroy( requested_object, requested_object_id ) - RecentView.where( :recent_view_object_id => RecentView::Object.where( :name => requested_object ) ). + RecentView.where( :object_lookup_id => ObjectLookup.by_name( requested_object ) ). where( :o_id => requested_object_id ). destroy_all end + def self.user_log_destroy( user ) + RecentView.where( :created_by_id => user.id ).destroy_all + end + def self.list( user, limit = 10 ) recent_views = RecentView.where( :created_by_id => user.id ). order('created_at DESC, id DESC'). @@ -33,8 +35,8 @@ class RecentView < ApplicationModel list = [] recent_views.each { |item| data = item.attributes - data['recent_view_object'] = self.object_lookup_id( data['recent_view_object_id'] ).name - data.delete( 'history_object_id' ) + data['object'] = ObjectLookup.by_id( data['object_lookup_id'] ) + data.delete( 'object_lookup_id' ) list.push data } list @@ -48,61 +50,17 @@ class RecentView < ApplicationModel ticket_ids = [] recent_viewed.each {|item| - # load article ids - # if item.recent_view_object == 'Ticket' - ticket = Ticket.find( item['o_id'] ) - ticket_ids.push ticket.id - # end - # if item.recent_view_object 'Ticket::Article' - # tickets.push Ticket::Article.find(item.o_id) - # end - # if item.recent_view_object 'User' - # tickets.push User.find(item.o_id) - # end + # get related objects + require item['object'].to_filename + record = Kernel.const_get( item['object'] ).find( item['o_id'] ) + assets = record.assets(assets) - assets = ticket.assets(assets) } return { :recent_viewed => recent_viewed, - :ticket_ids => ticket_ids, :assets => assets, } end - - private - - def self.object_lookup_id( id ) - - # use cache - return @@cache_object[ id ] if @@cache_object[ id ] - - # lookup - history_object = RecentView::Object.lookup( :id => id ) - @@cache_object[ id ] = history_object - history_object - end - - def self.object_lookup( name ) - - # use cache - return @@cache_object[ name ] if @@cache_object[ name ] - - # lookup - recent_view_object = RecentView::Object.lookup( :name => name ) - if recent_view_object - @@cache_object[ name ] = recent_view_object - return recent_view_object - end - - # create - recent_view_object = RecentView::Object.create( - :name => name - ) - @@cache_object[ name ] = recent_view_object - recent_view_object - end - class Object < ApplicationModel end - -end +end \ No newline at end of file diff --git a/db/migrate/20140823000001_update_recent_viewed_create_object_lookup.rb b/db/migrate/20140823000001_update_recent_viewed_create_object_lookup.rb new file mode 100644 index 000000000..eeffa6e88 --- /dev/null +++ b/db/migrate/20140823000001_update_recent_viewed_create_object_lookup.rb @@ -0,0 +1,22 @@ +class UpdateRecentViewedCreateObjectLookup < ActiveRecord::Migration + def up + + create_table :object_lookups do |t| + t.column :name, :string, :limit => 250, :null => false + t.timestamps + end + add_index :object_lookups, [:name], :unique => true + RecentView.all.each {|entry| + ro = RecentView::Object.find(entry.recent_view_object_id) + lookup_id = ObjectLookup.by_name( ro.name ) + entry.update_attribute( :recent_view_object_id, lookup_id ) + entry.cache_delete + } + + rename_column :recent_views, :recent_view_object_id, :object_lookup_id + drop_table :recent_view_objects + end + + def down + end +end diff --git a/test/unit/object_lookup_test.rb b/test/unit/object_lookup_test.rb new file mode 100644 index 000000000..312a69561 --- /dev/null +++ b/test/unit/object_lookup_test.rb @@ -0,0 +1,34 @@ +# encoding: utf-8 +require 'test_helper' + +class ObjectLookupTest < ActiveSupport::TestCase + + test 'simple tests' do + + object_lookup_id = ObjectLookup.by_name( 'SomeObject' ) + assert( object_lookup_id, 'first by_name' ) + + object_lookup_name = ObjectLookup.by_id( object_lookup_id ) + assert( object_lookup_name, 'first by_id' ) + assert_equal( object_lookup_name, 'SomeObject' ) + + + object_lookup_id2 = ObjectLookup.by_name( 'Some_Object' ) + assert( object_lookup_id2, 'by_name - Some_Object' ) + + object_lookup_name2 = ObjectLookup.by_id( object_lookup_id2 ) + assert( object_lookup_name2, 'by_id - Some_Object' ) + assert_equal( object_lookup_name2, 'Some_Object' ) + + + object_lookup_id3 = ObjectLookup.by_name( 'SomeObject' ) + assert( object_lookup_id3, 'by_name 2 - SomeObject' ) + + object_lookup_name3 = ObjectLookup.by_id( object_lookup_id3 ) + assert( object_lookup_name3, 'by_id 2 - SomeObject' ) + assert_equal( object_lookup_name3, 'SomeObject' ) + assert_equal( object_lookup_name3, object_lookup_name, 'SomeObject' ) + assert_equal( object_lookup_id3, object_lookup_id, 'SomeObject' ) + + end +end diff --git a/test/unit/recent_view_test.rb b/test/unit/recent_view_test.rb index 2778aaec6..bcf5a10a5 100644 --- a/test/unit/recent_view_test.rb +++ b/test/unit/recent_view_test.rb @@ -26,7 +26,7 @@ class RecentViewTest < ActiveSupport::TestCase ) assert( ticket2, "ticket created" ) user1 = User.find(2) - #RecentView.user_log_destroy(user1) + RecentView.user_log_destroy(user1) RecentView.log( ticket1, user1 ) @@ -39,21 +39,21 @@ class RecentViewTest < ActiveSupport::TestCase list = RecentView.list( user1 ) assert( list[0]['o_id'], ticket1.id ) - assert( list[0]['recent_view_object'], 'Ticket' ) + assert( list[0]['object'], 'Ticket' ) assert( list[1]['o_id'], ticket1.id ) - assert( list[1]['recent_view_object'], 'Ticket' ) + assert( list[1]['object'], 'Ticket' ) assert( list[2]['o_id'], ticket2.id ) - assert( list[2]['recent_view_object'], 'Ticket' ) + assert( list[2]['object'], 'Ticket' ) assert( list[3]['o_id'], ticket1.id ) - assert( list[3]['recent_view_object'], 'Ticket' ) + assert( list[3]['object'], 'Ticket' ) ticket1.destroy ticket2.destroy - #list = RecentView.list( user1 ) - #assert( !list[0], 'check if recent view list is empty' ) + list = RecentView.list( user1 ) + assert( !list[0], 'check if recent view list is empty' ) end end