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? raise "Can't load locales from #{url}: #{result.error}" if !result.success?
result.data.each {|locale| ActiveRecord::Base.transaction do
exists = Locale.where(locale: locale['locale']).first result.data.each {|locale|
if exists exists = Locale.where(locale: locale['locale']).first
exists.update(locale.symbolize_keys!) if exists
else exists.update(locale.symbolize_keys!)
Locale.create(locale.symbolize_keys!) else
end Locale.create(locale.symbolize_keys!)
} end
}
end
true true
end end

View file

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