Skip to content

Commit

Permalink
Merge pull request #42 from mudbugmedia/handle-all-paths-as-array
Browse files Browse the repository at this point in the history
Handle all paths as array
  • Loading branch information
michael-misshore authored Mar 3, 2019
2 parents 6f11de6 + a59875c commit b8bd870
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 54 deletions.
8 changes: 4 additions & 4 deletions lib/critical_path_css/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ def base_url
@config['base_url']
end

def css_path
@config['css_path']
end

def css_paths
@config['css_paths']
end
Expand All @@ -28,5 +24,9 @@ def routes
def penthouse_options
@config['penthouse_options'] || {}
end

def path_for_route(route)
css_paths[routes.index(route)] || css_paths.first
end
end
end
14 changes: 1 addition & 13 deletions lib/critical_path_css/css_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def fetch
def fetch_route(route)
options = {
'url' => @config.base_url + route,
'css' => fetch_css_path_for_route(route),
'css' => @config.path_for_route(route),
'width' => 1300,
'height' => 900,
'timeout' => 30_000,
Expand Down Expand Up @@ -51,17 +51,5 @@ def fetch_route(route)
end
out
end

private

def fetch_css_path_for_route(route)
index_for_route = @config.routes.index(route)

if index_for_route && @config.css_paths[index_for_route]
@config.css_paths[index_for_route]
else
@config.css_path
end
end
end
end
13 changes: 5 additions & 8 deletions lib/critical_path_css/rails/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ def configuration_file_path
end

def format_css_paths
if config['css_path']
config['css_path'] = format_path(config['css_path'])
config['css_paths'] = []
elsif config['css_paths']
config['css_path'] = ''
config['css_paths'] = config['css_paths'].collect { |path| format_path(path) }
config['css_paths'] = [config['css_path']] if config['css_path']

if config['css_paths']
config['css_paths'].map! { |path| format_path(path) }
else
config['css_path'] = ActionController::Base.helpers.stylesheet_path(config['manifest_name'], host: '')
config['css_paths'] = []
config['css_paths'] = [ActionController::Base.helpers.stylesheet_path(config['manifest_name'], host: '')]
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/critical_path_css/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module CriticalPathCSS
module Rails
VERSION = '3.0.2'.freeze
VERSION = '3.0.3'.freeze
end
end
21 changes: 9 additions & 12 deletions spec/lib/critical_path_css/css_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
let(:response) { ['foo','', OpenStruct.new(exitstatus: 0)] }
let(:routes) { ['/', '/new_route'] }
let(:config) do
OpenStruct.new(
base_url: base_url,
css_path: css_path,
css_paths: css_paths,
penthouse_options: {},
routes: routes
CriticalPathCss::Configuration.new(
OpenStruct.new(
base_url: base_url,
css_paths: css_paths,
penthouse_options: {},
routes: routes
)
)
end

describe '#fetch_route' do
context 'when a single css_path is configured' do
let(:css_path) { '/test.css' }
let(:css_paths) { [] }
let(:css_paths) { ['/test.css'] }

it 'generates css for the single route' do
expect(Open3).to receive(:capture3) do |arg1, arg2, arg3|
Expand All @@ -35,8 +35,7 @@

describe '#fetch' do
context 'when a single css_path is configured' do
let(:css_path) { '/test.css' }
let(:css_paths) { [] }
let(:css_paths) { ['/test.css'] }

it 'generates css for each route from the same file' do
expect(Open3).to receive(:capture3) do |arg1, arg2, arg3|
Expand All @@ -50,7 +49,6 @@
end

context 'when multiple css_paths are configured' do
let(:css_path) { '' }
let(:css_paths) { ['/test.css', '/test2.css'] }

it 'generates css for each route from the respective file' do
Expand All @@ -67,7 +65,6 @@
end

context 'when same css file applies to multiple routes' do
let(:css_path) { '' }
let(:css_paths) { ['/test.css', '/test2.css', '/test.css'] }
let(:routes) { ['/', '/new_route', '/newer_route'] }

Expand Down
20 changes: 4 additions & 16 deletions spec/lib/critical_path_css/rails/config_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,14 @@
context 'when single css_path is specified' do
let(:config_file) { file_fixture('config/single-css-path.yml').read }

it 'sets css_path with the path' do
expect(subject.config['css_path']).to eq '/app/spec/internal/public/test.css'
end

it 'leaves css_paths empty' do
expect(subject.config['css_paths']).to eq []
it 'sets css_paths with the lone path' do
expect(subject.config['css_paths']).to eq ['/app/spec/internal/public/test.css']
end
end

context 'when multiple css_paths are specified' do
let(:config_file) { file_fixture('config/mutliple-css-paths.yml').read }

it 'sets css_path to empty string' do
expect(subject.config['css_path']).to eq ''
end

it 'leaves css_paths to an array of paths' do
expect(subject.config['css_paths']).to eq ['/app/spec/internal/public/test.css','/app/spec/internal/public/test2.css']
end
Expand All @@ -35,12 +27,8 @@
context 'when no paths are specified' do
let(:config_file) { file_fixture('config/no-paths-specified.yml').read }

it 'sets css_path with the path' do
expect(subject.config['css_path']).to eq '/stylesheets/application.css'
end

it 'leaves css_paths empty' do
expect(subject.config['css_paths']).to eq []
it 'sets css_paths with the lone manifest path' do
expect(subject.config['css_paths']).to eq ['/stylesheets/application.css']
end
end

Expand Down

0 comments on commit b8bd870

Please sign in to comment.