Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cell's name instead of controller_path in cache key generation #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/trailblazer/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def view_name
@view_name ||= _view_name
end

# Computes the complete, namespaced cache key for +state+.
def state_cache_key(state, key_parts={})
expand_cache_key([util.underscore(name), state, key_parts])
end

include ViewName::Path
end

Expand Down
38 changes: 38 additions & 0 deletions test/cell_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
require "test_helper"
require "cells-erb"

module CacheStore
STORE = Class.new(Hash) do
def fetch(key, *)
value = self[key] || self[key] = yield
value
end
end.new

def cache_store
STORE
end
end

module Post
module Cell
class New < Trailblazer::Cell
Expand All @@ -10,12 +23,18 @@ class Show < Trailblazer::Cell
class SideBar < Trailblazer::Cell
self.view_paths = ["test/concepts"]
include ::Cell::Erb

include CacheStore
cache :show, expires_in: 10
end

class FlatSideBar < Trailblazer::Cell
self.view_paths = ["test/concepts"]
include ::Cell::Erb
extend ViewName::Flat

include CacheStore
cache :show
end
end
end
Expand Down Expand Up @@ -78,4 +97,23 @@ class CellTest < Minitest::Test
describe "flat layout" do
it { Post::Cell::Show::FlatSideBar.new(nil, layout: Post::Cell::Layout).().must_equal "layout{$flat_side_bar\n}\n" }
end

describe "::state_cache_key" do
it { Post::Cell::Show::SideBar.state_cache_key(:show, expiry: 1).must_equal "post/cell/show/side_bar/show/{:expiry=>1}" }
it { Post::Cell::Show::FlatSideBar.state_cache_key(:show).must_equal "post/cell/show/flat_side_bar/show/{}" }
end

describe "cache store" do
it do
Post::Cell::Show::SideBar.new(nil).()
Post::Cell::Show::FlatSideBar.new(nil).()

CacheStore::STORE.must_equal({
"post/cell/show/side_bar/show/"=>%{$side_bar
},
"post/cell/show/flat_side_bar/show/"=>%{$flat_side_bar
}
})
end
end
end