Improved doc and code layout.

This commit is contained in:
Martin Edenhofer 2016-01-26 15:25:32 +01:00
parent a28f29ae3d
commit 5ed4a73d12

View file

@ -22,17 +22,17 @@ class History < ApplicationModel
add a new history entry for an object add a new history entry for an object
History.add( History.add(
:history_type => 'updated', history_type: 'updated',
:history_object => 'Ticket', history_object: 'Ticket',
:history_attribute => 'state', history_attribute: 'state',
:o_id => ticket.id, o_id: ticket.id,
:id_to => 3, id_to: 3,
:id_from => 2, id_from: 2,
:value_from => 'open', value_from: 'open',
:value_to => 'pending reminder', value_to: 'pending reminder',
:created_by_id => 1, created_by_id: 1,
:created_at => '2013-06-04 10:00:00', created_at: '2013-06-04 10:00:00',
:updated_at => '2013-06-04 10:00:00' updated_at: '2013-06-04 10:00:00'
) )
=end =end
@ -44,19 +44,19 @@ add a new history entry for an object
# lookups # lookups
if data[:history_type] if data[:history_type]
history_type = type_lookup( data[:history_type] ) history_type = type_lookup(data[:history_type])
end end
if data[:history_object] if data[:history_object]
history_object = object_lookup( data[:history_object] ) history_object = 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 = object_lookup( data[:related_history_object] ) related_history_object = 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
if data[:history_attribute] if data[:history_attribute]
history_attribute = attribute_lookup( data[:history_attribute] ) history_attribute = attribute_lookup(data[:history_attribute])
history_attribute_id = history_attribute.id history_attribute_id = history_attribute.id
end end
@ -78,7 +78,7 @@ add a new history entry for an object
} }
history_record = nil history_record = nil
if data[:id] if data[:id]
history_record = History.find_by( id: data[:id] ) history_record = History.find_by(id: data[:id])
end end
if history_record if history_record
history_record.update_attributes(record) history_record.update_attributes(record)
@ -95,12 +95,12 @@ add a new history entry for an object
remove whole history entries of an object remove whole history entries of an object
History.remove( 'Ticket', 123 ) History.remove('Ticket', 123)
=end =end
def self.remove( requested_object, requested_object_id ) def self.remove(requested_object, requested_object_id)
history_object = History::Object.find_by( name: requested_object ) history_object = History::Object.find_by(name: requested_object)
return if !history_object return if !history_object
History.where( History.where(
history_object_id: history_object.id, history_object_id: history_object.id,
@ -112,7 +112,7 @@ remove whole history entries of an object
return all history entries of an object return all history entries of an object
history_list = History.list( 'Ticket', 123 ) history_list = History.list('Ticket', 123)
returns returns
@ -125,7 +125,7 @@ returns
return all history entries of an object and it's related history objects return all history entries of an object and it's related history objects
history_list = History.list( 'Ticket', 123, true ) history_list = History.list('Ticket', 123, true)
returns returns
@ -138,26 +138,26 @@ returns
return all history entries of an object and it's assets return all history entries of an object and it's assets
history = History.list( 'Ticket', 123, nil, true ) history = History.list('Ticket', 123, nil, true)
returns returns
history = { history = {
:list => list, list: list,
:assets => assets, assets: assets,
} }
=end =end
def self.list( requested_object, requested_object_id, related_history_object = nil, assets = nil ) def self.list(requested_object, requested_object_id, related_history_object = nil, assets = nil)
if !related_history_object if !related_history_object
history_object = object_lookup( requested_object ) history_object = 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)
.order('created_at ASC, id ASC') .order('created_at ASC, id ASC')
else else
history_object_requested = object_lookup( requested_object ) history_object_requested = object_lookup(requested_object)
history_object_related = object_lookup( related_history_object ) history_object_related = 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 = ? ))', '((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? ))',
history_object_requested.id, history_object_requested.id,
@ -172,36 +172,36 @@ returns
history.each do |item| history.each do |item|
if assets if assets
asset_list = item.assets( asset_list ) asset_list = item.assets(asset_list)
end end
data = item.attributes data = item.attributes
data['object'] = object_lookup_id( data['history_object_id'] ).name data['object'] = object_lookup_id(data['history_object_id']).name
data['type'] = type_lookup_id( data['history_type_id'] ).name data['type'] = 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')
if data['history_attribute_id'] if data['history_attribute_id']
data['attribute'] = attribute_lookup_id( data['history_attribute_id'] ).name data['attribute'] = attribute_lookup_id(data['history_attribute_id']).name
end end
data.delete('history_attribute_id') data.delete('history_attribute_id')
data.delete( 'updated_at' ) data.delete('updated_at')
if data['id_to'].nil? && data['id_from'].nil? if data['id_to'].nil? && data['id_from'].nil?
data.delete( 'id_to' ) data.delete('id_to')
data.delete( 'id_from' ) data.delete('id_from')
end end
if data['value_to'].nil? && data['value_from'].nil? if data['value_to'].nil? && data['value_from'].nil?
data.delete( 'value_to' ) data.delete('value_to')
data.delete( 'value_from' ) data.delete('value_from')
end end
if !data['related_history_object_id'].nil? if !data['related_history_object_id'].nil?
data['related_object'] = object_lookup_id( data['related_history_object_id'] ).name data['related_object'] = object_lookup_id(data['related_history_object_id']).name
end end
data.delete( 'related_history_object_id' ) data.delete('related_history_object_id')
if data['related_o_id'].nil? if data['related_o_id'].nil?
data.delete( 'related_o_id' ) data.delete('related_o_id')
end end
list.push data list.push data
@ -215,24 +215,24 @@ returns
list list
end end
def self.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 ]
# lookup # lookup
history_type = History::Type.lookup( id: id ) history_type = History::Type.lookup(id: id)
@@cache_type[ id ] = history_type @@cache_type[ id ] = history_type
history_type history_type
end end
def self.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 ]
# lookup # lookup
history_type = History::Type.lookup( name: name ) history_type = History::Type.lookup(name: name)
if history_type if history_type
@@cache_type[ name ] = history_type @@cache_type[ name ] = history_type
return history_type return history_type
@ -246,24 +246,24 @@ returns
history_type history_type
end end
def self.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 ]
# lookup # lookup
history_object = History::Object.lookup( id: id ) history_object = History::Object.lookup(id: id)
@@cache_object[ id ] = history_object @@cache_object[ id ] = history_object
history_object history_object
end end
def self.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 ]
# lookup # lookup
history_object = History::Object.lookup( name: name ) history_object = History::Object.lookup(name: name)
if history_object if history_object
@@cache_object[ name ] = history_object @@cache_object[ name ] = history_object
return history_object return history_object
@ -277,24 +277,24 @@ returns
history_object history_object
end end
def self.attribute_lookup_id( id ) def self.attribute_lookup_id(id)
# use cache # use cache
return @@cache_attribute[ id ] if @@cache_attribute[ id ] return @@cache_attribute[ id ] if @@cache_attribute[ id ]
# lookup # lookup
history_attribute = History::Attribute.lookup( id: id ) history_attribute = History::Attribute.lookup(id: id)
@@cache_attribute[ id ] = history_attribute @@cache_attribute[ id ] = history_attribute
history_attribute history_attribute
end end
def self.attribute_lookup( name ) def self.attribute_lookup(name)
# use cache # use cache
return @@cache_attribute[ name ] if @@cache_attribute[ name ] return @@cache_attribute[ name ] if @@cache_attribute[ name ]
# lookup # lookup
history_attribute = History::Attribute.lookup( name: name ) history_attribute = History::Attribute.lookup(name: name)
if history_attribute if history_attribute
@@cache_attribute[ name ] = history_attribute @@cache_attribute[ name ] = history_attribute
return history_attribute return history_attribute