Refactor Cti::CallerId

- execute rebuild in a single transaction
- reduce queries
- DRY up code
- satisfy RuboCop
This commit is contained in:
Julian Kornberger 2016-12-02 12:09:17 +01:00
parent 2ed9ed5897
commit 13be41c844

View file

@ -16,22 +16,17 @@ module Cti
=end
def self.maybe_add(data)
records = Cti::CallerId.where(
record = find_or_initialize_by(
caller_id: data[:caller_id],
level: data[:level],
object: data[:object],
o_id: data[:o_id],
user_id: data[:user_id],
)
return if records[0]
Cti::CallerId.create(
caller_id: data[:caller_id],
comment: data[:comment],
level: data[:level],
object: data[:object],
o_id: data[:o_id],
user_id: data[:user_id],
)
if record.new_record?
record.comment = data[:comment]
record.save!
end
end
=begin
@ -45,28 +40,11 @@ returns
=end
def self.lookup(caller_id)
result = Cti::CallerId.where(
caller_id: caller_id,
level: 'known',
).group(:user_id, :id).order(id: 'DESC').limit(20)
if !result[0]
result = Cti::CallerId.where(
caller_id: caller_id,
level: 'maybe',
).group(:user_id, :id).order(id: 'DESC').limit(20)
end
if !result[0]
result = Cti::CallerId.where(
caller_id: caller_id,
).order('id DESC').limit(20)
end
# in case do lookups in external sources
if !result[0]
# ...
end
result
where(caller_id: caller_id)
.group(:user_id, :id) \
# first known, then maybe, last others
.order("level != 'known', level != 'maybe', id DESC")
.limit(20)
end
=begin
@ -143,9 +121,9 @@ returns
=end
def self.rebuild
Cti::CallerId.delete_all
map = config
map.each { |item|
transaction do
delete_all
config.each { |item|
level = item[:level]
model = item[:model]
item[:model].find_each(batch_size: 500) do |record|
@ -153,6 +131,7 @@ returns
end
}
end
end
=begin