2017-01-31 17:13:45 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
module ApplicationModel::CanLookup
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
lookup model from cache (if exists) or retrieve it from db, id, name, login or email possible
|
|
|
|
|
|
|
|
result = Model.lookup(id: 123)
|
|
|
|
result = Model.lookup(name: 'some name')
|
|
|
|
result = Model.lookup(login: 'some login')
|
|
|
|
result = Model.lookup(email: 'some login')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-07-04 12:57:10 +00:00
|
|
|
def lookup(**attr)
|
2018-11-06 05:42:52 +00:00
|
|
|
raise ArgumentError, "Multiple lookup attributes given (#{attr.keys.join(', ')}), only support (#{lookup_keys.join(', ')})" if attr.many?
|
|
|
|
|
2018-07-04 12:57:10 +00:00
|
|
|
attr.transform_keys!(&:to_sym).slice!(*lookup_keys)
|
|
|
|
raise ArgumentError, "Valid lookup attribute required (#{lookup_keys.join(', ')})" if attr.empty?
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2019-06-28 11:38:49 +00:00
|
|
|
cache_get(attr.values.first) || find_and_save_to_cache_by(attr)
|
2018-07-04 12:57:10 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2018-11-06 05:42:52 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
return possible lookup keys for model
|
|
|
|
|
|
|
|
result = Model.lookup_keys
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
[:id, :name] # or, for users: [:id, :login, :email]
|
2018-11-06 05:42:52 +00:00
|
|
|
|
|
|
|
=end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2018-07-04 12:57:10 +00:00
|
|
|
def lookup_keys
|
|
|
|
@lookup_keys ||= %i[id name login email number] & attribute_names.map(&:to_sym)
|
|
|
|
end
|
2018-02-20 04:29:30 +00:00
|
|
|
|
2018-11-06 05:42:52 +00:00
|
|
|
private
|
|
|
|
|
2018-07-04 12:57:10 +00:00
|
|
|
def find_and_save_to_cache_by(attr)
|
2019-10-21 10:44:52 +00:00
|
|
|
record = find_by(attr)
|
|
|
|
return nil if string_key?(attr.keys.first) && (record&.send(attr.keys.first) != attr.values.first) # enforce case-sensitivity on MySQL
|
|
|
|
return record if ActiveRecord::Base.connection.transaction_open? # rollbacks can invalidate cache entries
|
|
|
|
|
|
|
|
cache_set(attr.values.first, record)
|
|
|
|
record
|
2018-07-04 12:57:10 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2018-07-04 12:57:10 +00:00
|
|
|
def string_key?(key)
|
|
|
|
type_for_attribute(key.to_s).type == :string
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|