2014-08-23 22:29:04 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class ObjectLookup < ApplicationModel
|
2015-05-08 13:47:27 +00:00
|
|
|
@@cache_object = {} # rubocop:disable Style/ClassVars
|
2014-08-23 22:29:04 +00:00
|
|
|
|
|
|
|
def self.by_id( id )
|
|
|
|
|
|
|
|
# use cache
|
|
|
|
return @@cache_object[ id ] if @@cache_object[ id ]
|
|
|
|
|
|
|
|
# lookup
|
2015-04-27 13:42:53 +00:00
|
|
|
lookup = self.lookup( id: id )
|
2014-08-24 08:04:20 +00:00
|
|
|
return if !lookup
|
|
|
|
@@cache_object[ id ] = lookup.name
|
|
|
|
lookup.name
|
2014-08-23 22:29:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.by_name( name )
|
|
|
|
|
|
|
|
# use cache
|
|
|
|
return @@cache_object[ name ] if @@cache_object[ name ]
|
|
|
|
|
|
|
|
# lookup
|
2015-04-27 13:42:53 +00:00
|
|
|
lookup = self.lookup( name: name )
|
2014-08-24 08:04:20 +00:00
|
|
|
if lookup
|
|
|
|
@@cache_object[ name ] = lookup.id
|
|
|
|
return lookup.id
|
2014-08-23 22:29:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create
|
2015-05-07 12:10:38 +00:00
|
|
|
lookup = create(
|
2015-04-27 13:42:53 +00:00
|
|
|
name: name
|
2014-08-23 22:29:04 +00:00
|
|
|
)
|
2014-08-24 08:04:20 +00:00
|
|
|
@@cache_object[ name ] = lookup.id
|
|
|
|
lookup.id
|
2014-08-23 22:29:04 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|