From fe2b7606122c360e66f33f49eb9131a3f8e8b215 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 24 Aug 2014 01:59:46 +0200 Subject: [PATCH] Moved object lookup to generic model. --- app/models/activity_stream.rb | 46 +++---------------- ...02_update_activity_stream_object_lookup.rb | 16 +++++++ 2 files changed, 23 insertions(+), 39 deletions(-) create mode 100644 db/migrate/20140823000002_update_activity_stream_object_lookup.rb diff --git a/app/models/activity_stream.rb b/app/models/activity_stream.rb index 088c50750..9c13d562f 100644 --- a/app/models/activity_stream.rb +++ b/app/models/activity_stream.rb @@ -3,10 +3,9 @@ class ActivityStream < ApplicationModel self.table_name = 'activity_streams' belongs_to :activity_stream_type, :class_name => 'ActivityStream::Type' - belongs_to :activity_stream_object, :class_name => 'ActivityStream::Object' + belongs_to :activity_stream_object, :class_name => 'ObjectLookup' @@cache_type = {} - @@cache_object = {} =begin @@ -30,7 +29,7 @@ add a new activity entry for an object type = self.type_lookup( data[:type] ) end if data[:object] - object = self.object_lookup( data[:object] ) + object_id = ObjectLookup.by_name( data[:object] ) end role_id = nil @@ -47,7 +46,7 @@ add a new activity entry for an object :o_id => data[:o_id], # :activity_stream_type_id => type.id, :role_id => role_id, - :activity_stream_object_id => object.id, + :activity_stream_object_id => object_id, :created_by_id => data[:created_by_id] ).order('created_at DESC, id DESC').first @@ -58,7 +57,7 @@ add a new activity entry for an object record = { :o_id => data[:o_id], :activity_stream_type_id => type.id, - :activity_stream_object_id => object.id, + :activity_stream_object_id => object_id, :role_id => role_id, :group_id => data[:group_id], :created_at => data[:created_at], @@ -77,9 +76,9 @@ remove whole activity entries of an object =end def self.remove( object_name, o_id ) - object = self.object_lookup( object_name ) + object_id = ObjectLookup.by_name( object_name ) ActivityStream.where( - :activity_stream_object_id => object.id, + :activity_stream_object_id => object_id, :o_id => o_id, ).destroy_all end @@ -112,7 +111,7 @@ return all activity entries of an user list = [] stream.each do |item| data = item.attributes - data['object'] = self.object_lookup_id( data['activity_stream_object_id'] ).name + data['object'] = ObjectLookup.by_id( data['activity_stream_object_id'] ) data['type'] = self.type_lookup_id( data['activity_stream_type_id'] ).name data.delete('activity_stream_object_id') data.delete('activity_stream_type_id') @@ -154,37 +153,6 @@ return all activity entries of an user type end - def self.object_lookup_id( id ) - - # use cache - return @@cache_object[ id ] if @@cache_object[ id ] - - # lookup - object = ActivityStream::Object.lookup( :id => id ) - @@cache_object[ id ] = object - object - end - - def self.object_lookup( name ) - - # use cache - return @@cache_object[ name ] if @@cache_object[ name ] - - # lookup - object = ActivityStream::Object.lookup( :name => name ) - if object - @@cache_object[ name ] = object - return object - end - - # create - object = ActivityStream::Object.create( - :name => name - ) - @@cache_object[ name ] = object - object - end - class Object < ApplicationModel end diff --git a/db/migrate/20140823000002_update_activity_stream_object_lookup.rb b/db/migrate/20140823000002_update_activity_stream_object_lookup.rb new file mode 100644 index 000000000..069a8cfce --- /dev/null +++ b/db/migrate/20140823000002_update_activity_stream_object_lookup.rb @@ -0,0 +1,16 @@ +class UpdateActivityStreamObjectLookup < ActiveRecord::Migration + def up + + ActivityStream.all.each {|entry| + ao = ActivityStream::Object.find(entry.activity_stream_object_id) + lookup_id = ObjectLookup.by_name( ao.name ) + entry.update_attribute( :activity_stream_object_id, lookup_id ) + entry.cache_delete + } + + drop_table :activity_stream_objects + end + + def down + end +end