implement the redirection page (+ specs)

did committed Feb 17, 2015
commit 9c9682866dec2fc4838d68293fac50545b53bbfa
Showing 8 changed files with 44 additions and 83 deletions
locomotive/steam/middlewares/renderer.rb b/lib/locomotive/steam/middlewares/renderer.rb +6 -3
@@ @@ -16,9 +16,12 @@ module Locomotive::Steam
private
def render_page
- # TODO: redirection
- content = parse_and_render_liquid
- render_response(content, page.not_found? ? 404: 200)
+ if page.redirect_url
+ redirect_to(page.redirect_url, page.redirect_type)
+ else
+ content = parse_and_render_liquid
+ render_response(content, page.not_found? ? 404: 200)
+ end
end
def render_missing_404
locomotive/steam/repositories/filesystem/models/page.rb b/lib/locomotive/steam/repositories/filesystem/models/page.rb +3 -2
@@ @@ -6,7 +6,7 @@ module Locomotive
class Page < Base
- set_localized_attributes [:title, :slug, :permalink, :editable_elements, :template, :template_path, :fullpath, :seo_title, :meta_description, :meta_keywords]
+ set_localized_attributes [:title, :slug, :permalink, :editable_elements, :template, :template_path, :redirect_url, :fullpath, :seo_title, :meta_description, :meta_keywords]
attr_accessor :depth, :_fullpath, :content_entry
@@ @@ -19,7 +19,8 @@ module Locomotive
content_type: nil,
position: 99,
template: {},
- editable_elements: {}
+ editable_elements: {},
+ redirect_url: {}
}.merge(attributes))
end
locomotive/steam/repositories/filesystem/sanitizers/page.rb b/lib/locomotive/steam/repositories/filesystem/sanitizers/page.rb +7 -0
@@ @@ -20,6 +20,7 @@ module Locomotive
modify_if_templatized(page, locale)
build_editable_elements(page, locale)
use_default_locale_template_path(page, locale)
+ set_default_redirect_type(page, locale)
end
end
end
@@ @@ -35,6 +36,12 @@ module Locomotive
end
end
+ def set_default_redirect_type(page, locale)
+ if page.redirect_url[locale]
+ page.attributes[:redirect_type] ||= 301
+ end
+ end
+
def build_editable_elements(page, locale)
elements = page.editable_elements[locale] || {}
elements.stringify_keys!
locomotive/steam/repositories/filesystem/yaml_loaders/page.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/page.rb +2 -0
@@ @@ -42,6 +42,7 @@ module Locomotive
slug: { locale => attributes.delete(:slug) || slug },
editable_elements: { locale => attributes.delete(:editable_elements) },
template_path: { locale => template_path(filepath, attributes, locale) },
+ redirect_url: { locale => attributes.delete(:redirect_url) },
_fullpath: fullpath
}.merge(attributes)
end
@@ @@ -54,6 +55,7 @@ module Locomotive
leaf[:slug][locale] = attributes.delete(:slug) || slug
leaf[:editable_elements][locale] = attributes.delete(:editable_elements)
leaf[:template_path][locale] = template_path(filepath, attributes, locale)
+ leaf[:redirect_url][locale] = attributes.delete(:redirect_url)
leaf.merge!(attributes)
end
spec/fixtures/default/app/views/pages/store.liquid +2 -1
@@ @@ -1,5 +1,6 @@
---
redirect_url: http://www.apple.com/en/itunes/
+ redirect_type: 301
listed: true
position: 3
- ---
\ No newline at end of file
+ ---
spec/integration/server/basic_spec.rb +24 -0
@@ @@ -20,6 +20,30 @@ describe Locomotive::Steam::Server do
expect(last_response.body).to include 'Lorem ipsum dolor sit amet'
end
+ describe 'redirection' do
+
+ let(:url) { '/store' }
+
+ subject { get url; last_response }
+
+ it 'redirects to another site' do
+ expect(subject.status).to eq(301)
+ expect(subject.location).to eq('http://www.apple.com/en/itunes/')
+ end
+
+ context 'localized page' do
+
+ let(:url) { '/fr/magasin' }
+
+ it 'redirects to another site' do
+ expect(subject.status).to eq(301)
+ expect(subject.location).to eq('http://www.apple.com/fr/itunes/')
+ end
+
+ end
+
+ end
+
describe 'page not found' do
it 'shows the 404 page' do
spec/unit/middlewares/base_spec.rb +0 -23
@@ @@ -1,23 +0,0 @@
- # require 'spec_helper'
-
- # require_relative '../../../lib/locomotive/steam/middlewares/base'
-
- # describe Locomotive::Steam::Middlewares::Base do
-
- # before { skip }
-
- # let(:app) { ->(env) { [200, env, 'app'] }}
-
- # let :middleware do
- # Locomotive::Steam::Middlewares::Base.new(app)
- # end
-
- # specify "return 200" do
- # code, headers, response = middleware.call env_for('http://www.example.com', { 'steam.path' => 'my path' })
- # expect(code).to eq(200)
- # end
-
- # def env_for url, opts={}
- # Rack::MockRequest.env_for(url, opts)
- # end
- # end
spec/unit/middlewares/page_spec.rb +0 -54
@@ @@ -1,54 +0,0 @@
- # require 'spec_helper'
-
- # require_relative '../../../lib/locomotive/steam/middlewares/base'
- # require_relative '../../../lib/locomotive/steam/middlewares/page'
-
- # describe Locomotive::Steam::Middlewares::Page do
-
- # before { skip }
-
- # let(:app) { ->(env) { [200, env, 'app'] }}
-
- # let :middleware do
- # Locomotive::Steam::Middlewares::Page.new(app)
- # end
-
- # context 'rack testing' do
- # let(:page) do
- # double(title: 'title', fullpath: 'fullpath')
- # end
-
- # before do
- # expect(middleware).to receive(:fetch_page).with('wk') { page }
- # expect(Locomotive::Common::Logger).to receive(:info).with("Found page \"title\" [fullpath]") { nil }
- # end
-
- # subject do
- # middleware.call env_for('http://www.example.com', { 'steam.locale' => 'wk' })
- # end
-
- # specify 'return 200' do
- # code, headers, response = subject
- # expect(code).to eq(200)
- # end
-
- # specify 'set page' do
- # code, headers, response = subject
- # expect(headers['steam.page']).to eq(page)
- # end
- # end
-
- # context 'test in isolation' do
- # describe '#path_combinations' do
- # specify do
- # expect(
- # middleware.send(:path_combinations, 'projects/project-2')
- # ).to eq(['projects/project-2', 'projects/*', '*/project-2'])
- # end
- # end
- # end
-
- # def env_for url, opts={}
- # Rack::MockRequest.env_for(url, opts)
- # end
- # end