From 5f21a40d7443ba574e1c3c39e50a577a10758bf8 Mon Sep 17 00:00:00 2001 From: Bob Dutcheshen Date: Thu, 25 Jan 2018 17:12:09 -0700 Subject: [PATCH 1/3] add :force option to cache! --- README.md | 2 +- gemfiles/.bundle/config | 2 ++ lib/jbuilder/jbuilder_template.rb | 3 ++- test/jbuilder_template_test.rb | 31 +++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 gemfiles/.bundle/config diff --git a/README.md b/README.md index 8580401..843c08c 100644 --- a/README.md +++ b/README.md @@ -222,7 +222,7 @@ Fragment caching is supported, it uses `Rails.cache` and works like caching in HTML templates: ```ruby -json.cache! ['v1', @person], expires_in: 10.minutes do +json.cache! ['v1', @person], expires_in: 10.minutes, force: true do json.extract! @person, :name, :age end ``` diff --git a/gemfiles/.bundle/config b/gemfiles/.bundle/config new file mode 100644 index 0000000..c127f80 --- /dev/null +++ b/gemfiles/.bundle/config @@ -0,0 +1,2 @@ +--- +BUNDLE_RETRY: "1" diff --git a/lib/jbuilder/jbuilder_template.rb b/lib/jbuilder/jbuilder_template.rb index 09a67c3..a82cdf0 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -130,7 +130,8 @@ def _render_partial(options) def _cache_fragment_for(key, options, &block) key = _cache_key(key, options) - _read_fragment_cache(key, options) || _write_fragment_cache(key, options, &block) + force = options[:force].present? + (!force && _read_fragment_cache(key, options)) || _write_fragment_cache(key, options, &block) end def _read_fragment_cache(key, options = nil) diff --git a/test/jbuilder_template_test.rb b/test/jbuilder_template_test.rb index d8bfc41..6f33410 100644 --- a/test/jbuilder_template_test.rb +++ b/test/jbuilder_template_test.rb @@ -289,6 +289,37 @@ def assert_collection_rendered(result, context = nil) assert_equal "Miss", result["test2"] end + test "fragment caching a JSON object with force option" do + undef_context_methods :fragment_name_with_digest, :cache_fragment_name + + jbuild <<-JBUILDER + json.cache! "cachekey" do + json.test1 "Value" + end + JBUILDER + + result = jbuild(<<-JBUILDER) + json.cache! "cachekey", force: false do + json.test1 "New Value" + end + JBUILDER + assert_equal "Value", result["test1"] + + result = jbuild(<<-JBUILDER) + json.cache! "cachekey", force: true do + json.test1 "New Value" + end + JBUILDER + assert_equal "New Value", result["test1"] + result = jbuild(<<-JBUILDER) + json.cache! "cachekey" do + json.test1 "Cache Miss" + end + JBUILDER + assert_equal "New Value", result["test1"] + puts result + end + test "fragment caching deserializes an array" do undef_context_methods :fragment_name_with_digest, :cache_fragment_name From 0e03dec4bfdbb289199c46f5ee9106413a3fdc63 Mon Sep 17 00:00:00 2001 From: Bob Dutcheshen Date: Tue, 23 Jul 2019 17:06:53 -0700 Subject: [PATCH 2/3] fix spec remove deprecated method --- test/jbuilder_template_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/jbuilder_template_test.rb b/test/jbuilder_template_test.rb index 8bd297e..645d694 100644 --- a/test/jbuilder_template_test.rb +++ b/test/jbuilder_template_test.rb @@ -159,7 +159,6 @@ class JbuilderTemplateTest < ActiveSupport::TestCase end test "fragment caching a JSON object with force option" do - undef_context_methods :fragment_name_with_digest, :cache_fragment_name jbuild <<-JBUILDER json.cache! "cachekey" do From 140030ae6fff8e38fd21e849ecf708a7614201bd Mon Sep 17 00:00:00 2001 From: Bob Dutcheshen Date: Wed, 24 Jul 2019 10:05:40 -0700 Subject: [PATCH 3/3] fix fragment caching with force test --- test/jbuilder_template_test.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/jbuilder_template_test.rb b/test/jbuilder_template_test.rb index 645d694..6258876 100644 --- a/test/jbuilder_template_test.rb +++ b/test/jbuilder_template_test.rb @@ -160,32 +160,31 @@ class JbuilderTemplateTest < ActiveSupport::TestCase test "fragment caching a JSON object with force option" do - jbuild <<-JBUILDER + render(<<-JBUILDER) json.cache! "cachekey" do json.test1 "Value" end JBUILDER - result = jbuild(<<-JBUILDER) + result = render(<<-JBUILDER) json.cache! "cachekey", force: false do json.test1 "New Value" end JBUILDER assert_equal "Value", result["test1"] - result = jbuild(<<-JBUILDER) + result = render(<<-JBUILDER) json.cache! "cachekey", force: true do json.test1 "New Value" end JBUILDER assert_equal "New Value", result["test1"] - result = jbuild(<<-JBUILDER) + result = render(<<-JBUILDER) json.cache! "cachekey" do json.test1 "Cache Miss" end JBUILDER assert_equal "New Value", result["test1"] - puts result end test "object fragment caching with expiry" do