slimmer 11.0.2 → 11.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +1 -1
- data/lib/slimmer.rb +3 -0
- data/lib/slimmer/component_resolver.rb +3 -38
- data/lib/slimmer/govuk_components.rb +1 -1
- data/lib/slimmer/local_component_resolver.rb +19 -0
- data/lib/slimmer/local_govuk_components.rb +49 -0
- data/lib/slimmer/network_component_resolver.rb +45 -0
- data/lib/slimmer/processors/inside_header_inserter.rb +2 -1
- data/lib/slimmer/template.rb +1 -1
- data/lib/slimmer/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 882c71d5e22c37f0dfe1f0598975f4c19e27ceb9
|
4
|
+
data.tar.gz: 6c115e9e40c73fa7216316ffe727663eb26f2b8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25ec33d5397c26d2392389bf173619c617ad41c8c54f64d7028c575b217f58f9c4104659c4eee671630412c2d367303c442f502d22bd204eed68cb1bbb4d380e
|
7
|
+
data.tar.gz: aa90a515cae94a8d23d530cf4543be9dba85955ca0421bd59848805803dd7d40d20b9362e2bcb1b009e985ccd898d6bb74b6a6e2b0aa2e07ddcf1e7d98415094
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
# 11.1.1
|
2
|
+
|
3
|
+
* Make the 'Inside Header Inserter' more resillient so that it doesn't throw
|
4
|
+
exceptions in a test context (#212)
|
5
|
+
|
6
|
+
# 11.1.0
|
7
|
+
|
8
|
+
* Allow components in static to be loaded locally rather than over the network (#208)
|
9
|
+
* Replace deprecated `after_filter` in slimmer_template (#209)
|
10
|
+
|
1
11
|
# 11.0.2
|
2
12
|
|
3
13
|
* Add missing class tags to test templates for including Slimmer in tests
|
data/README.md
CHANGED
@@ -84,7 +84,7 @@ end
|
|
84
84
|
|
85
85
|
## GOV.UK Components
|
86
86
|
|
87
|
-
To use [shared template components](https://govuk-
|
87
|
+
To use [shared template components](https://govuk-static.herokuapp.com/component-guide) you need to include the GOV.UK component module:
|
88
88
|
|
89
89
|
```rb
|
90
90
|
class ApplicationController < ActionController::Base
|
data/lib/slimmer.rb
CHANGED
@@ -28,7 +28,10 @@ module Slimmer
|
|
28
28
|
autoload :HTTPClient, 'slimmer/http_client'
|
29
29
|
|
30
30
|
autoload :GovukComponents, 'slimmer/govuk_components'
|
31
|
+
autoload :LocalGovukComponents, 'slimmer/local_govuk_components'
|
31
32
|
autoload :ComponentResolver, 'slimmer/component_resolver'
|
33
|
+
autoload :NetworkComponentResolver, 'slimmer/network_component_resolver'
|
34
|
+
autoload :LocalComponentResolver, 'slimmer/local_component_resolver'
|
32
35
|
autoload :I18nBackend, 'slimmer/i18n_backend'
|
33
36
|
|
34
37
|
module Processors
|
@@ -1,61 +1,26 @@
|
|
1
|
-
require 'slimmer/govuk_request_id'
|
2
|
-
require 'active_support/core_ext/string/inflections'
|
3
|
-
|
4
1
|
module Slimmer
|
5
2
|
class ComponentResolver < ::ActionView::Resolver
|
6
3
|
TEST_TAG_NAME = 'test-govuk-component'
|
7
4
|
|
8
5
|
def find_templates(name, prefix, partial, details, outside_app_allowed = false)
|
9
6
|
return [] unless prefix == 'govuk_component'
|
10
|
-
|
11
7
|
template_path = [prefix, name].join('/')
|
12
|
-
if test?
|
13
|
-
template_body = test_body(template_path)
|
14
|
-
else
|
15
|
-
template_body = Slimmer.cache.fetch(template_path, expires_in: Slimmer::CACHE_TTL) do
|
16
|
-
fetch(template_url(template_path))
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
8
|
details = {
|
21
9
|
:format => 'text/html',
|
22
10
|
:updated_at => Time.now,
|
23
11
|
:virtual_path => template_path
|
24
12
|
}
|
25
13
|
|
26
|
-
[ActionView::Template.new(template_body, template_path, erb_handler, details)]
|
14
|
+
[ActionView::Template.new(template_body(template_path), template_path, erb_handler, details)]
|
27
15
|
end
|
28
16
|
|
29
17
|
private
|
30
|
-
def test?
|
31
|
-
defined?(Rails) && Rails.env.test?
|
32
|
-
end
|
33
|
-
|
34
18
|
def erb_handler
|
35
19
|
@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
|
36
20
|
end
|
37
21
|
|
38
|
-
def
|
39
|
-
|
40
|
-
rescue RestClient::Exception => e
|
41
|
-
raise TemplateNotFoundException, "Unable to fetch: '#{template_url}' because #{e}", caller
|
42
|
-
rescue Errno::ECONNREFUSED => e
|
43
|
-
raise CouldNotRetrieveTemplate, "Unable to fetch: '#{template_url}' because #{e}", caller
|
44
|
-
rescue SocketError => e
|
45
|
-
raise CouldNotRetrieveTemplate, "Unable to fetch: '#{template_url}' because #{e}", caller
|
46
|
-
end
|
47
|
-
|
48
|
-
def template_url(template_path)
|
49
|
-
path = template_path.sub(/\.raw(\.html\.erb)?$/, '')
|
50
|
-
[static_host, "templates", "#{path}.raw.html.erb"].join('/')
|
51
|
-
end
|
52
|
-
|
53
|
-
def static_host
|
54
|
-
@static_host ||= Plek.new.find('static')
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_body(path)
|
58
|
-
%Q{<#{TEST_TAG_NAME} data-template="#{path.parameterize}"><%= JSON.dump(local_assigns) %></#{TEST_TAG_NAME}>}
|
22
|
+
def template_body(_template_path)
|
23
|
+
raise NotImplementedError, "Use NetworkComponentResolver or LocalComponentResolver"
|
59
24
|
end
|
60
25
|
end
|
61
26
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Slimmer
|
2
|
+
class LocalComponentResolver < ComponentResolver
|
3
|
+
private
|
4
|
+
|
5
|
+
def template_body(template_path)
|
6
|
+
File.read(template_file(template_path))
|
7
|
+
end
|
8
|
+
|
9
|
+
def template_file(template_path)
|
10
|
+
path = template_path.sub(/\.raw(\.html\.erb)?$/, '')
|
11
|
+
|
12
|
+
if defined?(Rails)
|
13
|
+
Rails.root.join("app", "views", "#{path}.raw.html.erb")
|
14
|
+
else
|
15
|
+
"#{path}.raw.html.erb"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Slimmer
|
2
|
+
# @api public
|
3
|
+
#
|
4
|
+
# Include this module to avoid loading components over the network
|
5
|
+
# @example
|
6
|
+
# class ApplicationController < ActionController::Base
|
7
|
+
# include Slimmer::LocalGovukComponents
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# # In your views:
|
11
|
+
#
|
12
|
+
# <%= render partial: 'govuk_component/example_component' %>
|
13
|
+
module LocalGovukComponents
|
14
|
+
def self.included into
|
15
|
+
into.before_action :add_govuk_components
|
16
|
+
end
|
17
|
+
|
18
|
+
# @private
|
19
|
+
def add_govuk_components
|
20
|
+
append_view_path LocalGovukComponents.expiring_resolver_cache.resolver
|
21
|
+
end
|
22
|
+
|
23
|
+
# @private
|
24
|
+
def self.expiring_resolver_cache
|
25
|
+
@expiring_resolver_cache ||= TimedExpirationResolverCache.new
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Slimmer::ComponentResolver instantiates a lot of large objects and leaks
|
31
|
+
# memory. This class will cache the resolver so that it doesn't have to
|
32
|
+
# create new ActionView::Template objects for each request. The cache is
|
33
|
+
# timed to allow frontends to pick up changes made to components in `static`.
|
34
|
+
class TimedExpirationResolverCache
|
35
|
+
def initialize
|
36
|
+
@cache_last_reset = Time.now
|
37
|
+
end
|
38
|
+
|
39
|
+
def resolver
|
40
|
+
if (@cache_last_reset + Slimmer::CACHE_TTL) < Time.now
|
41
|
+
@resolver = nil
|
42
|
+
@cache_last_reset = Time.now
|
43
|
+
end
|
44
|
+
|
45
|
+
@resolver ||= Slimmer::LocalComponentResolver.new
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'slimmer/govuk_request_id'
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
3
|
+
|
4
|
+
module Slimmer
|
5
|
+
class NetworkComponentResolver < ComponentResolver
|
6
|
+
private
|
7
|
+
|
8
|
+
def template_body(template_path)
|
9
|
+
if test?
|
10
|
+
test_body(template_path)
|
11
|
+
else
|
12
|
+
Slimmer.cache.fetch(template_path, expires_in: Slimmer::CACHE_TTL) do
|
13
|
+
fetch(template_url(template_path))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test?
|
19
|
+
defined?(Rails) && Rails.env.test?
|
20
|
+
end
|
21
|
+
|
22
|
+
def fetch(template_url)
|
23
|
+
HTTPClient.get(template_url)
|
24
|
+
rescue RestClient::Exception => e
|
25
|
+
raise TemplateNotFoundException, "Unable to fetch: '#{template_url}' because #{e}", caller
|
26
|
+
rescue Errno::ECONNREFUSED => e
|
27
|
+
raise CouldNotRetrieveTemplate, "Unable to fetch: '#{template_url}' because #{e}", caller
|
28
|
+
rescue SocketError => e
|
29
|
+
raise CouldNotRetrieveTemplate, "Unable to fetch: '#{template_url}' because #{e}", caller
|
30
|
+
end
|
31
|
+
|
32
|
+
def template_url(template_path)
|
33
|
+
path = template_path.sub(/\.raw(\.html\.erb)?$/, '')
|
34
|
+
[static_host, "templates", "#{path}.raw.html.erb"].join('/')
|
35
|
+
end
|
36
|
+
|
37
|
+
def static_host
|
38
|
+
@static_host ||= Plek.new.find('static')
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_body(path)
|
42
|
+
%{<#{TEST_TAG_NAME} data-template="#{path.parameterize}"><%= JSON.dump(local_assigns) %></#{TEST_TAG_NAME}>}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -4,7 +4,8 @@ module Slimmer::Processors
|
|
4
4
|
insertion = src.at_css('.slimmer-inside-header')
|
5
5
|
|
6
6
|
if insertion
|
7
|
-
dest.at_css('.header-logo')
|
7
|
+
logo = dest.at_css('.header-logo')
|
8
|
+
logo.add_next_sibling(insertion.inner_html) unless logo.nil?
|
8
9
|
end
|
9
10
|
end
|
10
11
|
end
|
data/lib/slimmer/template.rb
CHANGED
data/lib/slimmer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slimmer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 11.
|
4
|
+
version: 11.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GOV.UK Dev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -241,6 +241,9 @@ files:
|
|
241
241
|
- lib/slimmer/headers.rb
|
242
242
|
- lib/slimmer/http_client.rb
|
243
243
|
- lib/slimmer/i18n_backend.rb
|
244
|
+
- lib/slimmer/local_component_resolver.rb
|
245
|
+
- lib/slimmer/local_govuk_components.rb
|
246
|
+
- lib/slimmer/network_component_resolver.rb
|
244
247
|
- lib/slimmer/processors/body_class_copier.rb
|
245
248
|
- lib/slimmer/processors/body_inserter.rb
|
246
249
|
- lib/slimmer/processors/conditional_comment_mover.rb
|