Add support for reloading the language table.
authorTom Hughes <tom@compton.nu>
Thu, 3 Sep 2009 21:55:42 +0000 (21:55 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 3 Sep 2009 21:55:42 +0000 (21:55 +0000)
app/models/language.rb
db/migrate/034_create_languages.rb
script/locale/reload_languages [new file with mode: 0755]

index a33c760..7bb0b6e 100644 (file)
@@ -3,6 +3,22 @@ class Language < ActiveRecord::Base
 
   has_many :diary_entries, :foreign_key => 'language'
 
+  def self.load(file)
+    Language.transaction do
+      YAML.load(File.read(file)).each do |k,v|
+        begin
+          Language.update(k, :english_name => v["english"], :native_name => v["native"])
+        rescue ActiveRecord::RecordNotFound
+          Language.create do |l|
+            l.code = k
+            l.english_name = v["english"]
+            l.native_name = v["native"]
+          end
+        end
+      end
+    end
+  end
+
   def name
     name = english_name
     name += " (#{native_name})" unless native_name.nil?
index 6b8973c..ab5a7ee 100644 (file)
@@ -10,13 +10,7 @@ class CreateLanguages < ActiveRecord::Migration
 
     add_primary_key :languages, [:code]
 
-    YAML.load(File.read(RAILS_ROOT + "/config/languages.yml")).each do |k,v|
-      Language.create do |l|
-        l.code = k
-        l.english_name = v["english"]
-        l.native_name = v["native"]
-      end
-    end
+    Language.load(RAILS_ROOT + "/config/languages.yml")
 
     add_foreign_key :users, [:locale], :languages, [:code]
     add_foreign_key :diary_entries, [:language_code], :languages, [:code]    
diff --git a/script/locale/reload_languages b/script/locale/reload_languages
new file mode 100755 (executable)
index 0000000..b4f829c
--- /dev/null
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../config/environment'
+
+Language.load(RAILS_ROOT + "/config/languages.yml")