2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2016-04-25 23:04:59 +00:00
|
|
|
|
|
|
|
class ExternalSync < ApplicationModel
|
|
|
|
store :last_payload
|
2017-03-09 10:32:05 +00:00
|
|
|
|
|
|
|
class << self
|
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
def changed?(object:, current_changes:, previous_changes: {})
|
2017-03-09 10:32:05 +00:00
|
|
|
changed = false
|
|
|
|
previous_changes ||= {}
|
2017-10-01 12:25:52 +00:00
|
|
|
current_changes.each do |attribute, value|
|
2017-03-09 10:32:05 +00:00
|
|
|
next if !object.attributes.key?(attribute.to_s)
|
|
|
|
next if object[attribute] == value
|
|
|
|
next if object[attribute].present? && object[attribute] != previous_changes[attribute]
|
|
|
|
|
|
|
|
begin
|
|
|
|
object[attribute] = value
|
|
|
|
changed ||= true
|
|
|
|
rescue => e
|
2020-03-12 08:23:19 +00:00
|
|
|
Rails.logger.error "Unable to assign attribute #{attribute} to object #{object.class.name}: #{e.inspect}"
|
2017-03-09 10:32:05 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-09 10:32:05 +00:00
|
|
|
changed
|
|
|
|
end
|
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
def map(source:, mapping: {})
|
2017-03-09 10:32:05 +00:00
|
|
|
|
|
|
|
information_source = if source.is_a?(Hash)
|
|
|
|
source.deep_symbolize_keys
|
|
|
|
else
|
|
|
|
source.clone
|
|
|
|
end
|
|
|
|
|
|
|
|
result = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
mapping.each do |remote_key, local_key|
|
2017-03-09 10:32:05 +00:00
|
|
|
|
|
|
|
local_key_sym = local_key.to_sym
|
|
|
|
|
|
|
|
next if result[local_key_sym].present?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-09 10:32:05 +00:00
|
|
|
value = extract(remote_key, information_source)
|
|
|
|
next if value.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-09 10:32:05 +00:00
|
|
|
result[local_key_sym] = value
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-09 10:32:05 +00:00
|
|
|
result
|
|
|
|
end
|
2020-07-20 06:19:51 +00:00
|
|
|
|
|
|
|
def migrate(object, from_id, to_id)
|
|
|
|
where(
|
|
|
|
object: object,
|
|
|
|
o_id: from_id,
|
|
|
|
).update_all( # rubocop:disable Rails/SkipsModelValidations
|
|
|
|
o_id: to_id,
|
|
|
|
)
|
|
|
|
end
|
2017-03-09 10:32:05 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def extract(remote_key, structure)
|
|
|
|
return if !structure
|
|
|
|
|
|
|
|
information_source = structure.clone
|
|
|
|
result = nil
|
|
|
|
information_path = remote_key.split('.')
|
2020-09-30 09:07:01 +00:00
|
|
|
storable_classes = %w[String Integer Float Bool Array]
|
2017-03-09 10:32:05 +00:00
|
|
|
information_path.each do |segment|
|
|
|
|
|
|
|
|
segment_sym = segment.to_sym
|
|
|
|
|
|
|
|
if information_source.is_a?(Hash)
|
|
|
|
value = information_source[segment_sym]
|
|
|
|
elsif information_source.respond_to?(segment_sym)
|
|
|
|
# prevent accessing non-attributes (e.g. destroy)
|
2020-09-30 09:07:01 +00:00
|
|
|
break if information_source.respond_to?(:attributes) && !information_source.attributes.key?(segment)
|
|
|
|
|
2017-03-09 10:32:05 +00:00
|
|
|
value = information_source.send(segment_sym)
|
|
|
|
end
|
|
|
|
break if !value
|
|
|
|
|
|
|
|
storable = value.class.ancestors.any? do |ancestor|
|
2020-09-30 09:07:01 +00:00
|
|
|
storable_classes.include?(ancestor.to_s)
|
2017-03-09 10:32:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if storable
|
|
|
|
result = value
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
information_source = value
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
2016-04-25 23:04:59 +00:00
|
|
|
end
|