can_creates_and_updates.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module ApplicationModel::CanCreatesAndUpdates
  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. create model if not exists (check exists based on id, name, login, email or locale)
  8. result = Model.create_if_not_exists(attributes)
  9. returns
  10. result = model # with all attributes
  11. =end
  12. def create_if_not_exists(data)
  13. if data[:id]
  14. record = find_by(id: data[:id])
  15. return record if record
  16. elsif data[:name]
  17. # do lookup with == to handle case insensitive databases
  18. records = if Rails.application.config.db_case_sensitive
  19. where('LOWER(name) = LOWER(?)', data[:name])
  20. else
  21. where(name: data[:name])
  22. end
  23. records.each do |loop_record|
  24. return loop_record if loop_record.name == data[:name]
  25. end
  26. elsif data[:login]
  27. # do lookup with == to handle case insensitive databases
  28. records = if Rails.application.config.db_case_sensitive
  29. where('LOWER(login) = LOWER(?)', data[:login])
  30. else
  31. where(login: data[:login])
  32. end
  33. records.each do |loop_record|
  34. return loop_record if loop_record.login == data[:login]
  35. end
  36. elsif data[:email]
  37. # do lookup with == to handle case insensitive databases
  38. records = if Rails.application.config.db_case_sensitive
  39. where('LOWER(email) = LOWER(?)', data[:email])
  40. else
  41. where(email: data[:email])
  42. end
  43. records.each do |loop_record|
  44. return loop_record if loop_record.email == data[:email]
  45. end
  46. elsif data[:locale] && data[:source]
  47. # do lookup with == to handle case insensitive databases
  48. records = if Rails.application.config.db_case_sensitive
  49. where('LOWER(locale) = LOWER(?) AND LOWER(source) = LOWER(?)', data[:locale], data[:source])
  50. else
  51. where(locale: data[:locale], source: data[:source])
  52. end
  53. records.each do |loop_record|
  54. return loop_record if loop_record.source == data[:source]
  55. end
  56. end
  57. create(data)
  58. end
  59. =begin
  60. Model.create_or_update with ref lookups
  61. result = Model.create_or_update(attributes)
  62. returns
  63. result = model # with all attributes
  64. =end
  65. def create_or_update_with_ref(data)
  66. data = association_name_to_id_convert(data)
  67. create_or_update(data)
  68. end
  69. =begin
  70. Model.create_if_not_exists with ref lookups
  71. result = Model.create_if_not_exists_with_ref(attributes)
  72. returns
  73. result = model # with all attributes
  74. =end
  75. def create_if_not_exists_with_ref(data)
  76. data = association_name_to_id_convert(data)
  77. create_or_update(data)
  78. end
  79. =begin
  80. create or update model (check exists based on id, name, login, email or locale)
  81. result = Model.create_or_update(attributes)
  82. returns
  83. result = model # with all attributes
  84. =end
  85. def create_or_update(data)
  86. if data[:id]
  87. record = find_by(id: data[:id])
  88. if record
  89. record.update!(data)
  90. return record
  91. end
  92. record = new(data)
  93. record.save!
  94. record
  95. elsif data[:name]
  96. # do lookup with == to handle case insensitive databases
  97. records = if Rails.application.config.db_case_sensitive
  98. where('LOWER(name) = LOWER(?)', data[:name])
  99. else
  100. where(name: data[:name])
  101. end
  102. records.each do |loop_record|
  103. if loop_record.name == data[:name]
  104. loop_record.update!(data)
  105. return loop_record
  106. end
  107. end
  108. record = new(data)
  109. record.save!
  110. record
  111. elsif data[:login]
  112. # do lookup with == to handle case insensitive databases
  113. records = if Rails.application.config.db_case_sensitive
  114. where('LOWER(login) = LOWER(?)', data[:login])
  115. else
  116. where(login: data[:login])
  117. end
  118. records.each do |loop_record|
  119. if loop_record.login.casecmp(data[:login]).zero?
  120. loop_record.update!(data)
  121. return loop_record
  122. end
  123. end
  124. record = new(data)
  125. record.save!
  126. record
  127. elsif data[:email]
  128. # do lookup with == to handle case insensitive databases
  129. records = if Rails.application.config.db_case_sensitive
  130. where('LOWER(email) = LOWER(?)', data[:email])
  131. else
  132. where(email: data[:email])
  133. end
  134. records.each do |loop_record|
  135. if loop_record.email.casecmp(data[:email]).zero?
  136. loop_record.update!(data)
  137. return loop_record
  138. end
  139. end
  140. record = new(data)
  141. record.save!
  142. record
  143. elsif data[:locale]
  144. # do lookup with == to handle case insensitive databases
  145. records = if Rails.application.config.db_case_sensitive
  146. where('LOWER(locale) = LOWER(?)', data[:locale])
  147. else
  148. where(locale: data[:locale])
  149. end
  150. records.each do |loop_record|
  151. if loop_record.locale.casecmp(data[:locale]).zero?
  152. loop_record.update!(data)
  153. return loop_record
  154. end
  155. end
  156. record = new(data)
  157. record.save!
  158. record
  159. else
  160. raise ArgumentError, 'Need name, login, email or locale for create_or_update()'
  161. end
  162. end
  163. end
  164. end