Small code improvements.

This commit is contained in:
Martin Edenhofer 2013-09-23 16:44:43 +02:00
parent 3b6a101c51
commit 210aa72b7c
2 changed files with 16 additions and 16 deletions

View file

@ -38,14 +38,14 @@ add a new history entry for an object
# lookups # lookups
if data[:history_type] if data[:history_type]
history_type = self.history_type_lookup( data[:history_type] ) history_type = self.type_lookup( data[:history_type] )
end end
if data[:history_object] if data[:history_object]
history_object = self.history_object_lookup( data[:history_object] ) history_object = self.object_lookup( data[:history_object] )
end end
related_history_object_id = nil related_history_object_id = nil
if data[:related_history_object] if data[:related_history_object]
related_history_object = self.history_object_lookup( data[:related_history_object] ) related_history_object = self.object_lookup( data[:related_history_object] )
related_history_object_id = related_history_object.id related_history_object_id = related_history_object.id
end end
history_attribute_id = nil history_attribute_id = nil
@ -111,14 +111,14 @@ return all histoy entries of an object
def self.list( requested_object, requested_object_id, related_history_object = nil ) def self.list( requested_object, requested_object_id, related_history_object = nil )
if !related_history_object if !related_history_object
history_object = self.history_object_lookup( requested_object ) history_object = self.object_lookup( requested_object )
history = History.where( :history_object_id => history_object.id ). history = History.where( :history_object_id => history_object.id ).
where( :o_id => requested_object_id ). where( :o_id => requested_object_id ).
where( :history_type_id => History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] ) ). where( :history_type_id => History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] ) ).
order('created_at ASC, id ASC') order('created_at ASC, id ASC')
else else
history_object_requested = self.history_object_lookup( requested_object ) history_object_requested = self.object_lookup( requested_object )
history_object_related = self.history_object_lookup( related_history_object ) history_object_related = self.object_lookup( related_history_object )
history = History.where( history = History.where(
'((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? )) AND history_type_id IN (?)', '((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? )) AND history_type_id IN (?)',
history_object_requested.id, history_object_requested.id,
@ -145,8 +145,8 @@ return all histoy entries of an object
datas = [] datas = []
stream.each do |item| stream.each do |item|
data = item.attributes data = item.attributes
data['history_object'] = self.history_object_lookup_id( data['history_object_id'] ).name data['history_object'] = self.object_lookup_id( data['history_object_id'] ).name
data['history_type'] = self.history_type_lookup_id( data['history_type_id'] ).name data['history_type'] = self.type_lookup_id( data['history_type_id'] ).name
data.delete('history_object_id') data.delete('history_object_id')
data.delete('history_type_id') data.delete('history_type_id')
datas.push data datas.push data
@ -185,7 +185,7 @@ return all histoy entries of an object
private private
def self.history_type_lookup_id( id ) def self.type_lookup_id( id )
# use cache # use cache
return @@cache_type[ id ] if @@cache_type[ id ] return @@cache_type[ id ] if @@cache_type[ id ]
@ -196,7 +196,7 @@ return all histoy entries of an object
return history_type return history_type
end end
def self.history_type_lookup( name ) def self.type_lookup( name )
# use cache # use cache
return @@cache_type[ name ] if @@cache_type[ name ] return @@cache_type[ name ] if @@cache_type[ name ]
@ -216,7 +216,7 @@ return all histoy entries of an object
return history_type return history_type
end end
def self.history_object_lookup_id( id ) def self.object_lookup_id( id )
# use cache # use cache
return @@cache_object[ id ] if @@cache_object[ id ] return @@cache_object[ id ] if @@cache_object[ id ]
@ -227,7 +227,7 @@ return all histoy entries of an object
return history_object return history_object
end end
def self.history_object_lookup( name ) def self.object_lookup( name )
# use cache # use cache
return @@cache_object[ name ] if @@cache_object[ name ] return @@cache_object[ name ] if @@cache_object[ name ]

View file

@ -8,7 +8,7 @@ class RecentView < ApplicationModel
def self.log( object, user ) def self.log( object, user )
# lookups # lookups
recent_view_object = self.recent_view_object_lookup( object.class.to_s ) recent_view_object = self.object_lookup( object.class.to_s )
# create entry # create entry
record = { record = {
@ -33,7 +33,7 @@ class RecentView < ApplicationModel
list = [] list = []
recent_views.each { |item| recent_views.each { |item|
data = item.attributes data = item.attributes
data['recent_view_object'] = self.recent_view_object_lookup_id( data['recent_view_object_id'] ).name data['recent_view_object'] = self.object_lookup_id( data['recent_view_object_id'] ).name
data.delete( 'history_object_id' ) data.delete( 'history_object_id' )
list.push data list.push data
} }
@ -71,7 +71,7 @@ class RecentView < ApplicationModel
private private
def self.recent_view_object_lookup_id( id ) def self.object_lookup_id( id )
# use cache # use cache
return @@cache_object[ id ] if @@cache_object[ id ] return @@cache_object[ id ] if @@cache_object[ id ]
@ -82,7 +82,7 @@ class RecentView < ApplicationModel
return history_object return history_object
end end
def self.recent_view_object_lookup( name ) def self.object_lookup( name )
# use cache # use cache
return @@cache_object[ name ] if @@cache_object[ name ] return @@cache_object[ name ] if @@cache_object[ name ]