Refactoring: Clean up CanCreatesAndUpdates concern

This commit is contained in:
Ryan Lue 2019-03-14 19:03:20 +08:00 committed by Thorsten Eckel
parent 7700817143
commit 134dde5567

View file

@ -18,55 +18,9 @@ returns
=end =end
def create_if_not_exists(data) def create_if_not_exists(data)
if data[:id] identifier = [:id, :name, :login, :email, %i[source locale]].map { |a| data.slice(*a) }.find(&:any?) || {}
record = find_by(id: data[:id])
return record if record
elsif data[:name]
# do lookup with == to handle case insensitive databases case_sensitive_find_by(**identifier) || create(data)
records = if Rails.application.config.db_case_sensitive
where('LOWER(name) = LOWER(?)', data[:name])
else
where(name: data[:name])
end
records.each do |loop_record|
return loop_record if loop_record.name == data[:name]
end
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
records.each do |loop_record|
return loop_record if loop_record.login == data[:login]
end
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
records.each do |loop_record|
return loop_record if loop_record.email == data[:email]
end
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
records.each do |loop_record|
return loop_record if loop_record.source == data[:source]
end
end
create(data)
end end
=begin =begin
@ -116,86 +70,23 @@ returns
=end =end
def create_or_update(data) def create_or_update(data)
if data[:id] attr = (data.keys & %i[id name login email locale]).first
record = find_by(id: data[:id])
if record
record.update!(data)
return record
end
record = new(data)
record.save!
record
elsif data[:name]
# do lookup with == to handle case insensitive databases raise ArgumentError, 'Need name, login, email or locale for create_or_update()' if attr.nil?
records = if Rails.application.config.db_case_sensitive
where('LOWER(name) = LOWER(?)', data[:name])
else
where(name: data[:name])
end
records.each do |loop_record|
if loop_record.name == data[:name]
loop_record.update!(data)
return loop_record
end
end
record = new(data)
record.save!
record
elsif data[:login]
# do lookup with == to handle case insensitive databases record = case_sensitive_find_by(data.slice(attr))
records = if Rails.application.config.db_case_sensitive record.nil? ? create(data) : record.tap { |r| r.update(data) }
where('LOWER(login) = LOWER(?)', data[:login]) end
else
where(login: data[:login])
end
records.each do |loop_record|
if loop_record.login.casecmp(data[:login]).zero?
loop_record.update!(data)
return loop_record
end
end
record = new(data)
record.save!
record
elsif data[:email]
# do lookup with == to handle case insensitive databases def case_sensitive_find_by(**attrs)
records = if Rails.application.config.db_case_sensitive return nil if attrs.empty?
where('LOWER(email) = LOWER(?)', data[:email]) return find_by(**attrs) if Rails.application.config.db_case_sensitive || attrs.values.none? { |v| v.is_a?(String) }
else
where(email: data[:email])
end
records.each do |loop_record|
if loop_record.email.casecmp(data[:email]).zero?
loop_record.update!(data)
return loop_record
end
end
record = new(data)
record.save!
record
elsif data[:locale]
# do lookup with == to handle case insensitive databases where(**attrs).find { |record| record[attrs.keys.first] == attrs.values.first }
records = if Rails.application.config.db_case_sensitive
where('LOWER(locale) = LOWER(?)', data[:locale])
else
where(locale: data[:locale])
end
records.each do |loop_record|
if loop_record.locale.casecmp(data[:locale]).zero?
loop_record.update!(data)
return loop_record
end
end
record = new(data)
record.save!
record
else
raise ArgumentError, 'Need name, login, email or locale for create_or_update()'
end
end end
end end
included do
private_class_method :case_sensitive_find_by
end
end end