jbuilder_cache_multi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: 54a2a84bed25c236f2516f7d502551044d62a9a7
4
+ data.tar.gz: 30aa6ccb09a966117c4b5f94a666227006612211
5
+ SHA512:
6
+ metadata.gz: a048fd0ee8ab5a57c5c8526f6401dde121dd91ae2b90543602df90c204ebac1797623596da544cb8fe2120efd05cb13e39854c59d7a2442445a128d3e1b241d9
7
+ data.tar.gz: 3f1c15b833a01c38323404d7dd48ea2e2a724c86c38e746ad1ebfcb2b9e2441ae0c79f94f5a5741bc7d9c9bc4f6b54d1787128fde9d013126ab39e7fa8c96b76
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Appraisals ADDED
@@ -0,0 +1,24 @@
1
+ appraise "rails-3-0" do
2
+ gem "railties", "~> 3.0.0"
3
+ gem "actionpack", "~> 3.0.0"
4
+ end
5
+
6
+ appraise "rails-3-1" do
7
+ gem "railties", "~> 3.1.0"
8
+ gem "actionpack", "~> 3.1.0"
9
+ end
10
+
11
+ appraise "rails-3-2" do
12
+ gem "railties", "~> 3.2.0"
13
+ gem "actionpack", "~> 3.2.0"
14
+ end
15
+
16
+ appraise "rails-4-0" do
17
+ gem "railties", "~> 4.0.0"
18
+ gem "actionpack", "~> 4.0.0"
19
+ end
20
+
21
+ appraise "rails-4-1" do
22
+ gem "railties", "~> 4.1.0"
23
+ gem "actionpack", "~> 4.1.0"
24
+ end
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rake"
6
+ gem "mocha", require: false
7
+ gem "appraisal"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Yonah Forst
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # JbuilderCacheMulti
2
+
3
+ Adds cache_collection! method, useful when iterating over a collection. The main advantage is that it will try to use fetch_multi from rails (if available in rails and supported by the cache) to query the cache.
4
+
5
+ fetch_muti uses read_multi (supported by memcache) to retreive multiple items in one go. This means less queries to the cache == faster responses. If items are not found, they are writen to the cache (individualy, at the moment).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'jbuilder_cache_multi'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jbuilder_cache_multi
20
+
21
+ ## Usage
22
+
23
+ Caches a collection of objects using fetch_multi, if supported (otherwise iterates over the collection using fetch)
24
+ Requires a block for each item in the array. Accepts optional 'key' attribute in options (e.g. key: 'v1').
25
+
26
+ Note: At the moment, does not accept the partial name as an argument #todo
27
+
28
+ Examples:
29
+
30
+ json.cache_collection! @people, expires_in: 10.minutes do |person|
31
+ json.partial! 'person', :person => person
32
+ end
33
+
34
+ Or with optional key
35
+
36
+ json.cache_collection! @people, expires_in: 10.minutes, key: 'v1' do |person|
37
+ json.partial! 'person', :person => person
38
+ end
39
+
40
+
41
+ ## Todo
42
+
43
+ - Add support for passing a partial name as an argument (e.g. json.cache_collection! @people, partial: 'person') or maybe even just "json.cache_collection! @people" and infer the partial name from the collection...
44
+
45
+ - When rendering other partials, use the hash of THAT partial for the cache_key (I beleieve it currently uses the view from where cache_collection! is called to calculate the cache_key) #not_good
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/joshblour/jbuilder_cache_multi/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
54
+
55
+ ## Credit
56
+ Loads borrowed from https://github.com/n8/multi_fetch_fragments. Thank you!
57
+ And of course https://github.com/rails/jbuilder
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require "bundler/setup"
2
+ require "rake/testtask"
3
+
4
+ if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
5
+ require "appraisal/task"
6
+ Appraisal::Task.new
7
+ task default: :appraisal
8
+ else
9
+ Rake::TestTask.new do |test|
10
+ require "rails/version"
11
+
12
+ test.libs << "test"
13
+
14
+ if Rails::VERSION::MAJOR == 3
15
+ test.test_files = %w[test/jbuilder_template_test.rb]
16
+ else
17
+ test.test_files = FileList["test/*_test.rb"]
18
+ end
19
+ end
20
+
21
+ task default: :test
22
+ end
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "mocha", :require => false
7
+ gem "appraisal"
8
+ gem "railties", "~> 3.0.0"
9
+ gem "actionpack", "~> 3.0.0"
10
+
11
+ gemspec :path => "../"
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ jbuilder_cache_multi (0.0.1)
5
+ jbuilder (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ abstract (1.0.0)
11
+ actionpack (3.0.20)
12
+ activemodel (= 3.0.20)
13
+ activesupport (= 3.0.20)
14
+ builder (~> 2.1.2)
15
+ erubis (~> 2.6.6)
16
+ i18n (~> 0.5.0)
17
+ rack (~> 1.2.5)
18
+ rack-mount (~> 0.6.14)
19
+ rack-test (~> 0.5.7)
20
+ tzinfo (~> 0.3.23)
21
+ activemodel (3.0.20)
22
+ activesupport (= 3.0.20)
23
+ builder (~> 2.1.2)
24
+ i18n (~> 0.5.0)
25
+ activesupport (3.0.20)
26
+ appraisal (1.0.0)
27
+ bundler
28
+ rake
29
+ thor (>= 0.14.0)
30
+ builder (2.1.2)
31
+ erubis (2.6.6)
32
+ abstract (>= 1.0.0)
33
+ i18n (0.5.3)
34
+ jbuilder (2.0.7)
35
+ activesupport (>= 3.0.0, < 5)
36
+ multi_json (~> 1.2)
37
+ json (1.8.1)
38
+ metaclass (0.0.4)
39
+ mocha (1.0.0)
40
+ metaclass (~> 0.0.1)
41
+ multi_json (1.10.0)
42
+ rack (1.2.8)
43
+ rack-mount (0.6.14)
44
+ rack (>= 1.0.0)
45
+ rack-test (0.5.7)
46
+ rack (>= 1.0)
47
+ railties (3.0.20)
48
+ actionpack (= 3.0.20)
49
+ activesupport (= 3.0.20)
50
+ rake (>= 0.8.7)
51
+ rdoc (~> 3.4)
52
+ thor (~> 0.14.4)
53
+ rake (10.3.1)
54
+ rdoc (3.12.2)
55
+ json (~> 1.4)
56
+ thor (0.14.6)
57
+ tzinfo (0.3.39)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ actionpack (~> 3.0.0)
64
+ appraisal
65
+ jbuilder_cache_multi!
66
+ mocha
67
+ railties (~> 3.0.0)
68
+ rake
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "mocha", :require => false
7
+ gem "appraisal"
8
+ gem "railties", "~> 3.1.0"
9
+ gem "actionpack", "~> 3.1.0"
10
+
11
+ gemspec :path => "../"
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ jbuilder_cache_multi (0.0.1)
5
+ jbuilder (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (3.1.12)
11
+ activemodel (= 3.1.12)
12
+ activesupport (= 3.1.12)
13
+ builder (~> 3.0.0)
14
+ erubis (~> 2.7.0)
15
+ i18n (~> 0.6)
16
+ rack (~> 1.3.6)
17
+ rack-cache (~> 1.2)
18
+ rack-mount (~> 0.8.2)
19
+ rack-test (~> 0.6.1)
20
+ sprockets (~> 2.0.4)
21
+ activemodel (3.1.12)
22
+ activesupport (= 3.1.12)
23
+ builder (~> 3.0.0)
24
+ i18n (~> 0.6)
25
+ activesupport (3.1.12)
26
+ multi_json (~> 1.0)
27
+ appraisal (1.0.0)
28
+ bundler
29
+ rake
30
+ thor (>= 0.14.0)
31
+ builder (3.0.4)
32
+ erubis (2.7.0)
33
+ hike (1.2.3)
34
+ i18n (0.6.9)
35
+ jbuilder (2.0.7)
36
+ activesupport (>= 3.0.0, < 5)
37
+ multi_json (~> 1.2)
38
+ json (1.8.1)
39
+ metaclass (0.0.4)
40
+ mocha (1.0.0)
41
+ metaclass (~> 0.0.1)
42
+ multi_json (1.10.0)
43
+ rack (1.3.10)
44
+ rack-cache (1.2)
45
+ rack (>= 0.4)
46
+ rack-mount (0.8.3)
47
+ rack (>= 1.0.0)
48
+ rack-ssl (1.3.4)
49
+ rack
50
+ rack-test (0.6.2)
51
+ rack (>= 1.0)
52
+ railties (3.1.12)
53
+ actionpack (= 3.1.12)
54
+ activesupport (= 3.1.12)
55
+ rack-ssl (~> 1.3.2)
56
+ rake (>= 0.8.7)
57
+ rdoc (~> 3.4)
58
+ thor (~> 0.14.6)
59
+ rake (10.3.1)
60
+ rdoc (3.12.2)
61
+ json (~> 1.4)
62
+ sprockets (2.0.4)
63
+ hike (~> 1.2)
64
+ rack (~> 1.0)
65
+ tilt (~> 1.1, != 1.3.0)
66
+ thor (0.14.6)
67
+ tilt (1.4.1)
68
+
69
+ PLATFORMS
70
+ ruby
71
+
72
+ DEPENDENCIES
73
+ actionpack (~> 3.1.0)
74
+ appraisal
75
+ jbuilder_cache_multi!
76
+ mocha
77
+ railties (~> 3.1.0)
78
+ rake
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "mocha", :require => false
7
+ gem "appraisal"
8
+ gem "railties", "~> 3.2.0"
9
+ gem "actionpack", "~> 3.2.0"
10
+
11
+ gemspec :path => "../"
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ jbuilder_cache_multi (0.0.1)
5
+ jbuilder (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (3.2.17)
11
+ activemodel (= 3.2.17)
12
+ activesupport (= 3.2.17)
13
+ builder (~> 3.0.0)
14
+ erubis (~> 2.7.0)
15
+ journey (~> 1.0.4)
16
+ rack (~> 1.4.5)
17
+ rack-cache (~> 1.2)
18
+ rack-test (~> 0.6.1)
19
+ sprockets (~> 2.2.1)
20
+ activemodel (3.2.17)
21
+ activesupport (= 3.2.17)
22
+ builder (~> 3.0.0)
23
+ activesupport (3.2.17)
24
+ i18n (~> 0.6, >= 0.6.4)
25
+ multi_json (~> 1.0)
26
+ appraisal (1.0.0)
27
+ bundler
28
+ rake
29
+ thor (>= 0.14.0)
30
+ builder (3.0.4)
31
+ erubis (2.7.0)
32
+ hike (1.2.3)
33
+ i18n (0.6.9)
34
+ jbuilder (2.0.7)
35
+ activesupport (>= 3.0.0, < 5)
36
+ multi_json (~> 1.2)
37
+ journey (1.0.4)
38
+ json (1.8.1)
39
+ metaclass (0.0.4)
40
+ mocha (1.0.0)
41
+ metaclass (~> 0.0.1)
42
+ multi_json (1.10.0)
43
+ rack (1.4.5)
44
+ rack-cache (1.2)
45
+ rack (>= 0.4)
46
+ rack-ssl (1.3.4)
47
+ rack
48
+ rack-test (0.6.2)
49
+ rack (>= 1.0)
50
+ railties (3.2.17)
51
+ actionpack (= 3.2.17)
52
+ activesupport (= 3.2.17)
53
+ rack-ssl (~> 1.3.2)
54
+ rake (>= 0.8.7)
55
+ rdoc (~> 3.4)
56
+ thor (>= 0.14.6, < 2.0)
57
+ rake (10.3.1)
58
+ rdoc (3.12.2)
59
+ json (~> 1.4)
60
+ sprockets (2.2.2)
61
+ hike (~> 1.2)
62
+ multi_json (~> 1.0)
63
+ rack (~> 1.0)
64
+ tilt (~> 1.1, != 1.3.0)
65
+ thor (0.19.1)
66
+ tilt (1.4.1)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ actionpack (~> 3.2.0)
73
+ appraisal
74
+ jbuilder_cache_multi!
75
+ mocha
76
+ railties (~> 3.2.0)
77
+ rake
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "mocha", :require => false
7
+ gem "appraisal"
8
+ gem "railties", "~> 4.0.0"
9
+ gem "actionpack", "~> 4.0.0"
10
+
11
+ gemspec :path => "../"
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ jbuilder_cache_multi (0.0.1)
5
+ jbuilder (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (4.0.4)
11
+ activesupport (= 4.0.4)
12
+ builder (~> 3.1.0)
13
+ erubis (~> 2.7.0)
14
+ rack (~> 1.5.2)
15
+ rack-test (~> 0.6.2)
16
+ activesupport (4.0.4)
17
+ i18n (~> 0.6, >= 0.6.9)
18
+ minitest (~> 4.2)
19
+ multi_json (~> 1.3)
20
+ thread_safe (~> 0.1)
21
+ tzinfo (~> 0.3.37)
22
+ appraisal (1.0.0)
23
+ bundler
24
+ rake
25
+ thor (>= 0.14.0)
26
+ builder (3.1.4)
27
+ erubis (2.7.0)
28
+ i18n (0.6.9)
29
+ jbuilder (2.0.7)
30
+ activesupport (>= 3.0.0, < 5)
31
+ multi_json (~> 1.2)
32
+ metaclass (0.0.4)
33
+ minitest (4.7.5)
34
+ mocha (1.0.0)
35
+ metaclass (~> 0.0.1)
36
+ multi_json (1.10.0)
37
+ rack (1.5.2)
38
+ rack-test (0.6.2)
39
+ rack (>= 1.0)
40
+ railties (4.0.4)
41
+ actionpack (= 4.0.4)
42
+ activesupport (= 4.0.4)
43
+ rake (>= 0.8.7)
44
+ thor (>= 0.18.1, < 2.0)
45
+ rake (10.3.1)
46
+ thor (0.19.1)
47
+ thread_safe (0.3.3)
48
+ tzinfo (0.3.39)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ actionpack (~> 4.0.0)
55
+ appraisal
56
+ jbuilder_cache_multi!
57
+ mocha
58
+ railties (~> 4.0.0)
59
+ rake
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "mocha", :require => false
7
+ gem "appraisal"
8
+ gem "railties", "~> 4.1.0"
9
+ gem "actionpack", "~> 4.1.0"
10
+ gem "rails-perftest"
11
+ gem "ruby-prof"
12
+
13
+ gemspec :path => "../"
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ jbuilder_cache_multi (0.0.1)
5
+ jbuilder (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (4.1.0)
11
+ actionview (= 4.1.0)
12
+ activesupport (= 4.1.0)
13
+ rack (~> 1.5.2)
14
+ rack-test (~> 0.6.2)
15
+ actionview (4.1.0)
16
+ activesupport (= 4.1.0)
17
+ builder (~> 3.1)
18
+ erubis (~> 2.7.0)
19
+ activesupport (4.1.0)
20
+ i18n (~> 0.6, >= 0.6.9)
21
+ json (~> 1.7, >= 1.7.7)
22
+ minitest (~> 5.1)
23
+ thread_safe (~> 0.1)
24
+ tzinfo (~> 1.1)
25
+ appraisal (1.0.0)
26
+ bundler
27
+ rake
28
+ thor (>= 0.14.0)
29
+ builder (3.2.2)
30
+ erubis (2.7.0)
31
+ i18n (0.6.9)
32
+ jbuilder (2.0.7)
33
+ activesupport (>= 3.0.0, < 5)
34
+ multi_json (~> 1.2)
35
+ json (1.8.1)
36
+ metaclass (0.0.4)
37
+ minitest (5.3.3)
38
+ mocha (1.0.0)
39
+ metaclass (~> 0.0.1)
40
+ multi_json (1.10.0)
41
+ rack (1.5.2)
42
+ rack-test (0.6.2)
43
+ rack (>= 1.0)
44
+ rails-perftest (0.0.3)
45
+ railties (4.1.0)
46
+ actionpack (= 4.1.0)
47
+ activesupport (= 4.1.0)
48
+ rake (>= 0.8.7)
49
+ thor (>= 0.18.1, < 2.0)
50
+ rake (10.3.1)
51
+ ruby-prof (0.15.1)
52
+ thor (0.19.1)
53
+ thread_safe (0.3.3)
54
+ tzinfo (1.1.0)
55
+ thread_safe (~> 0.1)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ actionpack (~> 4.1.0)
62
+ appraisal
63
+ jbuilder_cache_multi!
64
+ mocha
65
+ rails-perftest
66
+ railties (~> 4.1.0)
67
+ rake
68
+ ruby-prof
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jbuilder_cache_multi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jbuilder_cache_multi"
8
+ spec.version = JbuilderCacheMulti::VERSION
9
+ spec.authors = ["Yonah Forst"]
10
+ spec.email = ["joshblour@hotmail.com"]
11
+ spec.summary = %q{Adds cache_collection! to jbuilder. Uses memcache fetch_multi/read_multi}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.required_ruby_version = '>= 1.9.3'
21
+
22
+ spec.add_dependency 'jbuilder', '~> 2.0'
23
+
24
+ end
@@ -0,0 +1,5 @@
1
+ require "jbuilder_cache_multi/version"
2
+ require "jbuilder_cache_multi/jbuilder_ext"
3
+
4
+ module JbuilderCacheMulti
5
+ end
@@ -0,0 +1,54 @@
1
+ JbuilderTemplate.class_eval do
2
+
3
+ # Caches a collection of objects using fetch_multi, if supported.
4
+ # Requires a block for each item in the array. Accepts optional 'key' attribute in options (e.g. key: 'v1').
5
+ #
6
+ # Example:
7
+ #
8
+ # json.cache_collection! @people, expires_in: 10.minutes do |person|
9
+ # json.partial! 'person', :person => person
10
+ # end
11
+ def cache_collection!(collection, options = {}, &block)
12
+ if @context.controller.perform_caching
13
+ keys_to_collection_map = _keys_to_collection_map(collection, options)
14
+
15
+ if ::Rails.cache.respond_to?(:fetch_multi)
16
+ results = ::Rails.cache.fetch_multi(*keys_to_collection_map.keys, options) do |key|
17
+ _scope { yield keys_to_collection_map[key] }
18
+ end
19
+ else
20
+ results = keys_to_collection_map.map do |key, item|
21
+ ::Rails.cache.fetch(key, options) { _scope { yield item } }
22
+ end
23
+ end
24
+
25
+ _process_collection_results(results)
26
+ else
27
+ array! collection, options, &block
28
+ end
29
+ end
30
+
31
+
32
+ protected
33
+
34
+ def _keys_to_collection_map(collection, options)
35
+ key = options.delete(:key)
36
+
37
+ collection.inject({}) do |result, item|
38
+ cache_key = key ? [key, item] : item
39
+ result[_cache_key(cache_key, options)] = item
40
+ result
41
+ end
42
+ end
43
+
44
+ def _process_collection_results(results)
45
+ case results
46
+ when ::Hash
47
+ merge! results.values
48
+ else
49
+ merge! results
50
+ end
51
+ end
52
+
53
+
54
+ end
@@ -0,0 +1,3 @@
1
+ module JbuilderCacheMulti
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,104 @@
1
+ require 'test_helper'
2
+ require 'mocha/setup'
3
+ require 'action_view'
4
+ require 'action_view/testing/resolvers'
5
+ require 'active_support/cache'
6
+ require 'jbuilder/jbuilder_template'
7
+ require 'jbuilder_cache_multi'
8
+
9
+
10
+ BLOG_POST_PARTIAL = <<-JBUILDER
11
+ json.extract! blog_post, :id, :body
12
+ json.author do
13
+ name = blog_post.author_name.split(nil, 2)
14
+ json.first_name name[0]
15
+ json.last_name name[1]
16
+ end
17
+ JBUILDER
18
+
19
+ BlogPost = Struct.new(:id, :body, :author_name)
20
+ blog_authors = [ 'David Heinemeier Hansson', 'Pavel Pravosud' ].cycle
21
+ BLOG_POST_COLLECTION = 10.times.map{ |i| BlogPost.new(i+1, "post body #{i+1}", blog_authors.next) }
22
+
23
+ module Rails
24
+ def self.cache
25
+ @cache ||= ActiveSupport::Cache::MemoryStore.new
26
+ end
27
+ end
28
+
29
+ class JbuilderTemplateTest < ActionView::TestCase
30
+ setup do
31
+ @context = self
32
+ Rails.cache.clear
33
+ end
34
+
35
+ def partials
36
+ {
37
+ '_partial.json.jbuilder' => 'json.content "hello"',
38
+ '_blog_post.json.jbuilder' => BLOG_POST_PARTIAL
39
+ }
40
+ end
41
+
42
+ def render_jbuilder(source)
43
+ @rendered = []
44
+ lookup_context.view_paths = [ActionView::FixtureResolver.new(partials.merge('test.json.jbuilder' => source))]
45
+ ActionView::Template.new(source, 'test', JbuilderHandler, :virtual_path => 'test').render(self, {}).strip
46
+ end
47
+
48
+ def undef_context_methods(*names)
49
+ self.class_eval do
50
+ names.each do |name|
51
+ undef_method name.to_sym if self.method_defined?(name.to_sym)
52
+ end
53
+ end
54
+ end
55
+
56
+ def assert_collection_rendered(json, context = nil)
57
+ result = MultiJson.load(json)
58
+ result = result.fetch(context) if context
59
+
60
+ assert_equal 10, result.length
61
+ assert_equal Array, result.class
62
+ assert_equal 'post body 5', result[4]['body']
63
+ assert_equal 'Heinemeier Hansson', result[2]['author']['last_name']
64
+ assert_equal 'Pavel', result[5]['author']['first_name']
65
+ end
66
+
67
+ test 'renders cached array of block partials' do
68
+ undef_context_methods :fragment_name_with_digest, :cache_fragment_name
69
+
70
+ json = render_jbuilder <<-JBUILDER
71
+ json.cache_collection! BLOG_POST_COLLECTION do |blog_post|
72
+ json.partial! 'blog_post', :blog_post => blog_post
73
+ end
74
+ JBUILDER
75
+
76
+ assert_collection_rendered json
77
+ end
78
+
79
+ test 'reverts to cache! if cache does not support fetch_multi' do
80
+ undef_context_methods :fragment_name_with_digest, :cache_fragment_name
81
+ ActiveSupport::Cache::Store.send(:undef_method, :fetch_multi) if ActiveSupport::Cache::Store.method_defined?(:fetch_multi)
82
+
83
+ json = render_jbuilder <<-JBUILDER
84
+ json.cache_collection! BLOG_POST_COLLECTION do |blog_post|
85
+ json.partial! 'blog_post', :blog_post => blog_post
86
+ end
87
+ JBUILDER
88
+
89
+ assert_collection_rendered json
90
+ end
91
+
92
+ test 'reverts to array! when controller.perform_caching is false' do
93
+ controller.perform_caching = false
94
+
95
+ json = render_jbuilder <<-JBUILDER
96
+ json.cache_collection! BLOG_POST_COLLECTION do |blog_post|
97
+ json.partial! 'blog_post', :blog_post => blog_post
98
+ end
99
+ JBUILDER
100
+
101
+ assert_collection_rendered json
102
+ end
103
+
104
+ end
@@ -0,0 +1,10 @@
1
+ require "bundler/setup"
2
+ require "rails/version"
3
+
4
+ if Rails::VERSION::STRING > "4.0"
5
+ require "active_support/testing/autorun"
6
+ else
7
+ require "test/unit"
8
+ end
9
+
10
+ require "active_support/test_case"
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jbuilder_cache_multi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yonah Forst
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jbuilder
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description:
28
+ email:
29
+ - joshblour@hotmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Appraisals
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - gemfiles/rails_3_0.gemfile
41
+ - gemfiles/rails_3_0.gemfile.lock
42
+ - gemfiles/rails_3_1.gemfile
43
+ - gemfiles/rails_3_1.gemfile.lock
44
+ - gemfiles/rails_3_2.gemfile
45
+ - gemfiles/rails_3_2.gemfile.lock
46
+ - gemfiles/rails_4_0.gemfile
47
+ - gemfiles/rails_4_0.gemfile.lock
48
+ - gemfiles/rails_4_1.gemfile
49
+ - gemfiles/rails_4_1.gemfile.lock
50
+ - jbuilder_cache_multi.gemspec
51
+ - lib/jbuilder_cache_multi.rb
52
+ - lib/jbuilder_cache_multi/jbuilder_ext.rb
53
+ - lib/jbuilder_cache_multi/version.rb
54
+ - test/jbuilder_template_test.rb
55
+ - test/test_helper.rb
56
+ homepage: ''
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.9.3
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Adds cache_collection! to jbuilder. Uses memcache fetch_multi/read_multi
80
+ test_files:
81
+ - test/jbuilder_template_test.rb
82
+ - test/test_helper.rb
83
+ has_rdoc: