multi_fetch_fragments 0.0.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.
- data/lib/multi_fetch_fragments.rb +72 -0
- metadata +46 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
module MultiFetchFragments
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
alias_method_chain :render_collection, :multi_fetch_cache
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
def render_collection_with_multi_fetch_cache
|
10
|
+
return nil if @collection.blank?
|
11
|
+
|
12
|
+
if @options.key?(:spacer_template)
|
13
|
+
spacer = find_template(@options[:spacer_template]).render(@view, @locals)
|
14
|
+
end
|
15
|
+
|
16
|
+
result = []
|
17
|
+
if @options[:cache].present?
|
18
|
+
|
19
|
+
keys_to_collection_map = {}
|
20
|
+
@collection.each do |item|
|
21
|
+
# debugger
|
22
|
+
|
23
|
+
key = @options[:cache].is_a?(Proc) ? @options[:cache].call(item) : item
|
24
|
+
|
25
|
+
expanded_key = ActiveSupport::Cache.expand_cache_key(key)
|
26
|
+
|
27
|
+
keys_to_collection_map[expanded_key] = item
|
28
|
+
end
|
29
|
+
collection_to_keys_map = keys_to_collection_map.invert
|
30
|
+
|
31
|
+
result_hash = Rails.cache.read_multi(keys_to_collection_map.keys)
|
32
|
+
|
33
|
+
# if we had a cached value, we don't need to render that object from the collection.
|
34
|
+
# if it wasn't cached, we need to render those objects as before
|
35
|
+
result_hash.each do |key, value|
|
36
|
+
if value.present?
|
37
|
+
collections_object = keys_to_collection_map[key]
|
38
|
+
@collection.delete(collections_object)
|
39
|
+
|
40
|
+
result << value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
if @collection.any?
|
45
|
+
collections_objects = @collection.clone
|
46
|
+
|
47
|
+
non_cached_results = @template ? collection_with_template : collection_without_template
|
48
|
+
|
49
|
+
non_cached_results.each_with_index do |item, index|
|
50
|
+
collection_object = collections_objects[index]
|
51
|
+
key = collection_to_keys_map[collection_object]
|
52
|
+
Rails.cache.write(key, item)
|
53
|
+
end
|
54
|
+
|
55
|
+
result += non_cached_results
|
56
|
+
end
|
57
|
+
|
58
|
+
else
|
59
|
+
result = @template ? collection_with_template : collection_without_template
|
60
|
+
end
|
61
|
+
|
62
|
+
result.join(spacer).html_safe
|
63
|
+
end
|
64
|
+
|
65
|
+
class Railtie < Rails::Railtie
|
66
|
+
initializer "etag_version_ids.initialize" do |app|
|
67
|
+
ActionView::PartialRenderer.class_eval do
|
68
|
+
include MultiFetchFragments
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multi_fetch_fragments
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nathan Kontny
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: nate.kontny@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/multi_fetch_fragments.rb
|
21
|
+
homepage:
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: multi_fetch_fragments allows you to render a collection of partials through
|
45
|
+
Rails multi read caching mechanism.
|
46
|
+
test_files: []
|