Class: Banyan::Tasks

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/banyan/tasks.rb

Instance Method Summary collapse

Constructor Details

#initializeTasks

Returns a new instance of Tasks.



7
8
9
# File 'lib/banyan/tasks.rb', line 7

def initialize
  define_tasks
end

Instance Method Details

#define_tasksObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/banyan/tasks.rb', line 11

def define_tasks
  namespace :translations do
    namespace :categories do
      desc "export data into yml file"
      task :export, [:full_filename] => :environment do |t, args|
        args.with_defaults(full_filename: "current_translations.yml")
        filename = args[:full_filename] || 'current_translations.yml'

        hash_categorygroup_translations = {}
        I18n.available_locales.each do |l|
          cg_translations = Banyan::CategoryGroup::Translation.select('banyan_category_group_id, name').where(locale: l)
          hash_categorygroup_translations[l.to_s] = cg_translations.each_with_object({}) do |cgt,h|
            banyan_category_group = Banyan::CategoryGroup.find cgt.banyan_category_group_id
            h[banyan_category_group.tag] = cgt.name
          end
        end

        hash_category_translations = {}
        I18n.available_locales.each do |l|
          cat_translations = Banyan::Category::Translation.select('banyan_category_id, name').where(locale: l)
          hash_category_translations[l.to_s] = cat_translations.each_with_object({}) do |ct,h|
            banyan_category = Banyan::Category.find ct.banyan_category_id
            h[banyan_category.tag] = ct.name
          end
        end

        File.open("#{Rails.root}/#{filename}", 'w') do |file|
          file.puts({'banyan_category_group_translations' => hash_categorygroup_translations,
                     'banyan_category_translations'       => hash_category_translations     }.to_yaml)
        end
      end

      desc "import translations from yml file"
      task :import, [:full_filename] => :environment do |t, args|
        args.with_defaults(full_filename: "new_translations.yml")
        filename = args[:full_filename] || 'new_translations.yml'

        counter = 0
        yf = File.open("#{Rails.root}/#{filename}", "r")
        ydata = YAML.load_file(yf)

        ydata.each do |(kind,node)|
          if kind == 'banyan_category_group_translations'
            puts I18n.available_locales.inspect
            I18n.available_locales.each do |l|
              if node[l.to_s]
                hash = node[l.to_s]
                hash.each_pair do |key, value|
                  banyan_category_groups = Banyan::CategoryGroup.where(tag: key)
                  banyan_category_groups.each do |banyan_category_group|
                    banyan_category_group_translation = Array(banyan_category_group.translations.find_or_create_by_locale(l)).first
                    if banyan_category_group_translation
                      banyan_category_group_translation.attributes= {name: value}
                      if banyan_category_group_translation.changed? && banyan_category_group_translation.save!
                        counter += 1
                      end
                    end
                  end
                end
              end
            end
          elsif kind == 'banyan_category_translations'
            I18n.available_locales.each do |l|
              if node[l.to_s]
                hash = node[l.to_s]
                hash.each_pair do |key, value|
                  banyan_categories = Banyan::Category.where(tag: key)
                  banyan_categories.each do |banyan_category|
                    banyan_category_translation = Array(banyan_category.translations.find_or_create_by_locale(l)).first
                    if banyan_category_translation
                      banyan_category_translation.attributes = {name: value}
                      if banyan_category_translation.changed? && banyan_category_translation.save!
                        counter += 1
                      end
                    end
                  end
                end
              end
            end
          end
        end

        puts "Updated #{counter} translations"

      end

    end
  end
end