jbuilder 2.0.3 → 2.0.4

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: 17575b4f4e87733b1e46d7762554f5387904cbd9
4
- data.tar.gz: 75c4a40da60c398469b52b1f2aeede6b7daf4527
3
+ metadata.gz: 4ac0617f83abd9f18566f7abb54b4bfb40f43292
4
+ data.tar.gz: 81ee411aedba8b8532011a4d02b30a287352aefc
5
5
  SHA512:
6
- metadata.gz: 221cc1da2d52352427b3631dbc7204db44bdcf0b71b2964afbe6cf527731f456f774b2b5cbeafacb4d6a00f898313152d1823c937593e22457fe01a1aa56442d
7
- data.tar.gz: 49801cf8ba732d8a6cea0586ae0b31bbc2bebb1a1ade669aacf89e7505fca992570710379f1916eb0b71377024447295dd8e5dd2fa85e4c306c55254ee89d998
6
+ metadata.gz: 9775756c8e0bceed5f9187629e23c50b11440c6d98f96831c90e0763319b540437d353be56737132a383a15514d1348b032d512448cee6bda4b9c76bae03173c
7
+ data.tar.gz: d56cca82eb949371a3dc93a4a6e2820de63b58426f9256907473874c46e6448c29dd96479115cd56ba26cff59f9c9a5aa30ae9dd253d47e7991639922c19e306
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ 2.0.4
4
+ -----
5
+ * [Add cache_if! to conditionally cache JSON fragments](https://github.com/rails/jbuilder/commit/commit/14a5afd8a2c939a6fd710d355a194c114db96eb2)
6
+
3
7
  2.0.3
4
8
  -----
5
9
  * [Pass options when calling cache_fragment_name](https://github.com/rails/jbuilder/commit/07c2cc7486fe9ef423d7bc821b83f6d485f330e0)
data/README.md CHANGED
@@ -182,6 +182,14 @@ json.cache! ['v1', @person], expires_in: 10.minutes do
182
182
  end
183
183
  ```
184
184
 
185
+ You can also conditionally cache a block by using `cache_if!` like this:
186
+
187
+ ```ruby
188
+ json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
189
+ json.extract! @person, :name, :age
190
+ end
191
+ ```
192
+
185
193
  Keys can be auto formatted using `key_format!`, this can be used to convert keynames from the standard ruby_format to CamelCase:
186
194
 
187
195
  ``` ruby
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jbuilder'
3
- s.version = '2.0.3'
3
+ s.version = '2.0.4'
4
4
  s.author = 'David Heinemeier Hansson'
5
5
  s.email = 'david@37signals.com'
6
6
  s.summary = 'Create JSON structures via a Builder-style DSL'
@@ -60,6 +60,19 @@ class JbuilderTemplate < Jbuilder
60
60
  end
61
61
  end
62
62
 
63
+ # Conditionally catches the json depending in the condition given as first parameter. Has the same
64
+ # signature as the `cache` helper method in `ActionView::Helpers::CacheHelper` and so can be used in
65
+ # the same way.
66
+ #
67
+ # Example:
68
+ #
69
+ # json.cache_if! !admin?, @person, expires_in: 10.minutes do
70
+ # json.extract! @person, :name, :age
71
+ # end
72
+ def cache_if!(condition, *args, &block)
73
+ condition ? cache!(*args, &block) : yield
74
+ end
75
+
63
76
  protected
64
77
  def _handle_partial_options(options)
65
78
  options.reverse_merge! locals: {}
@@ -185,6 +185,32 @@ class JbuilderTemplateTest < ActionView::TestCase
185
185
  assert_equal 'Cache', parsed['name']
186
186
  end
187
187
 
188
+ test 'conditionally fragment caching a JSON object' do
189
+ undef_context_methods :fragment_name_with_digest, :cache_fragment_name
190
+
191
+ render_jbuilder <<-JBUILDER
192
+ json.cache_if! true, 'cachekey' do
193
+ json.test1 'Cache'
194
+ end
195
+ json.cache_if! false, 'cachekey' do
196
+ json.test2 'Cache'
197
+ end
198
+ JBUILDER
199
+
200
+ json = render_jbuilder <<-JBUILDER
201
+ json.cache_if! true, 'cachekey' do
202
+ json.test1 'Miss'
203
+ end
204
+ json.cache_if! false, 'cachekey' do
205
+ json.test2 'Miss'
206
+ end
207
+ JBUILDER
208
+
209
+ parsed = MultiJson.load(json)
210
+ assert_equal 'Cache', parsed['test1']
211
+ assert_equal 'Miss', parsed['test2']
212
+ end
213
+
188
214
  test 'fragment caching deserializes an array' do
189
215
  undef_context_methods :fragment_name_with_digest, :cache_fragment_name
190
216
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-17 00:00:00.000000000 Z
11
+ date: 2014-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 2.2.1
90
+ rubygems_version: 2.2.2
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Create JSON structures via a Builder-style DSL