curly-templates 2.1.1 → 2.2.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: c88276c903b21a344ef66730915ad49c862b6b74
4
- data.tar.gz: e302028fc3a83e6f39c83ed30d129f432888cb35
3
+ metadata.gz: 00132e15e5f5616e89dcda67d5b0bd6177cdb6a4
4
+ data.tar.gz: 25d9aecdc4dcfab2d2e04844636a5a1631a74844
5
5
  SHA512:
6
- metadata.gz: 317419189ff20063face87bb7845da75c4f37e85f60188a745cba7d4079cfdaeb0df3b53f554684a5a611c75af2ff204a06251f6ec718cfe9c8d57cf0180c3dc
7
- data.tar.gz: 1a437609e0dd076bf321d430cb43316424b5ca1adeb3ad9704567041d42dc26012fa2161dff8bad804d969fc4c514c878f7a3e9e5a7d98e232142d3358afa45a
6
+ metadata.gz: e1a43cdcf185be6ee43ebfb6512e7cb16482f3c081ff966fabc5d98169a4de47c87acf91b139a7f7ca26ff4bfd7ca960dfe82236f67cc3956fdcdaa9e3f8ded6
7
+ data.tar.gz: b2f5cba338b6bf4d275674195f474925acee32680fd4d0f118d181311cbf107a52449c10abec7f7bdae10b0c121194896f4f5dc2e61049d010a15948cbfd7062
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ### Unreleased
2
2
 
3
+ ### Curly 2.2.0 (December 4, 2014)
4
+
5
+ * Allow configuring arbitrary cache options.
6
+
7
+ *Daniel Schierbeck*
8
+
3
9
  ### Curly 2.1.1 (November 12, 2014)
4
10
 
5
11
  * Fix a bug where a parent presenter's parameters were not being passed to the
data/CONTRIBUTING.md CHANGED
@@ -4,7 +4,7 @@ In order to keep the Curly code base nice and tidy, please observe these best pr
4
4
  - Document any unclear things in the code. Even better, don't make the code do unclear things.
5
5
  - Use the coding style already present in the code base.
6
6
  - Make your commit messages precise and to the point. Add a short summary (50 chars max) followed by a blank line and then a longer description, if necessary, e.g.
7
-
7
+
8
8
  > Make invalid references raise an exception
9
9
  >
10
10
  > In order to avoid nasty errors when doing stuff, make the Curly compiler
data/README.md CHANGED
@@ -71,13 +71,13 @@ class Posts::CommentPresenter < Curly::Presenter
71
71
  presents :comment
72
72
 
73
73
  def body
74
- BlueCloth.new(@comment.body).to_html
74
+ SafeMarkdown.render(@comment.body)
75
75
  end
76
76
 
77
77
  def author_link
78
78
  link_to @comment.author.name, @comment.author, rel: "author"
79
79
  end
80
-
80
+
81
81
  def deletion_link
82
82
  link_to "Delete", @comment, method: :delete
83
83
  end
@@ -85,7 +85,7 @@ class Posts::CommentPresenter < Curly::Presenter
85
85
  def time_ago
86
86
  time_ago_in_words(@comment.created_at)
87
87
  end
88
-
88
+
89
89
  def author?
90
90
  @comment.author == current_user
91
91
  end
@@ -268,7 +268,7 @@ the template inline. A simple template could look like:
268
268
  <b>Name: </b> {{name_field}}<br>
269
269
  <b>E-mail: </b> {{email_field}}<br>
270
270
  {{comment_field}}
271
-
271
+
272
272
  {{submit_button}}
273
273
  {{/comment_form}}
274
274
  ```
@@ -284,7 +284,7 @@ class PostPresenter < Curly::Presenter
284
284
  presents :post
285
285
  def title; @post.title; end
286
286
  def body; markdown(@post.body); end
287
-
287
+
288
288
  # A context block method *must* take a block argument. The return value
289
289
  # of the method will be used when rendering. Calling the block argument will
290
290
  # render the nested template. If you pass a value when calling the block
@@ -292,7 +292,7 @@ class PostPresenter < Curly::Presenter
292
292
  def comment_form(&block)
293
293
  form_for(Comment.new, &block)
294
294
  end
295
-
295
+
296
296
  # The presenter name is automatically deduced.
297
297
  class CommentFormPresenter < Curly::Presenter
298
298
  # The value passed to the block argument will be passed in a parameter named
@@ -404,18 +404,18 @@ a component:
404
404
  ```ruby
405
405
  class Posts::ShowPresenter < Curly::Presenter
406
406
  presents :post
407
-
407
+
408
408
  def title
409
409
  @post.title
410
410
  end
411
-
411
+
412
412
  def author_link
413
413
  # You can call any Rails helper from within a presenter instance:
414
414
  link_to author.name, profile_path(author), rel: "author"
415
415
  end
416
-
416
+
417
417
  private
418
-
418
+
419
419
  # Private methods are not available to the template, so they're safe to
420
420
  # use.
421
421
  def author
@@ -464,12 +464,12 @@ class ApplicationLayout < Curly::Presenter
464
464
  def title
465
465
  "You can use methods just like in any other presenter!"
466
466
  end
467
-
467
+
468
468
  def sidebar
469
469
  # A view can call `content_for(:sidebar) { "some HTML here" }`
470
470
  yield :sidebar
471
471
  end
472
-
472
+
473
473
  def body
474
474
  # The view will be rendered and inserted here:
475
475
  yield
@@ -584,6 +584,19 @@ class Posts::ShowPresenter < Curly::Presenter
584
584
  end
585
585
  ```
586
586
 
587
+ In order to set *any* cache option, define a `#cache_options` method that
588
+ returns a Hash of options:
589
+
590
+ ```ruby
591
+ class Posts::ShowPresenter < Curly::Presenter
592
+ ...
593
+
594
+ def cache_options
595
+ { compress: true, namespace: "my-app" }
596
+ end
597
+ end
598
+ ```
599
+
587
600
 
588
601
  ### Static Caching
589
602
 
@@ -4,8 +4,8 @@ Gem::Specification.new do |s|
4
4
  s.rubygems_version = '1.3.5'
5
5
 
6
6
  s.name = 'curly-templates'
7
- s.version = '2.1.1'
8
- s.date = '2014-11-12'
7
+ s.version = '2.2.0'
8
+ s.date = '2014-12-04'
9
9
 
10
10
  s.summary = "Free your views!"
11
11
  s.description = "A view layer for your Rails apps that separates structure and logic."
data/lib/curly.rb CHANGED
@@ -24,9 +24,8 @@
24
24
  # end
25
25
  #
26
26
  # See Curly::Presenter for more information on presenters.
27
- #
28
27
  module Curly
29
- VERSION = "2.1.1"
28
+ VERSION = "2.2.0"
30
29
 
31
30
  # Compiles a Curly template to Ruby code.
32
31
  #
data/lib/curly/parser.rb CHANGED
@@ -67,7 +67,7 @@ class Curly::Parser
67
67
  false
68
68
  end
69
69
  end
70
-
70
+
71
71
  class Block
72
72
  attr_reader :type, :component, :nodes
73
73
 
@@ -41,7 +41,6 @@ module Curly
41
41
  #
42
42
  # context - An ActionView::Base context.
43
43
  # options - A Hash of options given to the presenter.
44
- #
45
44
  def initialize(context, options = {})
46
45
  @_context = context
47
46
  options.stringify_keys!
@@ -63,7 +62,7 @@ module Curly
63
62
  # template is rendered. One use case is to call `content_for` in order
64
63
  # to inject content into other templates, e.g. a layout.
65
64
  #
66
- # Example
65
+ # Examples
67
66
  #
68
67
  # class Posts::ShowPresenter < Curly::Presenter
69
68
  # presents :post
@@ -96,10 +95,26 @@ module Curly
96
95
  nil
97
96
  end
98
97
 
98
+ # The options that should be passed to the cache backend when caching the
99
+ # view. The exact options may vary depending on the backend you're using.
100
+ #
101
+ # The most common option is `:expires_in`, which controls the duration of
102
+ # time that the cached view should be considered fresh. Because it's so
103
+ # common, you can set that option simply by defining `#cache_duration`.
104
+ #
105
+ # Note: if you set the `:expires_in` option through this method, the
106
+ # `#cache_duration` value will be ignored.
107
+ #
108
+ # Returns a Hash.
109
+ def cache_options
110
+ {}
111
+ end
112
+
99
113
  # The duration that the view should be cached for. Only relevant if
100
114
  # `#cache_key` returns a non nil value.
101
115
  #
102
- # If nil, the view will not have an expiration time set.
116
+ # If nil, the view will not have an expiration time set. See also
117
+ # `#cache_options` for a more flexible way to set cache options.
103
118
  #
104
119
  # Examples
105
120
  #
@@ -173,13 +188,14 @@ module Curly
173
188
  end
174
189
  end
175
190
 
176
- # Whether a component is available to templates rendered with the presenter.
191
+ # Whether a component is available to templates rendered with the
192
+ # presenter.
177
193
  #
178
194
  # Templates have components which correspond with methods defined on
179
195
  # the presenter. By default, only public instance methods can be
180
196
  # referenced, and any method defined on Curly::Presenter itself cannot be
181
- # referenced. This means that methods such as `#cache_key` and #inspect are
182
- # not available. This is done for safety purposes.
197
+ # referenced. This means that methods such as `#cache_key` and #inspect
198
+ # are not available. This is done for safety purposes.
183
199
  #
184
200
  # This policy can be changed by overriding this method in your presenters.
185
201
  #
@@ -201,7 +217,7 @@ module Curly
201
217
 
202
218
  # The set of view paths that the presenter depends on.
203
219
  #
204
- # Example
220
+ # Examples
205
221
  #
206
222
  # class Posts::ShowPresenter < Curly::Presenter
207
223
  # version 2
@@ -241,7 +257,8 @@ module Curly
241
257
  @version || 0
242
258
  end
243
259
 
244
- # The cache key for the presenter class. Includes all dependencies as well.
260
+ # The cache key for the presenter class. Includes all dependencies as
261
+ # well.
245
262
  #
246
263
  # Returns a String cache key.
247
264
  def cache_key
data/lib/curly/scanner.rb CHANGED
@@ -8,7 +8,6 @@ module Curly
8
8
  #
9
9
  # The Scanner goes through the template piece by piece, extracting tokens
10
10
  # until the end of the template is reached.
11
- #
12
11
  class Scanner
13
12
  CURLY_START = /\{\{/
14
13
  CURLY_END = /\}\}/
@@ -27,7 +26,7 @@ module Curly
27
26
  #
28
27
  # source - The String source of the template.
29
28
  #
30
- # Example
29
+ # Examples
31
30
  #
32
31
  # Curly::Scanner.scan("hello {{name}}!")
33
32
  # #=> [[:text, "hello "], [:component, "name"], [:text, "!"]]
@@ -27,11 +27,10 @@ class Curly::TemplateHandler
27
27
  presenter_key = nil
28
28
  end
29
29
 
30
- options = {
31
- expires_in: presenter.cache_duration
32
- }
30
+ cache_options = presenter.cache_options || {}
31
+ cache_options[:expires_in] ||= presenter.cache_duration
33
32
 
34
- context.cache([key, presenter_key].compact, options) do
33
+ context.cache([key, presenter_key].compact, cache_options) do
35
34
  yield
36
35
  end
37
36
  else
@@ -7,6 +7,7 @@ describe Curly::TemplateHandler do
7
7
  @context = context
8
8
  @cache_key = options.fetch(:cache_key, nil)
9
9
  @cache_duration = options.fetch(:cache_duration, nil)
10
+ @cache_options = options.fetch(:cache_options, {})
10
11
  end
11
12
 
12
13
  def setup!
@@ -29,6 +30,10 @@ describe Curly::TemplateHandler do
29
30
  @cache_duration
30
31
  end
31
32
 
33
+ def cache_options
34
+ @cache_options
35
+ end
36
+
32
37
  def self.component_available?(method)
33
38
  true
34
39
  end
@@ -179,6 +184,23 @@ describe Curly::TemplateHandler do
179
184
  context.advance_clock(1)
180
185
  output.should == "FOO"
181
186
  end
187
+
188
+ it "passes #cache_options to the cache backend" do
189
+ context.assigns[:cache_key] = "x"
190
+ context.assigns[:cache_options] = { expires_in: 42 }
191
+
192
+ output.should == "BAR"
193
+
194
+ context.stub(:bar) { "FOO" }
195
+
196
+ # Cached fragment has not yet expired.
197
+ context.advance_clock(41)
198
+ output.should == "BAR"
199
+
200
+ # Now it has! Huzzah!
201
+ context.advance_clock(1)
202
+ output.should == "FOO"
203
+ end
182
204
  end
183
205
 
184
206
  def output
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curly-templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schierbeck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-12 00:00:00.000000000 Z
11
+ date: 2014-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack