thredded 0.16.15 → 0.16.16

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
  SHA256:
3
- metadata.gz: ac00c63103252bbf9bff35aa057a6630605ad897368010665476b8b4d2abfc3a
4
- data.tar.gz: cf7c0937115c8967c8fed0ba5789e4445db0a9e8b098a3109563b63be3883059
3
+ metadata.gz: 8b60a5b76cbbde2198978ed1130913cb72ed1d85e6dbca1020b529eb94a35c46
4
+ data.tar.gz: ee2fe30289d73591eef33ee5355715fa313437a694abb8dedb6f876545547a25
5
5
  SHA512:
6
- metadata.gz: cd1d9295ea54d35b3f6f1ac48f741b2766645c118b4519b717d5c4f87d926be12492ff2f4b76449088dfe2357b10e518b28442c76ac2998c19614ea2d3631603
7
- data.tar.gz: 6d8892713aba82af3017146f0c4f5f8238af39842f4536640689915fa3833b68dc63023502c23e1311a63b6dd45aec0c02c14e4be3367fa6b67e3a471df70caf
6
+ metadata.gz: 1cec58bb0b89b1e7d9174821be631eb85db63ba82cc315dd7c47c97d6a917eba53fb2179d65f22375a92d84d12023785984a8852349d6d63b4abcd044ef774c4
7
+ data.tar.gz: ac9efa6f7844dbcb56cc900fd59c9eb32538a00735ad8a8335f5c61d9c3c33ababff6f324a1445a5c9e2ae32bd90ba6fb43002cf5b83f02cf143a816d525d635
data/README.md CHANGED
@@ -96,7 +96,7 @@ Then, see the rest of this Readme for more information about using and customizi
96
96
  Add the gem to your Gemfile:
97
97
 
98
98
  ```ruby
99
- gem 'thredded', '~> 0.16.15'
99
+ gem 'thredded', '~> 0.16.16'
100
100
  ```
101
101
 
102
102
  Add the Thredded [initializer] to your parent app by running the install generator.
@@ -13,6 +13,7 @@ module Thredded
13
13
  :blocked?,
14
14
  :last_moderation_record,
15
15
  :cache_key,
16
+ :cache_version,
16
17
  :cache_key_with_version,
17
18
  to: :@post
18
19
 
@@ -26,13 +26,12 @@ module Thredded
26
26
  collection = collection.to_a
27
27
  instrument(:collection, identifier: template.identifier, count: collection.size) do |instrumentation_payload|
28
28
  return [] if collection.blank?
29
- keyed_collection = collection.each_with_object({}) do |item, hash|
30
- key = ActiveSupport::Cache.expand_cache_key(
31
- view_context.cache_fragment_name(item, virtual_path: template.virtual_path), :views
32
- )
33
- # #read_multi & #write may require key mutability, Dalli 2.6.0.
34
- hash[key.frozen? ? key.dup : key] = item
35
- end
29
+
30
+ # Result is a hash with the key represents the
31
+ # key used for cache lookup and the value is the item
32
+ # on which the partial is being rendered
33
+ keyed_collection, ordered_keys = collection_by_cache_keys(collection, view_context, template)
34
+
36
35
  cache = collection_cache
37
36
  cached_partials = cache.read_multi(*keyed_collection.keys)
38
37
  instrumentation_payload[:cache_hits] = cached_partials.size if instrumentation_payload
@@ -44,9 +43,9 @@ module Thredded
44
43
  partial: partial, locals: locals, **opts
45
44
  ).each
46
45
 
47
- keyed_collection.map do |cache_key, item|
48
- [item, cached_partials[cache_key] || rendered_partials.next.tap do |rendered|
49
- cache.write(cache_key, rendered, expires_in: expires_in)
46
+ ordered_keys.map do |cache_key|
47
+ [keyed_collection[cache_key], cached_partials[cache_key] || rendered_partials.next.tap do |rendered|
48
+ cached_partials[cache_key] = cache.write(cache_key, rendered, expires_in: expires_in)
50
49
  end]
51
50
  end
52
51
  end
@@ -54,16 +53,24 @@ module Thredded
54
53
 
55
54
  private
56
55
 
57
- def collection_cache
58
- if ActionView::PartialRenderer.respond_to?(:collection_cache)
59
- # Rails 5.0+
60
- ActionView::PartialRenderer.collection_cache
61
- else
62
- # Rails 4.2.x
63
- Rails.application.config.action_controller.cache_store
56
+ def collection_by_cache_keys(collection, view, template)
57
+ digest_path = digest_path_from_template(view, template)
58
+
59
+ collection.each_with_object([{}, []]) do |item, (hash, ordered_keys)|
60
+ key = expanded_cache_key(item, view, template, digest_path)
61
+ ordered_keys << key
62
+ hash[key] = item
64
63
  end
65
64
  end
66
65
 
66
+ def expanded_cache_key(key, view, template, digest_path)
67
+ key = combined_fragment_cache_key(
68
+ view,
69
+ cache_fragment_name(view, key, virtual_path: template.virtual_path, digest_path: digest_path)
70
+ )
71
+ key.frozen? ? key.dup : key # #read_multi & #write may require mutability, Dalli 2.6.0.
72
+ end
73
+
67
74
  # @return [Array<String>]
68
75
  def render_partials(view_context, collection:, render_threads:, **opts)
69
76
  return [] if collection.empty?
@@ -72,7 +79,13 @@ module Thredded
72
79
  render_partials_serial(view_context, collection, opts)
73
80
  else
74
81
  collection.each_slice(collection.size / num_threads).map do |slice|
75
- Thread.start { render_partials_serial(view_context.dup, slice, opts) }
82
+ Thread.start do
83
+ # `ActionView::PartialRenderer` mutates the contents of `opts[:locals]`, `opts[:locals][:as]` in particular:
84
+ # https://github.com/rails/rails/blob/v6.0.2.1/actionview/lib/action_view/renderer/partial_renderer.rb#L379
85
+ # https://github.com/rails/rails/blob/v6.0.2.1/actionview/lib/action_view/renderer/partial_renderer.rb#L348-L356
86
+ opts[:locals] = opts[:locals].dup if opts[:locals]
87
+ render_partials_serial(view_context.dup, slice, opts)
88
+ end
76
89
  end.flat_map(&:value)
77
90
  end
78
91
  end
@@ -86,11 +99,59 @@ module Thredded
86
99
  collection.map { |object| render_partial(partial_renderer, view_context, opts.merge(object: object)) }
87
100
  end
88
101
 
102
+ if Rails::VERSION::MAJOR >= 5
103
+ def collection_cache
104
+ ActionView::PartialRenderer.collection_cache
105
+ end
106
+ else
107
+ def collection_cache
108
+ Rails.application.config.action_controller.cache_store
109
+ end
110
+ end
111
+
112
+ if Rails::VERSION::MAJOR > 5 || (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR >= 2)
113
+ def combined_fragment_cache_key(view, key)
114
+ view.combined_fragment_cache_key(key)
115
+ end
116
+ elsif Rails::VERSION::MAJOR >= 5
117
+ def combined_fragment_cache_key(view, key)
118
+ view.fragment_cache_key(key)
119
+ end
120
+ else
121
+ def combined_fragment_cache_key(view, key)
122
+ view.controller.fragment_cache_key(key)
123
+ end
124
+ end
125
+
89
126
  if Rails::VERSION::MAJOR >= 6
127
+ def cache_fragment_name(view, key, virtual_path:, digest_path:)
128
+ view.cache_fragment_name(key, virtual_path: virtual_path, digest_path: digest_path)
129
+ end
130
+
131
+ def digest_path_from_template(view, template)
132
+ view.digest_path_from_template(template)
133
+ end
134
+
90
135
  def render_partial(partial_renderer, view_context, opts)
91
136
  partial_renderer.render(view_context, opts, nil).body
92
137
  end
93
138
  else
139
+ def cache_fragment_name(_view, key, virtual_path:, digest_path:)
140
+ if digest_path
141
+ ["#{virtual_path}:#{digest_path}", key]
142
+ else
143
+ [virtual_path, key]
144
+ end
145
+ end
146
+
147
+ def digest_path_from_template(view, template)
148
+ ActionView::Digestor.digest(
149
+ name: template.virtual_path,
150
+ finder: @lookup_context,
151
+ dependencies: view.view_cache_dependencies
152
+ ).presence
153
+ end
154
+
94
155
  def render_partial(partial_renderer, view_context, opts)
95
156
  partial_renderer.render(view_context, opts, nil)
96
157
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Thredded
4
- VERSION = '0.16.15'
4
+ VERSION = '0.16.16'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thredded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.15
4
+ version: 0.16.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Oliveira
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-12-30 00:00:00.000000000 Z
12
+ date: 2020-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_record_union