2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-05-02 15:21:13 +00:00
|
|
|
module ApplicationModel::CanCreatesAndUpdates
|
2017-01-31 17:13:45 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# methods defined here are going to extend the class, not the instance of it
|
|
|
|
class_methods do
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
create model if not exists (check exists based on id, name, login, email or locale)
|
|
|
|
|
|
|
|
result = Model.create_if_not_exists(attributes)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def create_if_not_exists(data)
|
2019-03-14 11:03:20 +00:00
|
|
|
identifier = [:id, :name, :login, :email, %i[source locale]].map { |a| data.slice(*a) }.find(&:any?) || {}
|
|
|
|
|
|
|
|
case_sensitive_find_by(**identifier) || create(data)
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2019-03-14 11:03:44 +00:00
|
|
|
create or update model (check exists based on id, name, login, email or locale)
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
result = Model.create_or_update(attributes)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2019-03-14 11:03:44 +00:00
|
|
|
def create_or_update(data)
|
|
|
|
attr = (data.keys & %i[id name login email locale]).first
|
|
|
|
|
|
|
|
raise ArgumentError, 'Need name, login, email or locale for create_or_update()' if attr.nil?
|
|
|
|
|
|
|
|
record = case_sensitive_find_by(data.slice(attr))
|
|
|
|
record.nil? ? create(data) : record.tap { |r| r.update(data) }
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
Model.create_if_not_exists with ref lookups
|
|
|
|
|
|
|
|
result = Model.create_if_not_exists_with_ref(attributes)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def create_if_not_exists_with_ref(data)
|
|
|
|
data = association_name_to_id_convert(data)
|
|
|
|
create_or_update(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2019-03-14 11:03:44 +00:00
|
|
|
Model.create_or_update with ref lookups
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
result = Model.create_or_update(attributes)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2019-03-14 11:03:44 +00:00
|
|
|
def create_or_update_with_ref(data)
|
|
|
|
data = association_name_to_id_convert(data)
|
|
|
|
create_or_update(data)
|
2019-03-14 11:03:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def case_sensitive_find_by(**attrs)
|
|
|
|
return nil if attrs.empty?
|
2021-03-02 07:26:51 +00:00
|
|
|
return find_by(**attrs) if Rails.application.config.db_case_sensitive || attrs.values.none?(String)
|
2019-03-14 11:03:20 +00:00
|
|
|
|
|
|
|
where(**attrs).find { |record| record[attrs.keys.first] == attrs.values.first }
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
end
|
2019-03-14 11:03:20 +00:00
|
|
|
|
|
|
|
included do
|
|
|
|
private_class_method :case_sensitive_find_by
|
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|