can_lookup.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module ApplicationModel::CanLookup
  3. extend ActiveSupport::Concern
  4. # methods defined here are going to extend the class, not the instance of it
  5. class_methods do
  6. =begin
  7. lookup model from cache (if exists) or retrieve it from db, id, name, login or email possible
  8. result = Model.lookup(id: 123)
  9. result = Model.lookup(name: 'some name')
  10. result = Model.lookup(login: 'some login')
  11. result = Model.lookup(email: 'some login')
  12. returns
  13. result = model # with all attributes
  14. =end
  15. def lookup(data)
  16. if data[:id]
  17. cache = cache_get(data[:id])
  18. return cache if cache
  19. record = find_by(id: data[:id])
  20. cache_set(data[:id], record)
  21. return record
  22. elsif data[:name]
  23. cache = cache_get(data[:name])
  24. return cache if cache
  25. # do lookup with == to handle case insensitive databases
  26. records = if Rails.application.config.db_case_sensitive
  27. where('LOWER(name) = LOWER(?)', data[:name])
  28. else
  29. where(name: data[:name])
  30. end
  31. records.each do |loop_record|
  32. next if loop_record.name != data[:name]
  33. cache_set(data[:name], loop_record)
  34. return loop_record
  35. end
  36. return
  37. elsif data[:login]
  38. cache = cache_get(data[:login])
  39. return cache if cache
  40. # do lookup with == to handle case insensitive databases
  41. records = if Rails.application.config.db_case_sensitive
  42. where('LOWER(login) = LOWER(?)', data[:login])
  43. else
  44. where(login: data[:login])
  45. end
  46. records.each do |loop_record|
  47. next if loop_record.login != data[:login]
  48. cache_set(data[:login], loop_record)
  49. return loop_record
  50. end
  51. return
  52. elsif data[:email]
  53. cache = cache_get(data[:email])
  54. return cache if cache
  55. # do lookup with == to handle case insensitive databases
  56. records = if Rails.application.config.db_case_sensitive
  57. where('LOWER(email) = LOWER(?)', data[:email])
  58. else
  59. where(email: data[:email])
  60. end
  61. records.each do |loop_record|
  62. next if loop_record.email != data[:email]
  63. cache_set(data[:email], loop_record)
  64. return loop_record
  65. end
  66. return
  67. elsif data[:number]
  68. # do lookup with == to handle case insensitive databases
  69. records = if Rails.application.config.db_case_sensitive
  70. where('LOWER(number) = LOWER(?)', data[:number])
  71. else
  72. where(number: data[:number])
  73. end
  74. records.each do |loop_record|
  75. next if loop_record.number != data[:number]
  76. return loop_record
  77. end
  78. return
  79. end
  80. raise ArgumentError, 'Need name, id, number, login or email for lookup()'
  81. end
  82. end
  83. end