the import module takes care of redirection pages (+ tests)

This commit is contained in:
dinedine 2011-01-10 14:57:44 +01:00
parent 2adb950ac3
commit 890d5d7e18
4 changed files with 33 additions and 5 deletions

View File

@ -15,10 +15,21 @@ module Locomotive
self.add_page(fullpath)
end
# make sure all the pages were processed (redirection pages without template for instance)
self.pages.each { |fullpath, attributes| self.add_page_without_template(fullpath.to_s) }
end
protected
def add_page_without_template(fullpath)
page = context[:done][fullpath]
return page if page # already added, so skip it
self._save_page!(fullpath, nil)
end
def add_page(fullpath)
page = context[:done][fullpath]
@ -30,6 +41,10 @@ module Locomotive
self.build_parent_template(template)
self._save_page!(fullpath, template)
end
def _save_page!(fullpath, template)
parent = self.find_parent(fullpath)
attributes = {
@ -42,20 +57,22 @@ module Locomotive
# templatized ?
if content_type_slug = attributes.delete(:content_type)
fullpath.gsub!(/\/template$/, '/content_type_template')
attributes.merge!({
:templatized => true,
:content_type => site.content_types.where(:slug => content_type_slug).first
})
end
page = site.pages.where(:fullpath => fullpath).first || site.pages.build
# redirection page ?
attributes[:redirect] = true if attributes[:redirect_url].present?
page = site.pages.where(:fullpath => self.sanitize_fullpath(fullpath)).first || site.pages.build
page.attributes = attributes
page.save!
self.log "adding #{page.fullpath} / #{page.position}"
self.log "adding #{page.fullpath} (#{template.blank? ? 'without' : 'with'} template) / #{page.position}"
site.reload
@ -162,6 +179,10 @@ module Locomotive
pages
end
def sanitize_fullpath(fullpath)
fullpath.gsub(/\/template$/, '/content_type_template')
end
end
end
end

View File

@ -26,7 +26,8 @@ $(document).ready(function() {
params += '&_method=put';
$.post($(this).attr('data_url'), params, function(data) {
$.growl('success', data.notice);
var error = typeof(data.error) != 'undefined';
$.growl((error ? 'error' : 'success'), (error ? data.error : data.notice));
}, 'json');
}
});

Binary file not shown.

View File

@ -53,7 +53,7 @@ describe Locomotive::Import::Job do
end
it 'inserts all the pages' do
@site.pages.count.should == 8
@site.pages.count.should == 9
end
it 'inserts the index and 404 pages' do
@ -67,6 +67,12 @@ describe Locomotive::Import::Job do
page.fullpath.should == 'portfolio/content_type_template'
end
it 'inserts redirection page' do
page = @site.pages.where(:redirect => true).first
page.should_not be_nil
page.redirect_url.should == 'http://blog.locomotivecms.com'
end
after(:all) do
Site.destroy_all
end