Browse Source

Setup & test zh-tw locale fix for 2.5.0 release

Ryan Lue 6 years ago
parent
commit
1c386bc0b1

+ 38 - 0
db/migrate/20180502015927_issue_1219_zhtw_locale_typo.rb

@@ -0,0 +1,38 @@
+class Issue1219ZhtwLocaleTypo < ActiveRecord::Migration[5.1]
+  CURRENT_VERSION    = Gem::Version.new(Version.get)
+  APPLICABLE_VERSION = Gem::Version.new('2.5.0')
+
+  def up
+    return unless Setting.find_by(name: 'system_init_done')
+    return unless CURRENT_VERSION >= APPLICABLE_VERSION
+
+    if Locale.exists?(locale: 'zh-tw')
+      Locale.find_by(locale: 'zj-tw')&.destroy
+    else
+      Locale.find_by(locale: 'zj-tw')&.update(locale: 'zh-tw')
+    end
+
+    Translation.where(locale: 'zj-tw')&.update_all(locale: 'zh-tw')
+    User.where('preferences LIKE ?', "%\nlocale: zj-tw\n%").each do |u|
+      u.preferences[:locale] = 'zh-tw'
+      u.save
+    end
+  end
+
+  def down
+    return unless Setting.find_by(name: 'system_init_done')
+    return unless CURRENT_VERSION < APPLICABLE_VERSION
+
+    if Locale.exists?(locale: 'zj-tw')
+      Locale.find_by(locale: 'zh-tw')&.destroy
+    else
+      Locale.find_by(locale: 'zh-tw')&.update(locale: 'zj-tw')
+    end
+
+    Translation.where(locale: 'zh-tw')&.update_all(locale: 'zj-tw')
+    User.where('preferences LIKE ?', "%\nlocale: zh-tw\n%").each do |u|
+      u.preferences[:locale] = 'zj-tw'
+      u.save
+    end
+  end
+end

+ 4 - 13
lib/version.rb

@@ -15,18 +15,9 @@ returns
 =end
 
   def self.get
-
-    begin
-      version = File.read(Rails.root.join('VERSION'))
-      version.strip!
-    rescue => e
-      message = e.to_s
-      Rails.logger.error "VERSION file could not be read: #{message}"
-
-      version = ''
-    end
-
-    version
+    File.read(Rails.root.join('VERSION')).strip
+  rescue => e
+    Rails.logger.error "VERSION file could not be read: #{e}"
+    return ''
   end
-
 end

+ 58 - 0
spec/db/migrate/issue_1219_zhtw_locale_typo_spec.rb

@@ -0,0 +1,58 @@
+require 'rails_helper'
+
+RSpec.describe Issue1219ZhtwLocaleTypo, type: :db_migration do
+  let(:locale)      { create(:locale, locale: premigrate_locale, name: 'Chinese (Tradi.) (正體中文)') }
+  let(:translation) { create(:translation, locale: premigrate_locale) }
+  let(:user)        { create(:user, preferences: { locale: premigrate_locale }) }
+
+  before(:each) do
+    Locale.find_by(name: 'Chinese (Tradi.) (正體中文)')&.destroy
+    stub_const("#{described_class}::CURRENT_VERSION", version)
+  end
+
+  context 'upgrading to version 2.5.0+' do
+    let(:premigrate_locale) { 'zj-tw' }
+    let(:version) { Gem::Version.new('2.5.0') }
+
+    it 'corrects the zh-tw locale code' do
+      expect { migrate }
+        .to change { locale.reload.locale }
+        .from('zj-tw').to('zh-tw')
+    end
+
+    it 'updates translation records' do
+      expect { migrate }
+        .to change { translation.reload.locale }
+        .from('zj-tw').to('zh-tw')
+    end
+
+    it 'updates user records (preferences[:locale])' do
+      expect { migrate }
+        .to change { user.reload.preferences[:locale] }
+        .from('zj-tw').to('zh-tw')
+    end
+  end
+
+  context 'downgrading to version <2.5.0' do
+    let(:premigrate_locale) { 'zh-tw' }
+    let(:version) { Gem::Version.new('2.4.99') }
+
+    it 'reverts the zh-tw locale code back to zj-tw' do
+      expect { migrate(:down) }
+        .to change { locale.reload.locale }
+        .from('zh-tw').to('zj-tw')
+    end
+
+    it 'reverts translation records' do
+      expect { migrate(:down) }
+        .to change { translation.reload.locale }
+        .from('zh-tw').to('zj-tw')
+    end
+
+    it 'reverts user records (preferences[:locale])' do
+      expect { migrate(:down) }
+        .to change { user.reload.preferences[:locale] }
+        .from('zh-tw').to('zj-tw')
+    end
+  end
+end

+ 6 - 0
spec/factories/locale.rb

@@ -0,0 +1,6 @@
+FactoryBot.define do
+  factory :locale do
+    locale 'de-de'
+    name   'Deutsch'
+  end
+end

+ 9 - 0
spec/factories/translation.rb

@@ -0,0 +1,9 @@
+FactoryBot.define do
+  factory :translation do
+    locale 'de-de'
+    source 'date'
+    target 'dd/mm/yyyy'
+    created_by_id 1
+    updated_by_id 1
+  end
+end