From 1fb75e0a9d4f842e5fdd87a51392681879b72f9d Mon Sep 17 00:00:00 2001 From: Taketo Takashima Date: Thu, 2 Mar 2023 11:22:10 +0900 Subject: [PATCH 1/3] Added cache test with cache condtion helper method --- test/cache_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/cache_test.rb b/test/cache_test.rb index 786032fe..35859907 100644 --- a/test/cache_test.rb +++ b/test/cache_test.rb @@ -110,4 +110,24 @@ def cache_store # cache hit for the second render _(SongCell.new.(:show, "Album", title: "IT", part: "1")).must_equal("Album IT 1") end + + it "with cache condition helper method" do + WithCondition = Class.new(Cell::ViewModel) do + cache :show, if: :enable_cache?, expires_in: 10 + + def show + "Test" + end + + def cache_store + STORE + end + + def enable_cache? + true + end + end + + _(WithCondition.new.(:show)).must_equal("Test") + end end From a31a3f4f4b6ba36092d6413ddad091436a74e065 Mon Sep 17 00:00:00 2001 From: Taketo Takashima Date: Thu, 2 Mar 2023 11:23:00 +0900 Subject: [PATCH 2/3] Fixed caching render_state method with keyword args --- lib/cell/caching.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cell/caching.rb b/lib/cell/caching.rb index 3cd516af..05965976 100644 --- a/lib/cell/caching.rb +++ b/lib/cell/caching.rb @@ -43,7 +43,7 @@ def render_state(state, *args, **kws) state = state.to_sym # Before Ruby 3.0, this wasn't necessary, but since cache filters don't receive kwargs as per the "old" (existing cells version) implementation, we can make it one array. - cache_filter_args = args + [kws] + cache_filter_args = args + [**kws] return super(state, *args, **kws) unless cache?(state, *cache_filter_args) From 23984c823e5168dce1821929967170b8b2e710ff Mon Sep 17 00:00:00 2001 From: Taketo Takashima Date: Thu, 2 Mar 2023 17:08:07 +0900 Subject: [PATCH 3/3] Fixed caching render_state method with keyword args for ruby < 2.7 --- lib/cell/caching.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/cell/caching.rb b/lib/cell/caching.rb index 05965976..149719f1 100644 --- a/lib/cell/caching.rb +++ b/lib/cell/caching.rb @@ -43,7 +43,12 @@ def render_state(state, *args, **kws) state = state.to_sym # Before Ruby 3.0, this wasn't necessary, but since cache filters don't receive kwargs as per the "old" (existing cells version) implementation, we can make it one array. - cache_filter_args = args + [**kws] + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0') + cache_filter_args = args + [**kws] + else + cache_filter_args = args + cache_filter_args += [**kws] if kws.size > 0 + end return super(state, *args, **kws) unless cache?(state, *cache_filter_args)