Added a redirect page extension, integrated into the application and added new tests

This commit is contained in:
Mario Visic 2011-01-06 22:48:41 +08:00
parent 8a3151cf58
commit 64eff89c97
9 changed files with 95 additions and 3 deletions

View File

@ -0,0 +1,25 @@
module Models
module Extensions
module Page
module Redirect
extend ActiveSupport::Concern
included do
field :redirect, :type => Boolean, :default => false
field :redirect_url, :type => String
validates_presence_of :redirect_url, :if => :redirect
validates_format_of :redirect_url, :with => Locomotive::Regexps::URL, :allow_blank => true
end
end
end
end
end

View File

@ -8,6 +8,7 @@ class Page
include Models::Extensions::Page::Parse
include Models::Extensions::Page::Render
include Models::Extensions::Page::Templatized
include Models::Extensions::Page::Redirect
## fields ##
field :title

View File

@ -11,7 +11,7 @@
= f.input :slug, :required => false, :hint => @page.slug.blank? ? ' ' : @page.url, :input_html => { :data_url => get_path_admin_pages_url, :disabled => @page.index? || @page.not_found? }, :wrapper_html => { :style => "#{'display: none' if @page.templatized?}" }
= f.custom_input :templatized, :css => 'toggle' do
= f.custom_input :templatized, :css => 'toggle', :style => "#{'display: none' if @page.redirect?}" do
= f.check_box :templatized
= f.input :content_type_id, :as => :select, :collection => current_site.content_types.all.to_a, :include_blank => false, :wrapper_html => { :style => "#{'display: none' unless @page.templatized?}" }
@ -19,7 +19,12 @@
= f.custom_input :published, :css => 'toggle' do
= f.check_box :published
= f.input :cache_strategy, :as => :select, :collection => options_for_page_cache_strategy, :include_blank => false
= f.custom_input :redirect, :css => 'toggle', :style => "#{'display: none' if @page.templatized?}" do
= f.check_box :redirect
= f.input :cache_strategy, :as => :select, :collection => options_for_page_cache_strategy, :include_blank => false, :wrapper_html => { :style => "#{'display: none' if @page.redirect?}" }
= f.input :redirect_url, :required => true, :wrapper_html => { :style => "#{'display: none' unless @page.redirect?}" }
= render 'editable_elements', :page => @page

View File

@ -22,7 +22,7 @@ module Locomotive
html += inline_hints_for(name, options) || ''
html += self.errors_on(name) || ''
template.content_tag(:li, template.find_and_preserve(html), :class => "#{options[:css]} #{'error' unless @object.errors[name].empty?}")
template.content_tag(:li, template.find_and_preserve(html), :style => "#{options[:style]}", :class => "#{options[:css]} #{'error' unless @object.errors[name].empty?}")
end
def inline_errors_on(method, options = nil)

View File

@ -5,5 +5,7 @@ module Locomotive
DOMAIN = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
URL = /((http|https|ftp):\/)?\/\S*/
end
end

View File

@ -13,6 +13,8 @@ module Locomotive
else
@page = locomotive_page
redirect_to(@page.redirect_url) and return if @page.present? && @page.redirect?
render_no_page_error and return if @page.nil?
output = @page.render(locomotive_context)

View File

@ -35,15 +35,31 @@ $(document).ready(function() {
$.subscribe('toggle.page_templatized.checked', function(event, data) {
$('#page_slug_input').hide();
$('#page_redirect').parent('li').hide();
$('#page_content_type_id_input').show();
}, []);
$.subscribe('toggle.page_templatized.unchecked', function(event, data) {
$('#page_slug_input').show();
$('#page_redirect').parent('li').show();
$('#page_slug').val(makeSlug($('#page_title').val())).addClass('touched');
$('#page_content_type_id_input').hide();
}, []);
// redirect feature
$.subscribe('toggle.page_redirect.checked', function(event, data) {
$('#page_templatized').parent('li').hide();
$('#page_cache_strategy_input').hide();
$('#page_redirect_url_input').show();
}, []);
$.subscribe('toggle.page_redirect.unchecked', function(event, data) {
$('#page_templatized').parent('li').show();
$('#page_cache_strategy_input').show();
$('#page_redirect_url_input').hide();
}, []);
// automatic slug from page title
$('#page_title').keypress(function() {
var input = $(this);

View File

@ -75,6 +75,22 @@ describe 'Locomotive rendering system' do
@controller.send(:locomotive_page).should be_true
end
context 'redirect page' do
before(:each) do
@page.redirect = true
@page.redirect_url = 'http://www.example.com/'
@controller.request.fullpath = '/contact'
@controller.current_site.pages.expects(:any_in).with({ :fullpath => %w{contact content_type_template} }).returns([@page])
end
it 'redirects to the redirect_url' do
@controller.expects(:redirect_to).with('http://www.example.com/').returns(true)
@controller.send(:render_locomotive_page)
end
end
context 'templatized page' do
before(:each) do

View File

@ -205,4 +205,29 @@ describe Page do
end
end
describe 'redirect extension' do
before(:each) do
@page = Factory.build(:page, :site => nil, :redirect=> true, :redirect_url => 'http://www.google.com/')
end
it 'is considered as a redirect page' do
@page.redirect?.should be_true
end
it 'validates the redirect_url if redirect is set' do
@page.redirect_url = nil
@page.should_not be_valid
@page.errors[:redirect_url].should == ["can't be blank"]
end
it 'should validate format of redirect_url' do
@page.redirect_url = "invalid url with spaces"
@page.should_not be_valid
@page.errors[:redirect_url].should == ["is invalid"]
end
end
end