feature: per-site asset host
Chris Grant
committed May 30, 2017
commit 2824db419af76d04d4ce4556caa5cedbc2079720
Showing 7
changed files with
51 additions
and 8 deletions
locomotive/steam/entities/site.rb b/lib/locomotive/steam/entities/site.rb
+2
-1
| @@ | @@ -18,7 +18,8 @@ module Locomotive::Steam |
| private_access: false, | |
| password: nil, | |
| metafields_schema: {}, | |
| - | metafields: nil |
| + | metafields: nil, |
| + | asset_host: nil |
| }.merge(attributes)) | |
| end | |
locomotive/steam/liquid/drops/site.rb b/lib/locomotive/steam/liquid/drops/site.rb
+1
-1
| @@ | @@ -4,7 +4,7 @@ module Locomotive |
| module Drops | |
| class Site < I18nBase | |
| - | delegate :name, :domains, :seo_title, :meta_keywords, :meta_description, to: :@_source |
| + | delegate :name, :domains, :seo_title, :meta_keywords, :meta_description, :asset_host, to: :@_source |
| def index | |
| @index ||= repository.root.to_liquid | |
locomotive/steam/services/asset_host_service.rb b/lib/locomotive/steam/services/asset_host_service.rb
+3
-1
| @@ | @@ -31,7 +31,9 @@ module Locomotive |
| end | |
| def build_host(host, request, site) | |
| - | if host |
| + | if site && site.try(:asset_host) && !site.asset_host.empty? |
| + | site.asset_host =~ Steam::IsHTTP ? site.asset_host : "https://#{site.asset_host}" |
| + | elsif host |
| if host.respond_to?(:call) | |
| host.call(request, site) | |
| else | |
locomotive/steam/services/site_finder_service.rb b/lib/locomotive/steam/services/site_finder_service.rb
+1
-1
| @@ | @@ -6,7 +6,7 @@ module Locomotive |
| attr_accessor_initialize :repository, :request | |
| def find | |
| - | repository.by_domain(request.host) |
| + | repository.by_domain(request.host) if request |
| end | |
| end | |
spec/unit/entities/site_spec.rb
+15
-0
| @@ | @@ -98,4 +98,19 @@ describe Locomotive::Steam::Site do |
| end | |
| + | describe '#asset_host' do |
| + | |
| + | subject { site.asset_host } |
| + | |
| + | it { is_expected.to eq nil } |
| + | |
| + | context 'not blank' do |
| + | |
| + | let(:attributes) { { asset_host: 'http://asset.dev' } } |
| + | it { is_expected.to eq 'http://asset.dev' } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| end | |
spec/unit/liquid/drops/site_spec.rb
+2
-1
| @@ | @@ -4,7 +4,7 @@ describe Locomotive::Steam::Liquid::Drops::Site do |
| let(:services) { Locomotive::Steam::Services.build_instance } | |
| let(:context) { ::Liquid::Context.new({}, {}, { services: services }) } | |
| - | let(:site) { instance_double('Site', name: 'Locomotive', domains: ['acme.org'], seo_title: 'seo title', meta_keywords: 'keywords', meta_description: 'description', localized_attributes: {}) } |
| + | let(:site) { instance_double('Site', name: 'Locomotive', domains: ['acme.org'], seo_title: 'seo title', meta_keywords: 'keywords', meta_description: 'description', localized_attributes: {}, asset_host: 'http://asset.dev') } |
| let(:drop) { described_class.new(site).tap { |d| d.context = context } } | |
| subject { drop } | |
| @@ | @@ -15,6 +15,7 @@ describe Locomotive::Steam::Liquid::Drops::Site do |
| expect(subject.meta_keywords).to eq 'keywords' | |
| expect(subject.meta_description).to eq 'description' | |
| expect(subject.domains).to eq ['acme.org'] | |
| + | expect(subject.asset_host).to eq 'http://asset.dev' |
| end | |
| describe '#index' do | |
spec/unit/services/asset_host_service_spec.rb
+27
-3
| @@ | @@ -91,9 +91,9 @@ describe Locomotive::Steam::AssetHostService do |
| describe 'the host is a block' do | |
| - | let(:request) { instance_double('Request', ssl: true) } |
| - | let(:site) { instance_double('Site', cdn: true) } |
| - | let(:host) { ->(request, site) { site.cdn ? "http#{request.ssl ? 's' : ''}://assets.locomotivecms.com" : nil } } |
| + | let(:request) { instance_double('Request', ssl: true) } |
| + | let(:site) { instance_double('Site', cdn: true) } |
| + | let(:host) { ->(request, site) { site.cdn ? "http#{request.ssl ? 's' : ''}://assets.locomotivecms.com" : nil } } |
| it { is_expected.to eq 'https://assets.locomotivecms.com/sites/42/assets/1/banner.png' } | |
| @@ | @@ -113,4 +113,28 @@ describe Locomotive::Steam::AssetHostService do |
| end | |
| + | describe 'the site has an asset host' do |
| + | |
| + | let(:site) { instance_double('Site', asset_host: 'asset.dev') } |
| + | let(:host) { 'http://assets.locomotivecms.com' } |
| + | |
| + | it { is_expected.to eq 'https://asset.dev/sites/42/assets/1/banner.png' } |
| + | |
| + | context 'with the protocol' do |
| + | |
| + | let(:site) { instance_double('Site', asset_host: 'http://asset.dev') } |
| + | it { is_expected.to eq 'http://asset.dev/sites/42/assets/1/banner.png' } |
| + | |
| + | end |
| + | |
| + | context 'with an empty string' do |
| + | |
| + | let(:site) { instance_double('Site', asset_host: '') } |
| + | it { is_expected.to eq 'http://assets.locomotivecms.com/sites/42/assets/1/banner.png' } |
| + | |
| + | end |
| + | |
| + | |
| + | end |
| + | |
| end | |