2017-01-31 17:13:45 +00:00
|
|
|
# Copyright (C) 2012-2016 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)
|
|
|
|
if data[:id]
|
|
|
|
record = find_by(id: data[:id])
|
|
|
|
return record if record
|
|
|
|
elsif data[:name]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(name) = LOWER(?)', data[:name])
|
|
|
|
else
|
|
|
|
where(name: data[:name])
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
records.each do |loop_record|
|
2017-01-31 17:13:45 +00:00
|
|
|
return loop_record if loop_record.name == data[:name]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
elsif data[:login]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(login) = LOWER(?)', data[:login])
|
|
|
|
else
|
|
|
|
where(login: data[:login])
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
records.each do |loop_record|
|
2017-01-31 17:13:45 +00:00
|
|
|
return loop_record if loop_record.login == data[:login]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
elsif data[:email]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(email) = LOWER(?)', data[:email])
|
|
|
|
else
|
|
|
|
where(email: data[:email])
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
records.each do |loop_record|
|
2017-01-31 17:13:45 +00:00
|
|
|
return loop_record if loop_record.email == data[:email]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
elsif data[:locale] && data[:source]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(locale) = LOWER(?) AND LOWER(source) = LOWER(?)', data[:locale], data[:source])
|
|
|
|
else
|
|
|
|
where(locale: data[:locale], source: data[:source])
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
records.each do |loop_record|
|
2017-01-31 17:13:45 +00:00
|
|
|
return loop_record if loop_record.source == data[:source]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
create(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
Model.create_or_update with ref lookups
|
|
|
|
|
|
|
|
result = Model.create_or_update(attributes)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def create_or_update_with_ref(data)
|
|
|
|
data = association_name_to_id_convert(data)
|
|
|
|
create_or_update(data)
|
|
|
|
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
|
|
|
|
|
|
|
|
create or update model (check exists based on id, name, login, email or locale)
|
|
|
|
|
|
|
|
result = Model.create_or_update(attributes)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def create_or_update(data)
|
|
|
|
if data[:id]
|
|
|
|
record = find_by(id: data[:id])
|
|
|
|
if record
|
2017-09-11 11:16:08 +00:00
|
|
|
record.update!(data)
|
2017-01-31 17:13:45 +00:00
|
|
|
return record
|
|
|
|
end
|
|
|
|
record = new(data)
|
2017-11-23 08:09:44 +00:00
|
|
|
record.save!
|
|
|
|
record
|
2017-01-31 17:13:45 +00:00
|
|
|
elsif data[:name]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(name) = LOWER(?)', data[:name])
|
|
|
|
else
|
|
|
|
where(name: data[:name])
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
records.each do |loop_record|
|
2017-01-31 17:13:45 +00:00
|
|
|
if loop_record.name == data[:name]
|
2017-09-11 11:16:08 +00:00
|
|
|
loop_record.update!(data)
|
2017-01-31 17:13:45 +00:00
|
|
|
return loop_record
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
record = new(data)
|
2017-11-23 08:09:44 +00:00
|
|
|
record.save!
|
|
|
|
record
|
2017-01-31 17:13:45 +00:00
|
|
|
elsif data[:login]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(login) = LOWER(?)', data[:login])
|
|
|
|
else
|
|
|
|
where(login: data[:login])
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
records.each do |loop_record|
|
2017-01-31 17:13:45 +00:00
|
|
|
if loop_record.login.casecmp(data[:login]).zero?
|
2017-09-11 11:16:08 +00:00
|
|
|
loop_record.update!(data)
|
2017-01-31 17:13:45 +00:00
|
|
|
return loop_record
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
record = new(data)
|
2017-11-23 08:09:44 +00:00
|
|
|
record.save!
|
|
|
|
record
|
2017-01-31 17:13:45 +00:00
|
|
|
elsif data[:email]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(email) = LOWER(?)', data[:email])
|
|
|
|
else
|
|
|
|
where(email: data[:email])
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
records.each do |loop_record|
|
2017-01-31 17:13:45 +00:00
|
|
|
if loop_record.email.casecmp(data[:email]).zero?
|
2017-09-11 11:16:08 +00:00
|
|
|
loop_record.update!(data)
|
2017-01-31 17:13:45 +00:00
|
|
|
return loop_record
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
record = new(data)
|
2017-11-23 08:09:44 +00:00
|
|
|
record.save!
|
|
|
|
record
|
2017-01-31 17:13:45 +00:00
|
|
|
elsif data[:locale]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(locale) = LOWER(?)', data[:locale])
|
|
|
|
else
|
|
|
|
where(locale: data[:locale])
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
records.each do |loop_record|
|
2017-01-31 17:13:45 +00:00
|
|
|
if loop_record.locale.casecmp(data[:locale]).zero?
|
2017-09-11 11:16:08 +00:00
|
|
|
loop_record.update!(data)
|
2017-01-31 17:13:45 +00:00
|
|
|
return loop_record
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
record = new(data)
|
2017-11-23 08:09:44 +00:00
|
|
|
record.save!
|
|
|
|
record
|
2017-01-31 17:13:45 +00:00
|
|
|
else
|
|
|
|
raise ArgumentError, 'Need name, login, email or locale for create_or_update()'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|