Method: MissingText::Writer#write_entries

Defined in:
lib/missing_text/writer.rb

#write_entries(record) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/missing_text/writer.rb', line 28

def write_entries(record)
  languages.each do |lang|
    # for each language, get the entries from the diffmap where lang is the base language
    lang_entries = diffmap.select{|k, v| k.first == lang }

    # so now we have all entires that specify the target language. That is to say, comparator will be [:lang1, :lang2] where all entries in items are in lang1 but not in lang2
    # we need to find out all the languages that the item for lang1 is missing in and write that to the db
    entry_map = {}
    lang_entries.each do |comparator, items|
      target_language = comparator.last

      items.each do |item|
        if entry_map.has_key?(item)
          entry_map[item] << target_language
        else
          entry_map[item] = [target_language]
        end
      end

    end

    # There doesn't seem to be an efficient way to do this in one giant update... if anybody knows of another way please let me know!
    MissingText::Entry.transaction{
      MissingText::Entry.create(entry_map.map do |entry, target_languages|
          {missing_text_records_id: @record.id,
            locale_code: entry.join("."),
            base_language: lang.to_s,
            base_string: get_entry_for(entry, lang),
            target_languages: target_languages}
        end)
    }
  end
end