uniform way to assign a site to a Steam services instance + introduce the mounted_on env attribute in the request which allows to render a page not from / but from a different folder (needed by the engine) + write more specs

did committed May 14, 2015
commit a2b5cce2ecf4c14338e46f43f9763a0602a2e130
Showing 11 changed files with 134 additions and 13 deletions
locomotive/steam/middlewares/site.rb b/lib/locomotive/steam/middlewares/site.rb +10 -2
@@ @@ -9,8 +9,7 @@ module Locomotive::Steam
include Helpers
def _call
- env['steam.site'] ||= services.site_finder.find
- services.repositories.current_site = site = env['steam.site']
+ site = find_site
# render a simple message if the service was not able to find a site
# based on the request.
@@ @@ -22,6 +21,15 @@ module Locomotive::Steam
private
+ def find_site
+ if env['steam.site']
+ # happens if Steam is running within the Engine
+ services.set_site(env['steam.site'])
+ else
+ env['steam.site'] = services.current_site
+ end
+ end
+
def render_no_site
render_response('Hi, we are sorry but no site was found.', 404, 'text/html')
end
locomotive/steam/services.rb b/lib/locomotive/steam/services.rb +7 -3
@@ @@ -19,6 +19,10 @@ module Locomotive
include Morphine
+ register :current_site do
+ repositories.current_site = site_finder.find
+ end
+
register :repositories do
Steam::Repositories.new(nil, nil, configuration)
end
@@ @@ -52,7 +56,7 @@ module Locomotive
end
register :url_builder do
- Steam::UrlBuilderService.new(current_site, locale)
+ Steam::UrlBuilderService.new(current_site, locale, request)
end
register :theme_asset_url do
@@ @@ -108,8 +112,8 @@ module Locomotive
@locale = repositories.locale = locale
end
- def current_site
- repositories.current_site
+ def set_site(site)
+ self.current_site = repositories.current_site = site
end
end
locomotive/steam/services/url_builder_service.rb b/lib/locomotive/steam/services/url_builder_service.rb +18 -1
@@ @@ -1,9 +1,13 @@
module Locomotive
module Steam
- class UrlBuilderService < Struct.new(:site, :current_locale)
+ class UrlBuilderService < Struct.new(:site, :current_locale, :request)
def url_for(page, locale = nil)
+ prefix(_url_for(page, locale))
+ end
+
+ def _url_for(page, locale = nil)
[''].tap do |segments|
locale ||= current_locale
same_locale = locale.to_sym == site.default_locale.to_sym
@@ @@ -17,11 +21,24 @@ module Locomotive
end
def public_submission_url_for(content_type)
+ prefix(_public_submission_url_for(content_type))
+ end
+
+ def _public_submission_url_for(content_type)
"/entry_submissions/#{content_type.slug}"
end
private
+ def prefix(url)
+ mounted_on ? "#{mounted_on}#{url}" : url
+ end
+
+ def mounted_on
+ return if request.nil?
+ request.env['steam.mounted_on']
+ end
+
def sanitized_fullpath(page, same_locale)
path = page.fullpath
spec/support/helpers.rb +1 -0
@@ @@ -42,5 +42,6 @@ module Spec
def env_for(url, opts={})
Rack::MockRequest.env_for(url, opts)
end
+
end
end
spec/unit/entities/snippet_spec.rb +17 -0
@@ @@ -0,0 +1,17 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Snippet do
+
+ let(:attributes) { {} }
+ let(:snippet) { described_class.new(attributes) }
+
+ describe '#source' do
+
+ let(:attributes) { { template: 'Hello world' } }
+
+ subject { snippet.source }
+ it { is_expected.to eq 'Hello world' }
+
+ end
+
+ end
spec/unit/liquid/drops/content_entry_collection_spec.rb +2 -0
@@ @@ -8,6 +8,8 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntryCollection do
let(:context) { ::Liquid::Context.new(assigns, {}, { services: services }) }
let(:drop) { described_class.new(content_type).tap { |d| d.context = context } }
+ before { allow(services).to receive(:current_site).and_return(nil) }
+
describe '#public_submission_url' do
it { expect(drop.public_submission_url).to eq '/entry_submissions/articles' }
end
spec/unit/liquid/filters/html_spec.rb +3 -1
@@ @@ -6,7 +6,7 @@ describe Locomotive::Steam::Liquid::Filters::Html do
include Locomotive::Steam::Liquid::Filters::Html
let(:site) { instance_double('Site', _id: 42)}
- let(:services) { Locomotive::Steam::Services.build_instance.tap { |s| s.repositories.current_site = site } }
+ let(:services) { Locomotive::Steam::Services.build_instance }
let(:context) { instance_double('Context', registers: { services: services }) }
let(:theme_asset_url) { services.theme_asset_url }
@@ @@ -14,6 +14,8 @@ describe Locomotive::Steam::Liquid::Filters::Html do
before { services.repositories.theme_asset = EngineThemeAsset.new(nil, site) }
+ before { allow(services).to receive(:current_site).and_return(site) }
+
before { @context = context }
it 'writes the tag to display a rss/atom feed' do
spec/unit/liquid/tags/editable/file_spec.rb +3 -1
@@ @@ -71,9 +71,11 @@ describe Locomotive::Steam::Liquid::Tags::Editable::File do
let(:page) { instance_double('Page', fullpath: 'hello-world', updated_at: DateTime.parse('2007-06-29 21:00:00')) }
let(:element) { instance_double('EditableFile', id: 42, default_source_url: nil, source?: false, source: nil, base_url: '') }
- let(:services) { Locomotive::Steam::Services.build_instance(nil) }
+ let(:services) { Locomotive::Steam::Services.build_instance }
let(:context) { ::Liquid::Context.new({}, {}, { page: page, services: services }) }
+ before { allow(services).to receive(:current_site).and_return(nil) }
+
before { allow(services.editable_element).to receive(:find).and_return(element) }
subject { render_template(source, context, options) }
spec/unit/liquid/tags/nav_spec.rb +2 -2
@@ @@ -22,7 +22,7 @@ describe 'Locomotive::Steam::Liquid::Tags::Nav' do
let(:source) { '{% nav site %}' }
let(:site) { instance_double('Site', name: 'My portfolio', default_locale: 'en') }
let(:page) { index }
- let(:services) { Locomotive::Steam::Services.build_instance(nil) }
+ let(:services) { Locomotive::Steam::Services.build_instance }
let(:repository) { services.repositories.page }
let(:assigns) { {} }
let(:registers) { { services: services, site: site, page: page } }
@@ @@ -31,7 +31,7 @@ describe 'Locomotive::Steam::Liquid::Tags::Nav' do
let(:output) { render_template(source, context, options) }
- before { services.repositories.current_site = site }
+ before { allow(services).to receive(:current_site).and_return(site) }
describe 'rendering' do
spec/unit/repositories/content_type_field_repository_spec.rb +51 -0
@@ @@ -0,0 +1,51 @@
+ require 'spec_helper'
+
+ require_relative '../../../lib/locomotive/steam/adapters/memory.rb'
+
+ describe Locomotive::Steam::ContentTypeFieldRepository do
+
+ let(:collection) { [{ name: 'title', type: 'string' }, { name: 'body', type: 'text' }] }
+ let(:adapter) { Locomotive::Steam::MemoryAdapter.new(nil) }
+ let(:repository) { described_class.new(adapter) }
+
+ before { allow(adapter).to receive(:collection).and_return(collection) }
+
+ describe '#by_name' do
+
+ let(:name) { nil }
+
+ subject { repository.by_name(name) }
+
+ it { expect(subject).to eq nil }
+
+ context 'with an existing name' do
+
+ let(:name) { 'title' }
+ it { expect(subject.type).to eq :string }
+
+ end
+
+ end
+
+ describe '#no_associations' do
+
+ let(:collection) { [{ name: 'title', type: 'string' }, { name: 'author', type: 'belongs_to' }] }
+
+ subject { repository.no_associations }
+
+ it { expect(subject.size).to eq 1 }
+ it { expect(subject.size).to eq 1 }
+
+ end
+
+ describe '#unique' do
+
+ let(:collection) { [{ name: 'name', type: 'string' }, { name: 'email', type: 'email', unique: true }] }
+
+ subject { repository.unique }
+
+ it { expect(subject.keys).to eq ['email'] }
+
+ end
+
+ end
spec/unit/services/url_builder_service_spec.rb +20 -3
@@ @@ -2,9 +2,11 @@ require 'spec_helper'
describe Locomotive::Steam::UrlBuilderService do
- let(:site) { instance_double('Site', default_locale: 'en') }
- let(:locale) { 'en' }
- let(:service) { described_class.new(site, locale) }
+ let(:mounted_on) { nil }
+ let(:request) { instance_double('Request', env: { 'steam.mounted_on' => mounted_on }) }
+ let(:site) { instance_double('Site', default_locale: 'en') }
+ let(:locale) { 'en' }
+ let(:service) { described_class.new(site, locale, request) }
describe '#url_for' do
@@ @@ -19,6 +21,11 @@ describe Locomotive::Steam::UrlBuilderService do
let(:locale) { 'fr' }
it { is_expected.to eq '/fr/about-us' }
+ context 'with a prefix' do
+ let(:mounted_on) { '/foo' }
+ it { is_expected.to eq '/foo/fr/about-us' }
+ end
+
end
describe 'no need to put the index slug' do
@@ @@ -31,6 +38,11 @@ describe Locomotive::Steam::UrlBuilderService do
let(:locale) { 'fr' }
it { is_expected.to eq '/fr' }
+ context 'with a prefix' do
+ let(:mounted_on) { '/foo' }
+ it { is_expected.to eq '/foo/fr' }
+ end
+
end
end
@@ @@ -41,6 +53,11 @@ describe Locomotive::Steam::UrlBuilderService do
let(:page) { instance_double('Template', fullpath: 'articles/content_type_template', templatized?: true, content_entry: article) }
it { is_expected.to eq '/articles/hello-world' }
+ context 'with a prefix' do
+ let(:mounted_on) { '/foo' }
+ it { is_expected.to eq '/foo/articles/hello-world' }
+ end
+
end
end