From 64eff89c97e990e83cf9f389a27102bfdd1b3f0a Mon Sep 17 00:00:00 2001 From: Mario Visic Date: Thu, 6 Jan 2011 22:48:41 +0800 Subject: [PATCH] Added a redirect page extension, integrated into the application and added new tests --- app/models/extensions/page/redirect.rb | 25 +++++++++++++++++++++++++ app/models/page.rb | 1 + app/views/admin/pages/_form.html.haml | 9 +++++++-- lib/locomotive/misc_form_builder.rb | 2 +- lib/locomotive/regexps.rb | 2 ++ lib/locomotive/render.rb | 2 ++ public/javascripts/admin/pages.js | 16 ++++++++++++++++ spec/lib/locomotive/render_spec.rb | 16 ++++++++++++++++ spec/models/page_spec.rb | 25 +++++++++++++++++++++++++ 9 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 app/models/extensions/page/redirect.rb diff --git a/app/models/extensions/page/redirect.rb b/app/models/extensions/page/redirect.rb new file mode 100644 index 00000000..147b1c99 --- /dev/null +++ b/app/models/extensions/page/redirect.rb @@ -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 + + diff --git a/app/models/page.rb b/app/models/page.rb index 130a928b..fb607a4c 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -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 diff --git a/app/views/admin/pages/_form.html.haml b/app/views/admin/pages/_form.html.haml index 5bacb3a2..3a351b3d 100644 --- a/app/views/admin/pages/_form.html.haml +++ b/app/views/admin/pages/_form.html.haml @@ -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 diff --git a/lib/locomotive/misc_form_builder.rb b/lib/locomotive/misc_form_builder.rb index 71c4d69e..5a0a9c8b 100644 --- a/lib/locomotive/misc_form_builder.rb +++ b/lib/locomotive/misc_form_builder.rb @@ -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) diff --git a/lib/locomotive/regexps.rb b/lib/locomotive/regexps.rb index d301405a..a5df8728 100644 --- a/lib/locomotive/regexps.rb +++ b/lib/locomotive/regexps.rb @@ -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 diff --git a/lib/locomotive/render.rb b/lib/locomotive/render.rb index f5dc50f6..a039f2fa 100644 --- a/lib/locomotive/render.rb +++ b/lib/locomotive/render.rb @@ -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) diff --git a/public/javascripts/admin/pages.js b/public/javascripts/admin/pages.js index 8e98e133..7df11f1d 100644 --- a/public/javascripts/admin/pages.js +++ b/public/javascripts/admin/pages.js @@ -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); diff --git a/spec/lib/locomotive/render_spec.rb b/spec/lib/locomotive/render_spec.rb index f066e3a2..cb35d5b9 100644 --- a/spec/lib/locomotive/render_spec.rb +++ b/spec/lib/locomotive/render_spec.rb @@ -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 diff --git a/spec/models/page_spec.rb b/spec/models/page_spec.rb index e3b88c04..54b1130c 100644 --- a/spec/models/page_spec.rb +++ b/spec/models/page_spec.rb @@ -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