fix broken tests + make the editing of locales more robust

This commit is contained in:
did 2012-04-10 04:55:59 -07:00
parent e26febdaf6
commit 4d1b986f1b
14 changed files with 145 additions and 40 deletions

View File

@ -109,7 +109,7 @@ class Locomotive.Views.Pages.FormView extends Locomotive.Views.Shared.FormView
url: @$('#page_slug').attr('data-url')
type: 'get'
dataType: 'json'
data: { parent_id: @$('#page_parent_id').val(), slug: @$('#page_slug').val() }
data: { parent_id: @$('#page_parent_id').val(), slug: @$('#page_slug').val(), wildcard: @model.get('wildcard') }
success: (data) =>
@$('#page_slug_input .inline-hints').html(data.url).effect('highlight')

View File

@ -99,13 +99,13 @@ module Locomotive
Locomotive.config.multi_sites?
end
def public_page_url(page, options = {})
if content = options.delete(:content)
File.join(current_site_public_url, page.fullpath.gsub('content_type_template', ''), content._slug)
else
File.join(current_site_public_url, page.fullpath)
end
end
# def public_page_url(page, options = {})
# if content = options.delete(:content)
# File.join(current_site_public_url, page.fullpath.gsub('content_type_template', ''), content._slug)
# else
# File.join(current_site_public_url, page.fullpath)
# end
# end
# memberships

View File

@ -41,7 +41,7 @@ module Locomotive
# the current page has wildcards or not.
#
def has_wildcards?
!self.fullpath.try(:index, '*').nil?
self.wildcard? || !self.fullpath.try(:index, '*').nil?
end
# It returns a pretty output of the fullpath. The "*" characters

View File

@ -10,8 +10,12 @@ module Locomotive
## fields ##
field :locales, :type => 'RawArray', :default => []
## validations ##
validate :can_not_remove_default_locale
## callbacks ##
after_validation :add_default_locale
after_validation :add_default_locale
before_update :verify_localized_default_pages_integrity
end
@ -48,6 +52,14 @@ module Locomotive
self.locales.first || Locomotive.config.site_locales.first
end
def default_locale_was
self.locales_was.first || Locomotive.config.site_locales.first
end
def default_locale_changed?
self.default_locale != self.default_locale_was
end
def locale_fallbacks(locale)
[locale.to_s] + (locales - [locale.to_s])
end
@ -58,8 +70,49 @@ module Locomotive
self.locales = [Locomotive.config.site_locales.first] if self.locales.blank?
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
end
#
# %w(index 404).each do |slug|
# page = self.pages.where("slug.#{self.default_locale}" => slug).first
#
# self.locales.each do |locale|
# if .blank?
# page.collection.update({ :_id => page._id }, { '$set' => { "slug.#{locale}" => slug } })
# end
#
# if page.attributes['title'][locale.to_s].blank?
# page.collection.update({ :_id => page._id }, {
# '$set' => { "title.#{locale}" => }
# })
# end
# end
# end
# end

View File

@ -60,20 +60,21 @@ module Locomotive
protected
# FIXME: Currently there is no t/translate method on the I18n module
# Extensions::Site::I18n which is breaking the testing. The
# namespaced ::I18n should be changed to just I18n when the t()
# method is available
# FIXME: Currently there is no t/translate method on the
# Extensions::Site::I18n module which is breaking the testing. The
# namespaced ::I18n should be replaced by simply I18n when the t()
# method will be available.
def create_default_pages!
::Mongoid::Fields::I18n.with_locale(self.default_locale) do
%w{index 404}.each do |slug|
self.pages.create({
:slug => slug,
:title => ::I18n.t("attributes.defaults.pages.#{slug}.title"),
:raw_template => ::I18n.t("attributes.defaults.pages.#{slug}.body"),
:published => true
})
%w(index 404).each do |slug|
page = self.pages.build(:title => '', :slug => '', :raw_template => '', :published => true)
self.locales.each do |locale|
page.attributes['slug'][locale] = slug
page.attributes['title'][locale] = ::I18n.t("attributes.defaults.pages.#{slug}.title", :locale => locale)
page.attributes['raw_template'][locale] = ::I18n.t("attributes.defaults.pages.#{slug}.body", :locale => locale)
end
page.save
end
end

View File

@ -16,7 +16,7 @@
- if not @page.index? and not @page.not_found?
= f.input :parent_id, :as => :select, :collection => parent_pages_options, :include_blank => false
= f.input :slug, :required => false, :hint => @page.slug.blank? ? t('.empty_slug') : public_page_url(@page), :input_html => { :'data-url' => get_path_pages_url, :disabled => @page.index? || @page.not_found? }, :class => 'em-inline-hints' }
= f.input :slug, :required => false, :hint => @page.slug.blank? ? t('.empty_slug') : public_page_url(@page), :input_html => { :'data-url' => get_path_pages_url, :disabled => @page.index? || @page.not_found? }, :class => 'em-inline-hints'
= f.inputs :name => :seo, :class => "inputs foldable #{'folded' if inputs_folded?(@page)}" do

View File

@ -12,6 +12,8 @@ en:
extname_changed: "New file does not have the original extension"
array_too_short: "is too small (minimum element number is %{count})"
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:
liquid_syntax: "Liquid Syntax error ('%{error}' on '%{fullpath}')"
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"
array_too_short: "est trop petit (le nombre minimum d'éléments est %{count})"
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:
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"

View File

@ -24,10 +24,10 @@ module Locomotive
end
def public_page_url(page, options = {})
if content = options.delete(:content)
File.join(current_site_public_url, page.fullpath.gsub('content_type_template', ''), content._slug)
if contents = options.delete(:contents)
File.join(current_site_public_url, page.compiled_fullpath(contents))
else
File.join(current_site_public_url, page.fullpath)
File.join(current_site_public_url, page.pretty_fullpath)
end
end

View File

@ -81,6 +81,10 @@ describe 'Locomotive rendering system' do
context 'when retrieving page' do
before(:each) do
@page.fullpath = 'index' # we do not care if the fullpath is consistent or not.
end
it 'should retrieve the index page /' do
@controller.request.fullpath = '/'
@controller.current_site.pages.expects(:where).with(:depth => 0, :fullpath.in => %w{index}).returns([@page])
@ -95,13 +99,13 @@ describe 'Locomotive rendering system' do
it 'should retrieve it based on the full path' do
@controller.request.fullpath = '/about_us/team.html'
@controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{about_us/team about_us/content_type_template content_type_template/team}).returns([@page])
@controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{about_us/team about_us/* */team */*}).returns([@page])
@controller.send(:locomotive_page).should_not be_nil
end
it 'does not include the query string' do
@controller.request.fullpath = '/about_us/team.html?some=params&we=use'
@controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{about_us/team about_us/content_type_template content_type_template/team}).returns([@page])
@controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{about_us/team about_us/* */team */*}).returns([@page])
@controller.send(:locomotive_page).should_not be_nil
end
@ -118,7 +122,7 @@ describe 'Locomotive rendering system' do
@page.redirect = true
@page.redirect_url = 'http://www.example.com/'
@controller.request.fullpath = '/contact'
@controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
@controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact *}).returns([@page])
end
it 'redirects to the redirect_url' do
@ -157,7 +161,7 @@ describe 'Locomotive rendering system' do
it 'should return the 404 page if the page has not been published yet' do
@controller.request.fullpath = '/contact'
@controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
@controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact *}).returns([@page])
(klass = Locomotive::Page).expects(:published).returns([true])
@controller.current_site.pages.expects(:not_found).returns(klass)
@controller.send(:locomotive_page).should be_true
@ -166,7 +170,7 @@ describe 'Locomotive rendering system' do
it 'should not return the 404 page if the page has not been published yet and admin is logged in' do
@controller.current_locomotive_account = true
@controller.request.fullpath = '/contact'
@controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
@controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact *}).returns([@page])
@controller.send(:locomotive_page).should == @page
end

View File

@ -5,7 +5,7 @@ describe Locomotive::EditableShortText do
describe 'a simple case' do
before(:each) do
@site = FactoryGirl.create(:site)
@site = FactoryGirl.create(:site, :locales => %w(en fr))
@home = @site.pages.root.first
@home.update_attributes :raw_template => "{% block body %}{% editable_short_text 'body' %}Lorem ipsum{% endeditable_short_text %}{% endblock %}"

View File

@ -208,15 +208,15 @@ describe Locomotive::Page do
context '#path combinations' do
it 'generates them for a path depth equals to 1' do
Locomotive::Page.path_combinations('foo').should == ['foo', 'content_type_template']
Locomotive::Page.path_combinations('foo').should == ['foo', '*']
end
it 'generates them for a path depth equals to 2' do
Locomotive::Page.path_combinations('foo/bar').should == ['foo/bar', 'foo/content_type_template', 'content_type_template/bar']
Locomotive::Page.path_combinations('foo/bar').should == ['foo/bar', 'foo/*', '*/bar', '*/*']
end
it 'generates them for a path depth equals to 3' do
Locomotive::Page.path_combinations('foo/bar/baz').should == ['foo/bar/baz', 'foo/bar/content_type_template', 'foo/content_type_template/baz', 'content_type_template/bar/baz']
Locomotive::Page.path_combinations('foo/bar/baz').should == ['foo/bar/baz', 'foo/bar/*', 'foo/*/baz', 'foo/*/*', '*/bar/baz', '*/bar/*', '*/*/baz', '*/*/*']
end
end

View File

@ -1,3 +1,5 @@
# coding: utf-8
require 'spec_helper'
describe Locomotive::Site do
@ -109,11 +111,52 @@ describe Locomotive::Site do
@site = FactoryGirl.create(:site)
end
it 'should create index and 404 pages' do
it 'creates index and 404 pages' do
@site.pages.size.should == 2
@site.pages.map(&:fullpath).sort.should == %w{404 index}
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
describe 'deleting in cascade' do

View File

@ -26,7 +26,7 @@ describe Locomotive::Snippet do
context 'with a normal top level snippet' do
before :each do
@page = FactoryGirl.create(:page, :site => @site, :slug => 'my_page_here', :raw_template => "{% include 'my_test_snippet' %}")
@page = FactoryGirl.create(:page, :site => @site, :parent => @site.pages.root.first, :slug => 'my_page_here', :raw_template => "{% include 'my_test_snippet' %}")
end
it 'updates templates with the new snippet template' do
@ -39,7 +39,7 @@ describe Locomotive::Snippet do
context 'for snippets inside of a block' do
before :each do
@page = FactoryGirl.create(:page, :site => @site, :slug => 'my_page_here', :raw_template => "{% block main %}{% include 'my_test_snippet' %}{% endblock %}")
@page = FactoryGirl.create(:page, :site => @site, :parent => @site.pages.root.first, :slug => 'my_page_here', :raw_template => "{% block main %}{% include 'my_test_snippet' %}{% endblock %}")
end
it 'updates templates with the new snippet template' do
@ -54,7 +54,7 @@ describe Locomotive::Snippet do
before :each do
Mongoid::Fields::I18n.with_locale(:fr) do
@snippet = FactoryGirl.create(:snippet, :site => @site, :slug => 'my_localized_test_snippet', :template => 'a testing template')
@page = FactoryGirl.create(:page, :site => @site, :slug => 'my_localized_test_snippet', :raw_template => "{% block main %}{% include 'my_localized_test_snippet' %}{% endblock %}")
@page = FactoryGirl.create(:page, :site => @site, :parent => @site.pages.root.first, :slug => 'my_localized_test_snippet', :raw_template => "{% block main %}{% include 'my_localized_test_snippet' %}{% endblock %}")
end
end