Improved speed of .load

This commit is contained in:
Martin Edenhofer 2015-04-28 23:08:39 +02:00
parent 0e7f1bfb69
commit 049b760440
2 changed files with 30 additions and 25 deletions

View file

@ -15,14 +15,16 @@ class Locale < ApplicationModel
raise "Can't load locales from #{url}: #{result.error}" if !result.success?
result.data.each {|locale|
exists = Locale.where(locale: locale['locale']).first
if exists
exists.update(locale.symbolize_keys!)
else
Locale.create(locale.symbolize_keys!)
end
}
ActiveRecord::Base.transaction do
result.data.each {|locale|
exists = Locale.where(locale: locale['locale']).first
if exists
exists.update(locale.symbolize_keys!)
else
Locale.create(locale.symbolize_keys!)
end
}
end
true
end

View file

@ -27,26 +27,29 @@ load translations from online
}
)
raise "Can't load translations from #{url}: #{result.error}" if !result.success?
result.data.each {|translation|
#puts translation.inspect
# handle case insensitive sql
exists = Translation.where(locale: translation['locale'], format: translation['format'], source: translation['source'])
translaten = nil
exists.each {|item|
if item.source == translation['source']
translaten = item
ActiveRecord::Base.transaction do
result.data.each {|translation|
#puts translation.inspect
# handle case insensitive sql
exists = Translation.where(locale: translation['locale'], format: translation['format'], source: translation['source'])
translaten = nil
exists.each {|item|
if item.source == translation['source']
translaten = item
end
}
if translaten
# verify if update is needed
translaten.update_attributes(translation.symbolize_keys!)
translaten.save
else
Translation.create(translation.symbolize_keys!)
end
}
if translaten
# verify if update is needed
translaten.update_attributes(translation.symbolize_keys!)
translaten.save
else
Translation.create(translation.symbolize_keys!)
end
}
end
true
end