Skip to content

Instantly share code, notes, and snippets.

@charger
Created January 23, 2013 08:29
Show Gist options
  • Save charger/4603148 to your computer and use it in GitHub Desktop.
Save charger/4603148 to your computer and use it in GitHub Desktop.
TranslationProcessor
module TranslationProcessor
extend ActiveSupport::Concern
ALLOWED_FILES = ['common', "city", "energy", "farm", "market", "poker_common", "poker_mobile", "poker_flash"]
KEY_DOWNLOAD_AT = "__downloaded_at_do_not_edit__"
def parse_yml
if denied_name?
e = "#{path} was not allowed. allowed files should start with #{ALLOWED_FILES.join(",")}"
raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.translation_yml_processing_error", :e => e)
end
@result = ResultSummary.new
manipulate! do |yml|
put_keys_to_imports(yml)
puts yml
end
end
def denied_name?
fName=get_file_name
not ALLOWED_FILES.any?{|f| fName.starts_with?(f)}
end
def get_file_name
current_path.split("/").last
end
def get_file_name_with_ext
name=get_file_name
ALLOWED_FILES.each do |allowed_file_name|
if name.starts_with?(allowed_file_name)
return "#{allowed_file_name}.yml"
end
end
"unknown.yml"
end
def put_keys_to_imports yml
file_name= get_file_name_with_ext
import=Import.create!({:comment=>'none', :login=>model.owner, :enabled_languages=>'unknown', :file => file_name})
yml.each do |section, properties|
downloaded_at_by_lang=properties[KEY_DOWNLOAD_AT]
properties.delete(KEY_DOWNLOAD_AT)
properties.each do |property, languages|
languages.each do |language_code, value|
lang_id = lang_id_by_code(language_code)
downloaded_at=downloaded_at_by_lang[language_code]
ImportValue.create!({:import_id=>import.id, :section=>section, :key=>property,
:value => value, :language_id=>lang_id, :downloaded_at=>downloaded_at })
end
end
end
end
def lang_id_by_code (language_code)
@all_languages ||= Language.all.index_by(&:code)
language = @all_languages[language_code]
raise "#{language_code} translation was not found. Add this language in languages corner" unless language
language.id
end
#возвращает последнюю дату из переданной yml
def get_download_at yml
downloaded_at_properties = yml.values.collect{|x| x[KEY_DOWNLOAD_AT]}.compact
raise "__downloaded_at_do_not_edit__ attribute was not found in file" if downloaded_at_properties.size == 0
downloaded_at_properties.first.values.last
end
#----------OLD--------------OLD-----------------OLD-------------
def parse_yml_old
path = current_path.split("/").last
unless ALLOWED_FILES.any?{|f| path.starts_with?(f)}
e = "#{path} was not allowed. allowed files should start with #{ALLOWED_FILES.join(",")}"
raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.translation_yml_processing_error", :e => e)
end
ALLOWED_FILES.each do |allowed_file_name|
if path.starts_with?(allowed_file_name)
path = "#{allowed_file_name}.yml"
end
end
result = {}
result[path] = {new_keys: 0, changed_keys: 0, unchanged_keys: 0, new_translations: 0,
updated_translations: 0, unchanged_translations: 0, conflicted_translations: 0, incorrect_yml: false}
manipulate! do |yml|
Key.transaction do
Value.transaction do
downloaded_at_properties = yml.values.collect{|x| x[KEY_DOWNLOAD_AT]}.compact
raise "__downloaded_at_do_not_edit__ attribute was not found in file" if downloaded_at_properties.size == 0
downloaded_at = downloaded_at_properties.first.values.last
yml.each do |section, properties|
properties.delete(KEY_DOWNLOAD_AT)
properties.each do |property, languages|
key = Key.find_or_initialize_by_filename_and_section_and_property(path, section, property)
just_created = false
unless key.persisted?
key.save
just_created = true
end
updater = model.owner
stats = key.create_or_update_translations(languages, Time.parse(downloaded_at), updater)
result[path][:conflicted_translations] += stats[:conflicted_translations]
result[path][:unchanged_translations] += stats[:unchanged_translations]
result[path][:new_translations] += stats[:new_translations]
result[path][:updated_translations] += stats[:updated_translations]
result[path][:unchanged_keys] += 1 if (stats[:new_translations] == 0 && stats[:updated_translations] == 0)
if just_created
result[path][:new_keys] += 1
else
result[path][:changed_keys] += 1 if (stats[:new_translations] != 0 || stats[:updated_translations] != 0 )
end
end
end
end
end
end
model.report = result.to_json
model.save!
end
def manipulate!
cache_stored_file! if !cached?
yml = YAML.load(File.read current_path)
result = yield(yml)
rescue => e
# puts e.backtrace
if Rails.env.test?
# clean uploaded file if still exist after exceptions
dir = File.dirname(current_path)
FileUtils.rm_rf(dir) if File.exist?(dir)
end
raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.translation_yml_processing_error", :e => e)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment