<%= cache post do %>
# ActionView::Helpers::CacheHelper
def cache(name = {}, options = {}, &block)
if controller.respond_to?(:perform_caching) && controller.perform_caching
name_options = options.slice(:skip_digest, :virtual_path)
safe_concat(fragment_for(cache_fragment_name(name, name_options), options, &block))
else
yield
end
nil
end
name = {} # --> Is the Post I'm caching
options = {} # Is empty
controller = PostsController
controller.respond_to?(:perform_caching) # => TRUE
controller.perform_caching # => I'm almost certain it's a rails config that's set to true
INTO THE IF STATEMENT
name_options = options.slice(:skip_digest, :virtual_path) # => empty hash since
# options is empty
BREAKING DOWN CACHE_FRAGMENT_NAME ===============
def cache_fragment_name(name = {}, skip_digest: nil, virtual_path: nil, digest_path: nil)
if skip_digest
name
else
fragment_name_with_digest(name, virtual_path, digest_path)
end
end
cache_fragment_name(name, name_options)
name = post instance is still the post we're trying to cache
virtual_path = nil
digest_path = nil
BREAKING DOWN ==========================
# fragment_name_with_digest(name, virtual_path, digest_path)
def fragment_name_with_digest(name, virtual_path, digest_path)
virtual_path ||= @virtual_path
if virtual_path || digest_path
name = controller.url_for(name).split("://").last if name.is_a?(Hash)
digest_path ||= digest_path_from_template(@current_template)
[ digest_path, name ]
else
name
end
end
virtual_path is set to whatever @virtual path is. In this case:
@virtual_path = "posts/_post" !--- component ----! '/post_component'
We also have access to:
@current_template !---component----!
.locals = post !---component----! no method error (this is not a template with locals)
.short_identifier = 'app/views/posts/_post.html.erb !---component----! (this is not a template)
.class = ActionView::Template !---component----! PostComponent.rb
.virtual_path = "posts/_post" !---component----! "/post_component"
--- inside -> if virtual_path || digest_path
name is not a hash, name is still the post instance
BREAKING DOWN =====================
#digest_path_from_template
def digest_path_from_template(template) # :nodoc:
digest = Digestor.digest(name: template.virtual_path, format: template.format, finder: lookup_context, dependencies: view_cache_dependencies)
if digest.present?
"#{template.virtual_path}:#{digest}"
else
template.virtual_path
end
end
template is an ActionView::Template, the same as @current_template !---component----! template is a PostComponent.rb instance
template.short_identifier = 'app/views/posts/_post.html.erb' # for reference
The result from Digestor.digest(...) is 586517e78be822fa6f6e60856e002275
!---component----!
COMPONENT'S RESULT FROM DIGESTOR IS AN EMPTY STRING!!!
!---component----!
"#{template.virtual_path}:#{digest}" == 'posts/_post:586517e78be822fa6f6e60856e002275'
RETURNED TO fragment_name_with_digest ==============
[ digest_path, name ] = [ 'posts/_post:586517e78be822fa6f6e60856e002275', the_same_post_were_using_to_cache ]
RETURNED TO `cache`=========================
GOING INTO ================================
fragment_for with the return from cache_fragment_name
which is the return of fragment_name_with_digest, ergo:
[ digest_path, name ] = [ 'posts/_post:586517e78be822fa6f6e60856e002275', the_same_post_were_using_to_cache ]
def fragment_for(name = {}, options = nil, &block)
if content = read_fragment_for(name, options)
@view_renderer.cache_hits[@virtual_path] = :hit if defined?(@view_renderer)
content
else
@view_renderer.cache_hits[@virtual_path] = :miss if defined?(@view_renderer)
write_fragment_for(name, options, &block)
end
end
content = read_fragment_for(name, options) is nil because there's nothing cached
in the db at this point.
@view_renderer is defined => ActionView::Renderer
.lookup_context =>
.view_paths
.paths => app/views, actiontext/app/views, actionmailbox/app/views
write_fragment_for(name, options, &block) #=> will write the cache.
# I'll stop here since I don't think writing the cache has anyting to do with
# the view-component caching issue