]> git.openstreetmap.org Git - rails.git/commitdiff
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 a33c76081c786ae594fe86cffea721da6406e812..7bb0b6e771a7e692aad56464ec969758524cab27 100644 (file)
@@ -3,6 +3,22 @@ class Language < ActiveRecord::Base
 
   has_many :diary_entries, :foreign_key => 'language'
 
 
   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?
   def name
     name = english_name
     name += " (#{native_name})" unless native_name.nil?
index 6b8973cf32d6c9f5fd98fa0914634603f985b8be..ab5a7eee65ed3b7122a5e52b31b99b254e2b514f 100644 (file)
@@ -10,13 +10,7 @@ class CreateLanguages < ActiveRecord::Migration
 
     add_primary_key :languages, [:code]
 
 
     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]    
 
     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")