Skip to content

Commit 3470ef8

Browse files
Refactor config loader
1 parent 77282c8 commit 3470ef8

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

lib/critical-path-css-rails.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ def self.fetch(route)
2828
end
2929

3030
def self.fetcher
31-
@fetcher ||= CssFetcher.new(Configuration.new(CriticalPathCss::Rails::ConfigLoader.new.load))
31+
@fetcher ||= CssFetcher.new(Configuration.new(config))
32+
end
33+
34+
def self.config
35+
@config ||= begin
36+
loader = CriticalPathCss::Rails::ConfigLoader.new
37+
loader.load
38+
loader.config
39+
end
3240
end
3341
end

lib/critical_path_css/rails/config_loader.rb

+23-16
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@ module Rails
33
class ConfigLoader
44
CONFIGURATION_FILENAME = 'critical_path_css.yml'.freeze
55

6+
attr_reader :config
7+
68
def load
7-
config = YAML.safe_load(ERB.new(File.read(configuration_file_path)).result, [], [], true)[::Rails.env]
8-
validate_css_path config
9-
if config['css_path']
10-
config['css_path'] = "#{::Rails.root}/public" + (
11-
config['css_path'] ||
12-
ActionController::Base.helpers.stylesheet_path(
13-
config['manifest_name'], host: ''
14-
)
15-
)
16-
config['css_paths'] = []
17-
else
18-
config['css_path'] = ''
19-
config['css_paths'] = config['css_paths'].collect { |path| "#{::Rails.root}/public#{path}" }
20-
end
21-
config
9+
validate_css_paths
10+
format_css_paths
11+
end
12+
13+
def config
14+
@config ||= YAML.safe_load(ERB.new(File.read(configuration_file_path)).result, [], [], true)[::Rails.env]
2215
end
2316

2417
private
@@ -27,7 +20,21 @@ def configuration_file_path
2720
@configuration_file_path ||= ::Rails.root.join('config', CONFIGURATION_FILENAME)
2821
end
2922

30-
def validate_css_path(config)
23+
def format_css_paths
24+
if config['css_path']
25+
config['css_path'] = format_path(config['css_path'])
26+
config['css_paths'] = []
27+
else
28+
config['css_path'] = ''
29+
config['css_paths'] = config['css_paths'].collect { |path| format_path(path) }
30+
end
31+
end
32+
33+
def format_path(path)
34+
"#{::Rails.root}/public#{path}"
35+
end
36+
37+
def validate_css_paths
3138
if config['css_path'] && config['css_paths']
3239
raise LoadError, 'Cannot specify both css_path and css_paths'
3340
elsif config['css_paths'] && config['css_paths'].length != config['routes'].length

spec/lib/critical_path_css/rails/config_loader_spec.rb

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
RSpec.describe 'ConfigLoader' do
44
let(:subject) { CriticalPathCss::Rails::ConfigLoader.new }
5+
56
describe '#load' do
67
before do
78
allow(File).to receive(:read).and_return(config_file)
@@ -24,16 +25,16 @@
2425
CONFIG
2526
}
2627

27-
it 'sets css_path with the path' do
28-
config = subject.load
28+
before do
29+
subject.load
30+
end
2931

30-
expect(config['css_path']).to eq '/app/spec/internal/public/test.css'
32+
it 'sets css_path with the path' do
33+
expect(subject.config['css_path']).to eq '/app/spec/internal/public/test.css'
3134
end
3235

3336
it 'leaves css_paths empty' do
34-
config = subject.load
35-
36-
expect(config['css_paths']).to eq []
37+
expect(subject.config['css_paths']).to eq []
3738
end
3839
end
3940

@@ -57,16 +58,16 @@
5758
CONFIG
5859
}
5960

60-
it 'sets css_path to empty string' do
61-
config = subject.load
61+
before do
62+
subject.load
63+
end
6264

63-
expect(config['css_path']).to eq ''
65+
it 'sets css_path to empty string' do
66+
expect(subject.config['css_path']).to eq ''
6467
end
6568

6669
it 'leaves css_paths to an array of paths' do
67-
config = subject.load
68-
69-
expect(config['css_paths']).to eq ['/app/spec/internal/public/test.css','/app/spec/internal/public/test2.css']
70+
expect(subject.config['css_paths']).to eq ['/app/spec/internal/public/test.css','/app/spec/internal/public/test2.css']
7071
end
7172
end
7273

0 commit comments

Comments
 (0)