Skip to content

Commit b8bd870

Browse files
Merge pull request #42 from mudbugmedia/handle-all-paths-as-array
Handle all paths as array
2 parents 6f11de6 + a59875c commit b8bd870

File tree

6 files changed

+24
-54
lines changed

6 files changed

+24
-54
lines changed

lib/critical_path_css/configuration.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ def base_url
99
@config['base_url']
1010
end
1111

12-
def css_path
13-
@config['css_path']
14-
end
15-
1612
def css_paths
1713
@config['css_paths']
1814
end
@@ -28,5 +24,9 @@ def routes
2824
def penthouse_options
2925
@config['penthouse_options'] || {}
3026
end
27+
28+
def path_for_route(route)
29+
css_paths[routes.index(route)] || css_paths.first
30+
end
3131
end
3232
end

lib/critical_path_css/css_fetcher.rb

+1-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def fetch
1616
def fetch_route(route)
1717
options = {
1818
'url' => @config.base_url + route,
19-
'css' => fetch_css_path_for_route(route),
19+
'css' => @config.path_for_route(route),
2020
'width' => 1300,
2121
'height' => 900,
2222
'timeout' => 30_000,
@@ -51,17 +51,5 @@ def fetch_route(route)
5151
end
5252
out
5353
end
54-
55-
private
56-
57-
def fetch_css_path_for_route(route)
58-
index_for_route = @config.routes.index(route)
59-
60-
if index_for_route && @config.css_paths[index_for_route]
61-
@config.css_paths[index_for_route]
62-
else
63-
@config.css_path
64-
end
65-
end
6654
end
6755
end

lib/critical_path_css/rails/config_loader.rb

+5-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ def configuration_file_path
1919
end
2020

2121
def format_css_paths
22-
if config['css_path']
23-
config['css_path'] = format_path(config['css_path'])
24-
config['css_paths'] = []
25-
elsif config['css_paths']
26-
config['css_path'] = ''
27-
config['css_paths'] = config['css_paths'].collect { |path| format_path(path) }
22+
config['css_paths'] = [config['css_path']] if config['css_path']
23+
24+
if config['css_paths']
25+
config['css_paths'].map! { |path| format_path(path) }
2826
else
29-
config['css_path'] = ActionController::Base.helpers.stylesheet_path(config['manifest_name'], host: '')
30-
config['css_paths'] = []
27+
config['css_paths'] = [ActionController::Base.helpers.stylesheet_path(config['manifest_name'], host: '')]
3128
end
3229
end
3330

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module CriticalPathCSS
22
module Rails
3-
VERSION = '3.0.2'.freeze
3+
VERSION = '3.0.3'.freeze
44
end
55
end

spec/lib/critical_path_css/css_fetcher_spec.rb

+9-12
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
let(:response) { ['foo','', OpenStruct.new(exitstatus: 0)] }
88
let(:routes) { ['/', '/new_route'] }
99
let(:config) do
10-
OpenStruct.new(
11-
base_url: base_url,
12-
css_path: css_path,
13-
css_paths: css_paths,
14-
penthouse_options: {},
15-
routes: routes
10+
CriticalPathCss::Configuration.new(
11+
OpenStruct.new(
12+
base_url: base_url,
13+
css_paths: css_paths,
14+
penthouse_options: {},
15+
routes: routes
16+
)
1617
)
1718
end
1819

1920
describe '#fetch_route' do
2021
context 'when a single css_path is configured' do
21-
let(:css_path) { '/test.css' }
22-
let(:css_paths) { [] }
22+
let(:css_paths) { ['/test.css'] }
2323

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

3636
describe '#fetch' do
3737
context 'when a single css_path is configured' do
38-
let(:css_path) { '/test.css' }
39-
let(:css_paths) { [] }
38+
let(:css_paths) { ['/test.css'] }
4039

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

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

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

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

spec/lib/critical_path_css/rails/config_loader_spec.rb

+4-16
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,14 @@
1111
context 'when single css_path is specified' do
1212
let(:config_file) { file_fixture('config/single-css-path.yml').read }
1313

14-
it 'sets css_path with the path' do
15-
expect(subject.config['css_path']).to eq '/app/spec/internal/public/test.css'
16-
end
17-
18-
it 'leaves css_paths empty' do
19-
expect(subject.config['css_paths']).to eq []
14+
it 'sets css_paths with the lone path' do
15+
expect(subject.config['css_paths']).to eq ['/app/spec/internal/public/test.css']
2016
end
2117
end
2218

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

26-
it 'sets css_path to empty string' do
27-
expect(subject.config['css_path']).to eq ''
28-
end
29-
3022
it 'leaves css_paths to an array of paths' do
3123
expect(subject.config['css_paths']).to eq ['/app/spec/internal/public/test.css','/app/spec/internal/public/test2.css']
3224
end
@@ -35,12 +27,8 @@
3527
context 'when no paths are specified' do
3628
let(:config_file) { file_fixture('config/no-paths-specified.yml').read }
3729

38-
it 'sets css_path with the path' do
39-
expect(subject.config['css_path']).to eq '/stylesheets/application.css'
40-
end
41-
42-
it 'leaves css_paths empty' do
43-
expect(subject.config['css_paths']).to eq []
30+
it 'sets css_paths with the lone manifest path' do
31+
expect(subject.config['css_paths']).to eq ['/stylesheets/application.css']
4432
end
4533
end
4634

0 commit comments

Comments
 (0)