Skip to content

Commit 6fdc667

Browse files
Move configurations into their own class, read from a YAML file
1 parent f1def0d commit 6fdc667

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

.rubocop_todo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2015-09-19 12:37:55 -0500 using RuboCop version 0.34.1.
3+
# on 2015-10-01 20:43:37 -0500 using RuboCop version 0.34.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -9,4 +9,4 @@
99
# Offense count: 2
1010
# Configuration parameters: AllowURI, URISchemes.
1111
Metrics/LineLength:
12-
Max: 91
12+
Max: 90
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module CriticalPathCss
2+
class Configuration
3+
CONFIGURATION_FILENAME = 'critical_path_css.yml'
4+
5+
def initialize
6+
@configurations = YAML.load_file(configuration_file_path)[Rails.env]
7+
end
8+
9+
def routes
10+
@routes ||= @configurations['routes']
11+
end
12+
13+
def base_url
14+
@base_url ||= @configurations['base_url']
15+
end
16+
17+
private
18+
19+
def configuration_file_path
20+
Rails.root.join('config', CONFIGURATION_FILENAME)
21+
end
22+
end
23+
end

lib/critical_path_css_rails.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ module CriticalPathCss
22
require 'phantomjs'
33

44
CACHE_NAMESPACE = 'critical-path-css'
5-
PENTHOUSE_PATH = "#{File.dirname(__FILE__)}/penthouse/penthouse.js"
5+
PENTHOUSE_PATH = "#{File.dirname(__FILE__)}/penthouse/penthouse.js"
66

7-
def self.generate(main_css_path, base_url, routes)
7+
def self.generate(main_css_path)
88
full_main_css_path = "#{Rails.root}/public#{main_css_path}"
9+
config = CriticalPathCss::Configuration.new(Rails.root, Rails.env)
10+
11+
config.routes.each do |route|
12+
css = Phantomjs.run(PENTHOUSE_PATH, config.base_url + route, full_main_css_path)
913

10-
routes.each do |route|
11-
css = Phantomjs.run(PENTHOUSE_PATH, base_url + route, full_main_css_path)
1214
Rails.cache.write(route, css, namespace: CACHE_NAMESPACE)
1315
end
1416
end

lib/tasks/critical_path_css.rake

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
require 'critical_path_css_rails'
22

33
namespace :critical_path_css do
4-
@base_url = Rails.env.production? ? 'http://example.com' : 'http://localhost:3000'
5-
@routes = %w(
6-
/
7-
)
8-
94
desc 'Generate critical CSS for the routes defined'
105
task generate: :environment do
11-
@main_css_path = ActionController::Base.helpers.stylesheet_path('application.css').to_s
6+
main_css_path = ActionController::Base.helpers.stylesheet_path('application.css').to_s
127

13-
CriticalPathCss.generate(@main_css_path, @base_url, @routes)
8+
CriticalPathCss.generate(main_css_path)
149
end
1510
end

0 commit comments

Comments
 (0)