slimmer 11.0.2 → 11.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bf16d4e2f2259c21173a5fd0015a810adb3f34c
4
- data.tar.gz: 02012c14551c9b5111bce08210a024aa25929075
3
+ metadata.gz: 387557200b31b5f4fa4cda08f9286ce17dee3f7e
4
+ data.tar.gz: 944392917f3c40742d123597aa9618d3034bbf9e
5
5
  SHA512:
6
- metadata.gz: 49b50c6ce47b18d9e68950e15facdec3823429a046a4847f3737a92c85bb1f18867d52c53fe8b36f4aa52a9b05f3c734fecc05ce9d43698d7e05b54f97f0f6ac
7
- data.tar.gz: c0f31d7cc3f946664e1d258f308e8099c822e49c63c3ce4e14cb585f3e85999a51560945666c1f7d59f550683d368bbc2d59e927e5cc7d283453655dc64c7926
6
+ metadata.gz: 500f77c015043040668fb179f29579de3595bfc7ea36c46f3a7d72fb73714469631d3bccf0aff83fe048e8a128f84527f949db0e8525f046095bd73df69bbee9
7
+ data.tar.gz: 252d342dbb50d38f6fab78738850298d18ef20cbd917ea0963bb5fa1f877e8b02b1c5511a0bea8cebae0f5d0751dd6d897ed179b177d1d9cb4ee9f04be0992b9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 11.1.0
2
+
3
+ * Allow components in static to be loaded locally rather than over the network (#208)
4
+ * Replace deprecated `after_filter` in slimmer_template (#209)
5
+
1
6
  # 11.0.2
2
7
 
3
8
  * Add missing class tags to test templates for including Slimmer in tests
@@ -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 fetch(template_url)
39
- HTTPClient.get(template_url)
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
@@ -50,7 +50,7 @@ module Slimmer
50
50
  @cache_last_reset = Time.now
51
51
  end
52
52
 
53
- @resolver ||= Slimmer::ComponentResolver.new
53
+ @resolver ||= Slimmer::NetworkComponentResolver.new
54
54
  end
55
55
  end
56
56
  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
@@ -11,7 +11,7 @@ module Slimmer
11
11
 
12
12
  module ClassMethods
13
13
  def slimmer_template template_name
14
- after_filter do
14
+ after_action do
15
15
  response.headers[Slimmer::Headers::TEMPLATE_HEADER] ||= template_name.to_s
16
16
  end
17
17
  end
@@ -1,3 +1,3 @@
1
1
  module Slimmer
2
- VERSION = '11.0.2'
2
+ VERSION = '11.1.0'
3
3
  end
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
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.0.2
4
+ version: 11.1.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: 2017-08-29 00:00:00.000000000 Z
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