Sha256: 97aaebaf86a9dc540007538ff0f1b1183d51bc0cff7af9351d240f2bccb0017e

Contents?: true

Size: 1.13 KB

Versions: 14

Compression:

Stored size: 1.13 KB

Contents

module Armot
  module PuretIntegration

    # It assumes all of your tables ending with _translations are puret
    # translation tables.
    def self.migrate

      black_list = ["created_at", "updated_at", "locale", "id"]

      new_i18n_translations = []
      ActiveRecord::Base.connection.tables.each do |t|
        if t =~ /(\w+)_translations$/
          model = $1
          attributes = ActiveRecord::Base.connection.columns(t).select{|x| !(black_list + ["#{model}_id"]).include?(x.name)}.map{|x| x.name}

          t.classify.constantize.all.each do |instance|

            attributes.each do |attr|
              if value = instance.send(attr)
                new_i18n_translations << I18n::Backend::ActiveRecord::Translation.new(
                          :locale => instance.locale,
                          :value => value,
                          :key => "armot.#{model.pluralize}.#{attr}.#{attr}_#{  instance.send("#{model}_id")  }")
              end
            end
          end
        end
      end

      I18n::Backend::ActiveRecord::Translation.transaction do
        new_i18n_translations.each {|x| x.save}
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
armot-0.4.0 lib/armot/puret_integration.rb
armot-0.3.4 lib/armot/puret_integration.rb
armot-0.3.3 lib/armot/puret_integration.rb
armot-0.3.2 lib/armot/puret_integration.rb
armot-0.3.1 lib/armot/puret_integration.rb
armot-0.3.0 lib/armot/puret_integration.rb
armot-0.2.5 lib/armot/puret_integration.rb
armot-0.2.4 lib/armot/puret_integration.rb
armot-0.2.3 lib/armot/puret_integration.rb
armot-0.2.2 lib/armot/puret_integration.rb
armot-0.2.1 lib/armot/puret_integration.rb
armot-0.2.0 lib/armot/puret_integration.rb
armot-0.1.1 lib/armot/puret_integration.rb
armot-0.1.0 lib/armot/puret_integration.rb