Compare commits
3 Commits
master
...
fragment-c
Author | SHA1 | Date |
---|---|---|
ccocchi | 4dae04896d | |
ccocchi | 4cf10fb8f6 | |
ccocchi | 79d6bb9c81 |
|
@ -6,6 +6,7 @@ require 'active_support/core_ext/class/attribute_accessors'
|
|||
|
||||
require 'rabl-rails/version'
|
||||
require 'rabl-rails/template'
|
||||
require 'rabl-rails/fragment'
|
||||
require 'rabl-rails/compiler'
|
||||
|
||||
require 'rabl-rails/renderer'
|
||||
|
|
|
@ -6,6 +6,7 @@ module RablRails
|
|||
class Compiler
|
||||
def initialize
|
||||
@glue_count = 0
|
||||
@cache_count = 0
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -124,6 +125,17 @@ module RablRails
|
|||
t = Library.instance.get(path)
|
||||
@template.merge!(t.source)
|
||||
end
|
||||
|
||||
#
|
||||
# cache key: ->(resource) { |resource| resource.cache_key }, expires_in: 1800
|
||||
#
|
||||
def cache(options = {}, &block)
|
||||
return unless block_given?
|
||||
key = options.delete(:key)
|
||||
source = sub_compile { yield }
|
||||
@template[:"_cache#{@cache_count}"] = Fragment.new(key, source, options)
|
||||
@cache_count += 1
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
|
@ -148,11 +160,11 @@ module RablRails
|
|||
end
|
||||
end
|
||||
|
||||
def sub_compile(data)
|
||||
def sub_compile(data = nil)
|
||||
return {} unless block_given?
|
||||
old_template, @template = @template, {}
|
||||
yield
|
||||
@template.merge!(:_data => data)
|
||||
data ? @template.merge!(:_data => data) : @template
|
||||
ensure
|
||||
@template = old_template
|
||||
end
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
require 'active_support/cache'
|
||||
|
||||
module RablRails
|
||||
class Fragment
|
||||
attr_reader :compiled_source, :options
|
||||
|
||||
def initialize(key, source, options = {})
|
||||
@key = key
|
||||
@compiled_source = source
|
||||
@options = options
|
||||
end
|
||||
|
||||
def expand_cache_key(data)
|
||||
if @key
|
||||
@key.respond_to?(:call) ? @key.call(data) : @key
|
||||
else
|
||||
ActiveSupport::Cache.expand_cache_key(data, 'rabl')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# cache key: ->(resource) { |resource| resource.cache_key }, expires_in: 1800
|
||||
#
|
||||
# def cache(options = {}, &block)
|
||||
# return unless block_given?
|
||||
# key = options.delete(:key)
|
||||
# source = sub_compiler { compile_block(&block) }
|
||||
# @template[:"_cache#{cache_count}"] = Fragment.new(key, source, options)
|
||||
# end
|
||||
|
||||
#
|
||||
# when Fragment
|
||||
# render_fragment(value)
|
||||
#
|
||||
|
||||
# def render_fragment(data, fragment)
|
||||
# Rails.cache.fetch(fragment.expand_cache_key(data), fragment.options) do
|
||||
# render_resource(data, fragment.compiled_source)
|
||||
# end
|
||||
# end
|
|
@ -62,6 +62,12 @@ module RablRails
|
|||
else # child
|
||||
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
|
||||
end
|
||||
when Fragment
|
||||
cache_output = Rails.cache.fetch(value.expand_cache_key(data), value.options) do
|
||||
data.respond_to?(:each) ? render_collection(data, value.compiled_source) : render_resource(data, value.compiled_source)
|
||||
end
|
||||
output.merge!(cache_output)
|
||||
next output
|
||||
end
|
||||
output[key] = out
|
||||
output
|
||||
|
|
|
@ -149,4 +149,14 @@ class CompilerTest < ActiveSupport::TestCase
|
|||
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
|
||||
assert_equal false, t.data
|
||||
end
|
||||
|
||||
test "cache are compiled to fragment" do
|
||||
t = @compiler.compile_source(%{
|
||||
cache do
|
||||
attributes :name
|
||||
end
|
||||
})
|
||||
assert_instance_of RablRails::Fragment, t.source[:_cache0]
|
||||
assert_equal({ :name => :name }, t.source[:_cache0].compiled_source)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class FragmentCacheTest < ActiveSupport::TestCase
|
||||
end
|
Loading…
Reference in New Issue