slimmer 12.1.0 → 13.0.0
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 +5 -0
- data/README.md +1 -50
- data/lib/slimmer.rb +1 -8
- data/lib/slimmer/cucumber.rb +0 -7
- data/lib/slimmer/rspec.rb +0 -7
- data/lib/slimmer/version.rb +1 -1
- metadata +2 -9
- data/lib/slimmer/component_resolver.rb +0 -26
- data/lib/slimmer/govuk_components.rb +0 -57
- data/lib/slimmer/i18n_backend.rb +0 -56
- data/lib/slimmer/local_component_resolver.rb +0 -19
- data/lib/slimmer/local_govuk_components.rb +0 -49
- data/lib/slimmer/network_component_resolver.rb +0 -45
- data/lib/slimmer/test_helpers/govuk_components.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 668e43ea2579afcef2047274b110c10f7a377560
|
4
|
+
data.tar.gz: eb7f475b36cda8923e92c230bb190121cea7badb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d02f1d56e8dd317087cc6a237cedb52df633e7baad7e35cb36f5ec934d2adf6610d272fa1c76d05b2ee8883c553cc1d0cadf44c0932457ecb1e27e6448c157e4
|
7
|
+
data.tar.gz: 3937badf8181de169406b57a6ba2eed98250d49239f727e3fd1e5d80899872e53c2da45f7af7324777117a599935f414e658b800a53bf0ed6db041db30c9498a
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
# 13.0.0
|
2
|
+
|
3
|
+
* Drop cache TTL to 60 seconds.
|
4
|
+
* BREAKING: Remove the component system, components are now consumed via the [govuk_publishing_components gem](https://github.com/alphagov/govuk_publishing_components)
|
5
|
+
|
1
6
|
# 12.1.0
|
2
7
|
|
3
8
|
* Make sure that the metatags defined in the application are inserted at the
|
data/README.md
CHANGED
@@ -15,8 +15,7 @@ Slimmer provides a Railtie so no configuration is necessary.
|
|
15
15
|
|
16
16
|
## Caching
|
17
17
|
|
18
|
-
Slimmer makes HTTP requests to `static` for templates
|
19
|
-
are cached for 15 minutes. Slimmer uses `Rails.cache` for this.
|
18
|
+
Slimmer makes HTTP requests to `static` for templates. These are cached using `Rails.cache`.
|
20
19
|
|
21
20
|
## Asset tag helpers
|
22
21
|
|
@@ -82,54 +81,6 @@ YourApp::Application.configure do
|
|
82
81
|
end
|
83
82
|
```
|
84
83
|
|
85
|
-
## GOV.UK Components
|
86
|
-
|
87
|
-
To use [shared template components](https://govuk-static.herokuapp.com/component-guide) you need to include the GOV.UK component module:
|
88
|
-
|
89
|
-
```rb
|
90
|
-
class ApplicationController < ActionController::Base
|
91
|
-
include Slimmer::GovukComponents
|
92
|
-
end
|
93
|
-
```
|
94
|
-
|
95
|
-
This will make calls out to static when you try and render a partial prefixed with `govuk_component`:
|
96
|
-
|
97
|
-
```erb
|
98
|
-
<%= render partial: 'govuk_component/example_component' %>
|
99
|
-
```
|
100
|
-
|
101
|
-
You will need a copy of static running for the templates to be loaded from.
|
102
|
-
|
103
|
-
### Testing components
|
104
|
-
|
105
|
-
In test mode (when `Rails.env.test?` returns `true`), shared components are not
|
106
|
-
fetched from Static. Instead they are rendered as a dummy tag which contains a
|
107
|
-
JSON dump of the `locals` - the arguments passed to the component.
|
108
|
-
|
109
|
-
A test helper is included which returns a CSS selector for finding a given
|
110
|
-
component to assert that it was used. You can make it available in your tests
|
111
|
-
with:
|
112
|
-
|
113
|
-
```rb
|
114
|
-
require 'slimmer/test_helpers/govuk_components'
|
115
|
-
include Slimmer::TestHelpers::GovukComponents
|
116
|
-
```
|
117
|
-
|
118
|
-
And then assert that the component has been used:
|
119
|
-
|
120
|
-
```rb
|
121
|
-
page.should have_css(shared_component_selector('metadata'))
|
122
|
-
```
|
123
|
-
|
124
|
-
Or look for one of the arguments to the component which will have been
|
125
|
-
`JSON.dump`ed inside the tag:
|
126
|
-
|
127
|
-
```rb
|
128
|
-
within(shared_component_selector('title')) do
|
129
|
-
expect(page).to have_content(expected_title_text)
|
130
|
-
end
|
131
|
-
```
|
132
|
-
|
133
84
|
### Cucumber
|
134
85
|
|
135
86
|
Add the following code to features/support:
|
data/lib/slimmer.rb
CHANGED
@@ -7,7 +7,7 @@ require 'slimmer/version'
|
|
7
7
|
require 'slimmer/railtie' if defined? Rails
|
8
8
|
|
9
9
|
module Slimmer
|
10
|
-
CACHE_TTL =
|
10
|
+
CACHE_TTL = 60
|
11
11
|
|
12
12
|
def self.cache
|
13
13
|
@cache ||= defined?(Rails) ? Rails.cache : NoCache.new
|
@@ -27,13 +27,6 @@ module Slimmer
|
|
27
27
|
autoload :Headers, 'slimmer/headers'
|
28
28
|
autoload :HTTPClient, 'slimmer/http_client'
|
29
29
|
|
30
|
-
autoload :GovukComponents, 'slimmer/govuk_components'
|
31
|
-
autoload :LocalGovukComponents, 'slimmer/local_govuk_components'
|
32
|
-
autoload :ComponentResolver, 'slimmer/component_resolver'
|
33
|
-
autoload :NetworkComponentResolver, 'slimmer/network_component_resolver'
|
34
|
-
autoload :LocalComponentResolver, 'slimmer/local_component_resolver'
|
35
|
-
autoload :I18nBackend, 'slimmer/i18n_backend'
|
36
|
-
|
37
30
|
module Processors
|
38
31
|
autoload :BodyClassCopier, 'slimmer/processors/body_class_copier'
|
39
32
|
autoload :BodyInserter, 'slimmer/processors/body_inserter'
|
data/lib/slimmer/cucumber.rb
CHANGED
data/lib/slimmer/rspec.rb
CHANGED
@@ -2,10 +2,3 @@ require 'rspec/core'
|
|
2
2
|
|
3
3
|
require 'slimmer'
|
4
4
|
require 'slimmer/test'
|
5
|
-
require 'slimmer/test_helpers/govuk_components'
|
6
|
-
|
7
|
-
RSpec.configure do |config|
|
8
|
-
config.include Slimmer::TestHelpers::GovukComponents
|
9
|
-
|
10
|
-
config.before { stub_shared_component_locales }
|
11
|
-
end
|
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:
|
4
|
+
version: 13.0.0
|
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: 2018-
|
11
|
+
date: 2018-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -234,16 +234,10 @@ files:
|
|
234
234
|
- bin/render_slimmer_error
|
235
235
|
- lib/slimmer.rb
|
236
236
|
- lib/slimmer/app.rb
|
237
|
-
- lib/slimmer/component_resolver.rb
|
238
237
|
- lib/slimmer/cucumber.rb
|
239
|
-
- lib/slimmer/govuk_components.rb
|
240
238
|
- lib/slimmer/govuk_request_id.rb
|
241
239
|
- lib/slimmer/headers.rb
|
242
240
|
- lib/slimmer/http_client.rb
|
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
|
247
241
|
- lib/slimmer/processors/body_class_copier.rb
|
248
242
|
- lib/slimmer/processors/body_inserter.rb
|
249
243
|
- lib/slimmer/processors/conditional_comment_mover.rb
|
@@ -262,7 +256,6 @@ files:
|
|
262
256
|
- lib/slimmer/skin.rb
|
263
257
|
- lib/slimmer/template.rb
|
264
258
|
- lib/slimmer/test.rb
|
265
|
-
- lib/slimmer/test_helpers/govuk_components.rb
|
266
259
|
- lib/slimmer/test_templates/header_footer_only.html
|
267
260
|
- lib/slimmer/test_templates/proposition_menu.html
|
268
261
|
- lib/slimmer/test_templates/wrapper.html
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module Slimmer
|
2
|
-
class ComponentResolver < ::ActionView::Resolver
|
3
|
-
TEST_TAG_NAME = 'test-govuk-component'
|
4
|
-
|
5
|
-
def find_templates(name, prefix, partial, details, outside_app_allowed = false)
|
6
|
-
return [] unless prefix == 'govuk_component'
|
7
|
-
template_path = [prefix, name].join('/')
|
8
|
-
details = {
|
9
|
-
:format => 'text/html',
|
10
|
-
:updated_at => Time.now,
|
11
|
-
:virtual_path => template_path
|
12
|
-
}
|
13
|
-
|
14
|
-
[ActionView::Template.new(template_body(template_path), template_path, erb_handler, details)]
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
def erb_handler
|
19
|
-
@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
|
20
|
-
end
|
21
|
-
|
22
|
-
def template_body(_template_path)
|
23
|
-
raise NotImplementedError, "Use NetworkComponentResolver or LocalComponentResolver"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
module Slimmer
|
2
|
-
# @api public
|
3
|
-
#
|
4
|
-
# Include this module to add the GOV.UK Components to your app.
|
5
|
-
# @example
|
6
|
-
# class ApplicationController < ActionController::Base
|
7
|
-
# include Slimmer::GovukComponents
|
8
|
-
# end
|
9
|
-
#
|
10
|
-
# # In your views:
|
11
|
-
#
|
12
|
-
# <%= render partial: 'govuk_component/example_component' %>
|
13
|
-
module GovukComponents
|
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 GovukComponents.expiring_resolver_cache.resolver
|
21
|
-
|
22
|
-
return if slimmer_backend_included?
|
23
|
-
I18n.backend = I18n::Backend::Chain.new(I18n.backend, Slimmer::I18nBackend.new)
|
24
|
-
end
|
25
|
-
|
26
|
-
# @private
|
27
|
-
def self.expiring_resolver_cache
|
28
|
-
@expiring_resolver_cache ||= TimedExpirationResolverCache.new
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
def slimmer_backend_included?
|
34
|
-
I18n.backend.is_a?(I18n::Backend::Chain) &&
|
35
|
-
I18n.backend.backends.any? { |b| b.is_a? Slimmer::I18nBackend }
|
36
|
-
end
|
37
|
-
|
38
|
-
# Slimmer::ComponentResolver instantiates a lot of large objects and leaks
|
39
|
-
# memory. This class will cache the resolver so that it doesn't have to
|
40
|
-
# create new ActionView::Template objects for each request. The cache is
|
41
|
-
# timed to allow frontends to pick up changes made to components in `static`.
|
42
|
-
class TimedExpirationResolverCache
|
43
|
-
def initialize
|
44
|
-
@cache_last_reset = Time.now
|
45
|
-
end
|
46
|
-
|
47
|
-
def resolver
|
48
|
-
if (@cache_last_reset + Slimmer::CACHE_TTL) < Time.now
|
49
|
-
@resolver = nil
|
50
|
-
@cache_last_reset = Time.now
|
51
|
-
end
|
52
|
-
|
53
|
-
@resolver ||= Slimmer::NetworkComponentResolver.new
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
data/lib/slimmer/i18n_backend.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
require 'i18n'
|
3
|
-
|
4
|
-
module Slimmer
|
5
|
-
class I18nBackend
|
6
|
-
include I18n::Backend::Base, I18n::Backend::Flatten
|
7
|
-
|
8
|
-
def available_locales
|
9
|
-
Slimmer.cache.fetch("available_locales", expires_in: Slimmer::CACHE_TTL) do
|
10
|
-
locale_json = fetch(static_locales_url)
|
11
|
-
JSON.parse(locale_json).map(&:to_sym)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def lookup(locale, key, scope = [], options = {})
|
16
|
-
key = normalize_flat_keys(locale, key, scope, options[:separator])
|
17
|
-
translations = translations(locale)
|
18
|
-
translations["#{locale}.#{key}".to_sym]
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def translations(locale)
|
24
|
-
Slimmer.cache.fetch("translations/#{locale}", expires_in: Slimmer::CACHE_TTL) do
|
25
|
-
fetch_translations(locale)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def static_locales_url(locale=nil)
|
30
|
-
[static_host, "templates", "locales", locale].compact.join('/')
|
31
|
-
end
|
32
|
-
|
33
|
-
def static_host
|
34
|
-
@static_host ||= Plek.new.find('static')
|
35
|
-
end
|
36
|
-
|
37
|
-
def fetch_translations(locale)
|
38
|
-
url = static_locales_url(locale)
|
39
|
-
json_data = fetch(url)
|
40
|
-
translations = JSON.parse(json_data)
|
41
|
-
flatten_translations(locale, translations, false, false)
|
42
|
-
rescue TemplateNotFoundException
|
43
|
-
{}
|
44
|
-
end
|
45
|
-
|
46
|
-
def fetch(url)
|
47
|
-
HTTPClient.get(url)
|
48
|
-
rescue RestClient::Exception => e
|
49
|
-
raise TemplateNotFoundException, "Unable to fetch: '#{url}' because #{e}", caller
|
50
|
-
rescue Errno::ECONNREFUSED => e
|
51
|
-
raise CouldNotRetrieveTemplate, "Unable to fetch: '#{url}' because #{e}", caller
|
52
|
-
rescue SocketError => e
|
53
|
-
raise CouldNotRetrieveTemplate, "Unable to fetch: '#{url}' because #{e}", caller
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,19 +0,0 @@
|
|
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
|
@@ -1,49 +0,0 @@
|
|
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
|
@@ -1,45 +0,0 @@
|
|
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
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'webmock'
|
2
|
-
|
3
|
-
module Slimmer
|
4
|
-
module TestHelpers
|
5
|
-
module GovukComponents
|
6
|
-
def stub_shared_component_locales
|
7
|
-
stub_request(:get, /https?:\/\/\S+.gov.uk\/templates\/locales\/.+/).
|
8
|
-
to_return(status: 400, headers: {})
|
9
|
-
stub_request(:get, /https?:\/\/\S+.gov.uk\/templates\/locales/).
|
10
|
-
to_return(status: 200, body: '{}', headers: {})
|
11
|
-
end
|
12
|
-
|
13
|
-
def shared_component_selector(name)
|
14
|
-
"#{Slimmer::ComponentResolver::TEST_TAG_NAME}[data-template='govuk_component-#{name}']"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|