Moved object lookup to generic model.

This commit is contained in:
Martin Edenhofer 2014-08-24 01:59:46 +02:00
parent bafd51e0c8
commit fe2b760612
2 changed files with 23 additions and 39 deletions

View file

@ -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

View file

@ -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