fix a couple of potential bugs when playing with the site locales

This commit is contained in:
Didier Lafforgue 2012-04-13 18:32:55 +02:00
parent a507d756b7
commit 06fa564f37
4 changed files with 79 additions and 1 deletions

View File

@ -10,8 +10,12 @@ module Locomotive
## fields ## ## fields ##
field :locales, :type => 'RawArray', :default => [] field :locales, :type => 'RawArray', :default => []
## validations ##
validate :can_not_remove_default_locale
## callbacks ## ## callbacks ##
after_validation :add_default_locale after_validation :add_default_locale
before_update :verify_localized_default_pages_integrity
end end
@ -48,6 +52,10 @@ module Locomotive
self.locales.first || Locomotive.config.site_locales.first self.locales.first || Locomotive.config.site_locales.first
end end
def default_locale_was
self.locales_was.first || Locomotive.config.site_locales.first
end
def locale_fallbacks(locale) def locale_fallbacks(locale)
[locale.to_s] + (locales - [locale.to_s]) [locale.to_s] + (locales - [locale.to_s])
end end
@ -58,6 +66,29 @@ module Locomotive
self.locales = [Locomotive.config.site_locales.first] if self.locales.blank? self.locales = [Locomotive.config.site_locales.first] if self.locales.blank?
end end
def can_not_remove_default_locale
if self.persisted? && !self.locales.include?(self.default_locale_was)
self.errors.add :locales, I18n.t(:default_locale_removed, :scope => [:errors, :messages, :site])
end
end
def verify_localized_default_pages_integrity
if self.persisted? && self.locales_changed?
self.pages.where(:"slug.#{self.default_locale_was}".in => %w(index 404), :depth => 0).each do |page|
modifications = { 'title' => {}, 'slug' => {} }
self.locales.each do |locale|
slug = page.attributes['slug'][self.default_locale_was]
modifications['slug'][locale] = slug
modifications['title'][locale] = page.attributes['title'][locale] || ::I18n.t("attributes.defaults.pages.#{slug}.title", :locale => locale)
end
page.collection.update({ :_id => page._id }, { '$set' => modifications })
end
end
end
end end
end end

View File

@ -12,6 +12,8 @@ en:
extname_changed: "New file does not have the original extension" extname_changed: "New file does not have the original extension"
array_too_short: "is too small (minimum element number is %{count})" array_too_short: "is too small (minimum element number is %{count})"
invalid_theme_file: "can't be blank or isn't a zip file" invalid_theme_file: "can't be blank or isn't a zip file"
site:
default_locale_removed: The previous default locale can not be removed right away.
page: page:
liquid_syntax: "Liquid Syntax error ('%{error}' on '%{fullpath}')" liquid_syntax: "Liquid Syntax error ('%{error}' on '%{fullpath}')"
liquid_extend: "The page '%{fullpath}' extends a template which does not exist" liquid_extend: "The page '%{fullpath}' extends a template which does not exist"

View File

@ -33,6 +33,8 @@ fr:
extname_changed: "Nouveau fichier n'a pas l'extension original" extname_changed: "Nouveau fichier n'a pas l'extension original"
array_too_short: "est trop petit (le nombre minimum d'éléments est %{count})" array_too_short: "est trop petit (le nombre minimum d'éléments est %{count})"
security: "présente un problème de sécurité" security: "présente un problème de sécurité"
site:
default_locale_removed: La langue par défaut ne peut pas être supprimée de cette façon.
page: page:
liquid_syntax: "Erreur de syntaxe dans les sections de page, veuillez vérifier la syntaxe ('%{error}'/'%{fullpath}')" liquid_syntax: "Erreur de syntaxe dans les sections de page, veuillez vérifier la syntaxe ('%{error}'/'%{fullpath}')"
liquid_extend: "La page '%{fullpath}' étend le contenu d'une page qui n'existe pas" liquid_extend: "La page '%{fullpath}' étend le contenu d'une page qui n'existe pas"

View File

@ -1,3 +1,5 @@
# coding: utf-8
require 'spec_helper' require 'spec_helper'
describe Locomotive::Site do describe Locomotive::Site do
@ -114,6 +116,47 @@ describe Locomotive::Site do
@site.pages.map(&:fullpath).sort.should == %w{404 index} @site.pages.map(&:fullpath).sort.should == %w{404 index}
end end
it 'translates the index/404 pages if a new locale is added' do
@site.update_attributes :locales => %w(en fr)
@site.errors.should be_empty
::Mongoid::Fields::I18n.with_locale('fr') do
@site.pages.root.first.tap do |page|
page.title.should == "Page d'accueil"
page.slug.should == 'index'
end
@site.pages.not_found.first.tap do |page|
page.title.should == 'Page non trouvée'
page.slug.should == '404'
end
end
end
it 'translates the index/404 pages if the default locale changes' do
@site.update_attributes :locales => %w(fr en)
@site.errors.should be_empty
::Mongoid::Fields::I18n.with_locale('fr') do
@site.pages.root.first.tap do |page|
page.title.should == "Page d'accueil"
page.slug.should == 'index'
end
@site.pages.not_found.first.tap do |page|
page.title.should == 'Page non trouvée'
page.slug.should == '404'
end
end
end
it 'does not allow to remove the default locale' do
@site.update_attributes :locales => %w(fr)
@site.errors[:locales].should == ['The previous default locale can not be removed right away.']
end
end end
describe 'deleting in cascade' do describe 'deleting in cascade' do