FIX: Wrap custom fields database statements in a transaction.
Kind of strange that we don't do it because a database statement may fail and leave us in a weird state.
This commit is contained in:
parent
287e780f22
commit
22408f93c9
|
@ -194,54 +194,55 @@ module HasCustomFields
|
||||||
def save_custom_fields(force = false)
|
def save_custom_fields(force = false)
|
||||||
if force || !custom_fields_clean?
|
if force || !custom_fields_clean?
|
||||||
dup = @custom_fields.dup
|
dup = @custom_fields.dup
|
||||||
|
|
||||||
array_fields = {}
|
array_fields = {}
|
||||||
|
|
||||||
_custom_fields.reload.each do |f|
|
ActiveRecord::Base.transaction do
|
||||||
if dup[f.name].is_a?(Array)
|
_custom_fields.reload.each do |f|
|
||||||
# we need to collect Arrays fully before we can compare them
|
if dup[f.name].is_a?(Array)
|
||||||
if !array_fields.has_key?(f.name)
|
# we need to collect Arrays fully before we can compare them
|
||||||
array_fields[f.name] = [f]
|
if !array_fields.has_key?(f.name)
|
||||||
|
array_fields[f.name] = [f]
|
||||||
|
else
|
||||||
|
array_fields[f.name] << f
|
||||||
|
end
|
||||||
|
elsif dup[f.name].is_a?(Hash)
|
||||||
|
if dup[f.name].to_json != f.value
|
||||||
|
f.destroy!
|
||||||
|
else
|
||||||
|
dup.delete(f.name)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
array_fields[f.name] << f
|
t = {}
|
||||||
end
|
self.class.append_custom_field(t, f.name, f.value)
|
||||||
elsif dup[f.name].is_a? Hash
|
|
||||||
if dup[f.name].to_json != f.value
|
|
||||||
f.destroy!
|
|
||||||
else
|
|
||||||
dup.delete(f.name)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
t = {}
|
|
||||||
self.class.append_custom_field(t, f.name, f.value)
|
|
||||||
|
|
||||||
if dup[f.name] != t[f.name]
|
if dup[f.name] != t[f.name]
|
||||||
f.destroy!
|
f.destroy!
|
||||||
else
|
else
|
||||||
dup.delete(f.name)
|
dup.delete(f.name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# let's iterate through our arrays and compare them
|
# let's iterate through our arrays and compare them
|
||||||
array_fields.each do |field_name, fields|
|
array_fields.each do |field_name, fields|
|
||||||
if fields.length == dup[field_name].length && fields.map(&:value) == dup[field_name]
|
if fields.length == dup[field_name].length && fields.map(&:value) == dup[field_name]
|
||||||
dup.delete(field_name)
|
dup.delete(field_name)
|
||||||
else
|
else
|
||||||
fields.each(&:destroy)
|
fields.each(&:destroy!)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
dup.each do |k, v|
|
dup.each do |k, v|
|
||||||
field_type = self.class.get_custom_field_type(k)
|
field_type = self.class.get_custom_field_type(k)
|
||||||
|
|
||||||
if v.is_a?(Array) && field_type != :json
|
if v.is_a?(Array) && field_type != :json
|
||||||
v.each { |subv| _custom_fields.create!(name: k, value: subv) }
|
v.each { |subv| _custom_fields.create!(name: k, value: subv) }
|
||||||
else
|
else
|
||||||
_custom_fields.create!(
|
_custom_fields.create!(
|
||||||
name: k,
|
name: k,
|
||||||
value: v.is_a?(Hash) || field_type == :json ? v.to_json : v
|
value: v.is_a?(Hash) || field_type == :json ? v.to_json : v
|
||||||
)
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue