Increased speed of Translation.load, just update if needed.

This commit is contained in:
Martin Edenhofer 2015-10-28 13:24:52 +01:00
parent b84047b74f
commit 0707bfa512

View file

@ -45,23 +45,32 @@ dedicated:
fail "Can't load translations from #{url}: #{result.error}" if !result.success? fail "Can't load translations from #{url}: #{result.error}" if !result.success?
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
result.data.each {|translation| result.data.each {|translation_raw|
# handle case insensitive sql # handle case insensitive sql
exists = Translation.where(locale: translation['locale'], format: translation['format'], source: translation['source']) exists = Translation.where(locale: translation_raw['locale'], format: translation_raw['format'], source: translation_raw['source'])
translaten = nil translation = nil
exists.each {|item| exists.each {|item|
if item.source == translation['source'] if item.source == translation_raw['source']
translaten = item translation = item
end end
} }
if translaten if translation
# verify if update is needed # verify if update is needed
translaten.update_attributes(translation.symbolize_keys!) update_needed = false
translaten.save translation_raw.each {|key, _value|
if translation_raw[key] == translation[key]
update_needed = true
break
end
}
if update_needed
translation.update_attributes(translation_raw.symbolize_keys!)
translation.save
end
else else
Translation.create(translation.symbolize_keys!) Translation.create(translation_raw.symbolize_keys!)
end end
} }
end end