page redirection was not really using the redirect attribute

did committed May 26, 2016
commit 21a07b0ead4d72d12ee09c70aefe00358f7e173c
Showing 7 changed files with 63 additions and 28 deletions
locomotive/steam/decorators/page_decorator.rb b/lib/locomotive/steam/decorators/page_decorator.rb +20 -0
@@ @@ -0,0 +1,20 @@
+ require_relative 'template_decorator'
+
+ module Locomotive
+ module Steam
+ module Decorators
+
+ class PageDecorator < TemplateDecorator
+
+ def redirect?
+ redirect.nil? ? redirect_url.present? : redirect
+ end
+
+ end
+
+ end
+ end
+ end
+
+
+
locomotive/steam/entities/page.rb b/lib/locomotive/steam/entities/page.rb +1 -2
@@ @@ -21,7 +21,7 @@ module Locomotive::Steam
raw_template: nil,
source: nil,
editable_elements: {},
- redirect: false,
+ redirect: nil,
redirect_url: {},
redirect_type: nil,
parent_id: nil,
@@ @@ -33,7 +33,6 @@ module Locomotive::Steam
def listed?; !!listed; end
def published?; !!published; end
def templatized?; !!templatized; end
- def redirect?; redirect.nil? ? redirect_url.present? : redirect; end
def content_type_id
self.target_klass_name =~ Locomotive::Steam::CONTENT_ENTRY_ENGINE_CLASS_NAME
locomotive/steam/middlewares/renderer.rb b/lib/locomotive/steam/middlewares/renderer.rb +1 -1
@@ @@ -16,7 +16,7 @@ module Locomotive::Steam
private
def render_page
- if page.redirect_url.presence
+ if page.redirect?
redirect_to(page.redirect_url, page.redirect_type)
else
content = parse_and_render_liquid
locomotive/steam/services/page_finder_service.rb b/lib/locomotive/steam/services/page_finder_service.rb +4 -0
@@ @@ -27,6 +27,10 @@ module Locomotive
private
+ def decorate(&block)
+ super(Decorators::PageDecorator, &block)
+ end
+
# Instead of hitting the DB each time we want a page from its handle,
# just get all the handles at once and cache the result. (up to 20% boost)
#
spec/integration/server/sitemap_spec.rb +1 -1
@@ @@ -36,7 +36,7 @@ describe Locomotive::Steam::Server do
context 'existing sitemap page' do
let(:template) { %{<?xml version="1.0" encoding="utf-8"?>OK</xml>} }
- let(:page) { instance_double('Page', liquid_source: template, templatized?: false, redirect_url: false, to_liquid: template, not_found?: false, response_type: 'application/xml') }
+ let(:page) { instance_double('Page', liquid_source: template, templatized?: false, redirect?: false, to_liquid: template, not_found?: false, response_type: 'application/xml') }
let(:env) { { 'steam.page' => page } }
it 'renders the existing sitemap page' do
spec/unit/decorators/page_decorator_spec.rb +36 -0
@@ @@ -0,0 +1,36 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Decorators::PageDecorator do
+
+ let(:redirect) { nil }
+ let(:redirect_url) { nil }
+ let(:page) { instance_double('Page', localized_attributes: [], redirect: redirect, redirect_url: redirect_url) }
+ let(:locale) { 'fr' }
+ let(:default_locale) { 'en' }
+ let(:decorated) { described_class.new(page, locale, default_locale) }
+
+ describe '#redirect?' do
+
+ subject { decorated.redirect? }
+
+ it { is_expected.to eq false }
+
+ context 'redirect_url has been set' do
+
+ let(:redirect_url) { 'http://www.google.fr' }
+
+ it { is_expected.to eq true }
+
+ context 'but redirect is set to false' do
+
+ let(:redirect) { false }
+
+ it { is_expected.to eq false }
+
+ end
+
+ end
+
+ end
+
+ end
spec/unit/entities/page_spec.rb +0 -24
@@ @@ -72,30 +72,6 @@ describe Locomotive::Steam::Page do
end
end
- describe '#redirect?' do
-
- subject { page.redirect? }
-
- it { is_expected.to eq false }
-
- context 'redirect_url has been set' do
-
- let(:attributes) { { redirect: nil, redirect_url: 'http://www.google.fr' } }
-
- it { is_expected.to eq true }
-
- context 'but redirect is set to false' do
-
- let(:attributes) { { redirect: false, redirect_url: 'http://www.google.fr' } }
-
- it { is_expected.to eq false }
-
- end
-
- end
-
- end
-
describe '#source' do
let(:attributes) { { 'raw_template' => 'template code here' } }