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

Add liquid 2.5.2 compatibility, make performance fixes. #14

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion lib/puffer_pages/backends/models/layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class PufferPages::Backends::Layout < ActiveRecord::Base
validates_uniqueness_of :name

def self.find_layout(name)
where(:name => name).first
layout = where(:name => name).first
layout.cache_translations if PufferPages.localize
layout
end

def render *args
Expand Down
42 changes: 23 additions & 19 deletions lib/puffer_pages/backends/models/mixins/renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module Backends
module Mixins
module Renderable
extend ActiveSupport::Concern
extend ::NewRelic::Agent::MethodTracer if defined? ::NewRelic

def template
@template ||= ::Liquid::Template.parse(self)
end

private
private


# Method gets some arguments and returns source, context and additional
Expand Down Expand Up @@ -104,24 +105,27 @@ def normalize_context_options options
end

def render_template source, context = {}, additional = {}
template = source.respond_to?(:template) ? source.template : ::Liquid::Template.parse(source)
context = merge_context(context, additional)
self.class.trace_execution_scoped(["Custom/render_template/#{self.name.try(:underscore) || 'other'}"]) do

if context.is_a?(::Liquid::Context)
instrument_render! context do
template.send(render_method, context)
end
else
tracker = PufferPages::Liquid::Tracker.new
context = merge_context(context, registers: {
:file_system => PufferPages::Liquid::FileSystem.new,
:tracker => tracker
})

instrument_render! context do
tracker.cleanup template.send(render_method,
context[:drops].merge!(context[:environment]),
registers: context[:registers])
template = source.respond_to?(:template) ? source.template : ::Liquid::Template.parse(source)
context = merge_context(context, additional)

if context.is_a?(::Liquid::Context)
instrument_render! context do
template.send(render_method, context)
end
else
tracker = PufferPages::Liquid::Tracker.new
context = merge_context(context, registers: {
:file_system => PufferPages::Liquid::FileSystem.new,
:tracker => tracker
})

instrument_render! context do
tracker.cleanup template.send(render_method,
context[:drops].merge!(context[:environment]),
registers: context[:registers])
end
end
end
end
Expand Down Expand Up @@ -150,4 +154,4 @@ def render_method
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/puffer_pages/backends/models/mixins/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def self.globalize_migrator
@globalize_migrator ||= PufferPages::Globalize::Migrator.new(self)
end

define_method :cache_translations do
translations.with_locale(Globalize.fallbacks).each do |translation|
translation_caches[translation.locale.to_sym] = translation
end
end

fields.each do |field|
define_method "#{field}_translations" do
result = translations.each_with_object(HashWithIndifferentAccess.new) do |translation, result|
Expand Down
5 changes: 3 additions & 2 deletions lib/puffer_pages/backends/models/origin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ def import_json json
end

def self.export_json
%w(layouts snippets pages).each_with_object({}) do |table, result|
res = %w(layouts snippets pages).each_with_object({}) do |table, result|
klass = "puffer_pages/#{table}".classify.constantize
result[table] = klass.export_json
end.as_json.to_json
end
MultiJson.dump(res)
end

private
Expand Down
23 changes: 18 additions & 5 deletions lib/puffer_pages/backends/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,22 @@ def self_and_ancestors_page_parts
end

def inherited_page_parts
@inherited_page_parts ||= self_and_ancestors_page_parts.group_by(&:name).map { |(_, group)| group.first }
@inherited_page_parts ||= begin
page_parts = self_and_ancestors_page_parts.group_by(&:name)
if PufferPages.localize
translation_cached = page_parts.values.map { |group| group.first.handler == 'yaml' ? group : group.first }.
flatten.index_by(&:id)
PufferPages::PagePart::Translation.where(page_part_id: translation_cached.keys).
with_locale(Globalize.fallbacks).each do |translation|
translation_cached[translation.page_part_id].translation_caches[translation.locale.to_sym] = translation
end
end
page_parts
end
end

def inherited_page_part name
inherited_page_parts.detect { |part| part.name == name }
inherited_page_parts[name].first
end

def render *args
Expand All @@ -161,9 +172,11 @@ def render *args
end
else
instrument_render! context do
inherited_page_parts.map do |part|
result = part.render context
part.main? ? result : "<% content_for :'#{part.name}' do %>#{result}<% end %>"
inherited_page_parts.values.map(&:first).map do |part|
if part.main? || part.name.in?(%w(keywords title description))
result = part.render context
part.main? ? result : "<% content_for :'#{part.name}' do %>#{result}<% end %>"
end
end.join
end
end
Expand Down
16 changes: 7 additions & 9 deletions lib/puffer_pages/backends/models/page_part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class PufferPages::Backends::PagePart < ActiveRecord::Base

attr_protected

default_scope ->{ includes :translations } if PufferPages.localize

validates_presence_of :name
validates_uniqueness_of :name, :scope => :page_id

Expand Down Expand Up @@ -50,17 +48,17 @@ def additional_render_options
{ environment: { processed: self } }
end

def page_segments
@page_segments ||= page.segments
end

def i18n_scope
i18n_scope_for page.segments, :page_parts, name
i18n_scope_for page_segments
end

def i18n_defaults
page.segments.inject([]) do |memo, element|
memo.push (memo.last || []).dup.push(element)
end.unshift([]).inject([]) do |memo, segments|
memo.unshift i18n_scope_for(segments)
memo.unshift i18n_scope_for(segments, :page_parts, name)
end
page_segments[0..-2].each_with_object([[]]) { |segment, result| result.push result.last + [segment] }.reverse.
map { |segments| i18n_scope_for(segments) }
end

private
Expand Down
4 changes: 3 additions & 1 deletion lib/puffer_pages/backends/models/snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class PufferPages::Backends::Snippet < ActiveRecord::Base
validates_uniqueness_of :name

def self.find_snippet(name)
where(:name => name).first
snippet = where(:name => name).first
snippet.cache_translations if PufferPages.localize
snippet
end

def render *args
Expand Down
5 changes: 4 additions & 1 deletion lib/puffer_pages/handlers/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module PufferPages
module Handlers
class Yaml < Base
def process renderable, context = nil
renderable.self_and_ancestors.where(handler: 'yaml').reverse.each_with_object({}) do |renderable, result|
page_parts = context ? context.registers[:page].inherited_page_parts[renderable.name] :
renderable.self_and_ancestors
page_parts.select { |renderable| renderable.handler == 'yaml' }.reverse.
each_with_object({}) do |renderable, result|
load_arguments = [renderable.render(context)]
load_arguments.push renderable.name if YAML.method(:load).arity == -2
hash = YAML.load *load_arguments
Expand Down
4 changes: 4 additions & 0 deletions lib/puffer_pages/liquid/tags/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def render(context)
''
end

def blank?
false
end

private

def variables_from_string(markup)
Expand Down
4 changes: 4 additions & 0 deletions lib/puffer_pages/liquid/tags/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def render(context)
context.registers[:tracker].register(erb)
end

def blank?
false
end

private

def variables_from_string(markup)
Expand Down
6 changes: 5 additions & 1 deletion lib/puffer_pages/liquid/tags/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def expires_in expiration
end

def cache_key key
ActiveSupport::Cache.expand_cache_key key.unshift('puffer_pages_cache')
Digest::MD5.hexdigest ActiveSupport::Cache.expand_cache_key(key.unshift('puffer_pages_cache'))
end

def cache_store
Expand All @@ -71,6 +71,10 @@ def cache_store
def cache?
PufferPages.config.perform_caching && cache_store
end

def blank?
false
end
end

end
Expand Down
4 changes: 4 additions & 0 deletions lib/puffer_pages/liquid/tags/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def render(context)

context.registers[:tracker].register("<%= image_tag #{@path}, #{attributes} %>")
end

def blank?
false
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/puffer_pages/liquid/tags/include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Tags

class Include < ::Liquid::Include
def render(context)
source = _read_template_from_file_system(context)
source = read_template_from_file_system(context)
variable = context[@variable_name || @template_name[1..-2]]

context.stack do
Expand Down
4 changes: 4 additions & 0 deletions lib/puffer_pages/liquid/tags/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class Javascript < ::Liquid::Block
def render(context)
context.registers[:tracker].register("<%= javascript_tag do %>#{super}<% end %>")
end

def blank?
false
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/puffer_pages/liquid/tags/partials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Partials < Include

private

def _read_template_from_file_system(context)
def read_template_from_file_system(context)
file_system = context.registers[:file_system] || Liquid::Template.file_system
template_name = "#{@tag_name.pluralize}/#{context[@template_name]}"

Expand Down
4 changes: 4 additions & 0 deletions lib/puffer_pages/liquid/tags/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def render(context)
end
%>")
end

def blank?
false
end
end

end
Expand Down
4 changes: 4 additions & 0 deletions lib/puffer_pages/liquid/tags/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def render(context)
super
end
end

def blank?
false
end
end

end
Expand Down
19 changes: 12 additions & 7 deletions lib/puffer_pages/liquid/tags/translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ def render(context)
end
end

if processed && key.first == '.'
I18n.translate i18n_key(processed, key.last(-1)),
options.merge!(:default => i18n_defaults(processed, key.last(-1)))
else
I18n.translate key, options
end
if processed && key.first == '.'
default = i18n_defaults(processed, key.last(-1))
options.merge!(:default => default) if default.any?
I18n.translate i18n_key(processed, key.last(-1)), options
else
I18n.translate key, options
end
end

def i18n_key(processed, key)
Expand All @@ -51,7 +52,11 @@ def i18n_defaults(processed, key)
end

def array_to_key *array
array.flatten.map { |segment| segment.to_s.gsub(?., ?/) }.join(?.).to_sym
array.flatten.map { |segment| segment.to_s.tr(?., ?/) }.join(?.).to_sym
end

def blank?
false
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/puffer_pages/liquid/tags/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def render(context)
context.registers[:controller].send("#{@helper_name}_#{@tag_name}", *arguments, attributes)
end

def blank?
false
end

end

end
Expand Down
4 changes: 4 additions & 0 deletions lib/puffer_pages/liquid/tags/yield.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def render(context)
"<%= yield :'#{@name}' %>" :
"<%= yield %>")
end

def blank?
false
end
end

end
Expand Down
13 changes: 12 additions & 1 deletion lib/puffer_pages/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,35 @@ module PufferPages
class LogSubscriber < ActiveSupport::LogSubscriber
def render_page event
message = " PufferPages: rendered page /#{event.payload[:subject].location} #{duration(event)}"
info message
persistent_log event.payload[:subject].location, event.duration
debug message
end

def render_page_part event
message = " PufferPages: rendered page_part #{event.payload[:subject].name} #{duration(event)}"
persistent_log event.payload[:subject].name, event.duration
debug message
end

def render_layout event
message = " PufferPages: rendered layout #{event.payload[:subject].name} #{duration(event)}"
persistent_log event.payload[:subject].name, event.duration
debug message
end

def render_snippet event
message = " PufferPages: rendered snippet #{event.payload[:subject].name} #{duration(event)}"
persistent_log event.payload[:subject].name, event.duration
debug message
end

def persistent_log id, duration
if Rails.env != 'development'
message = "Template debug - id: #{CmsEngine::DomainConfig.current.locale}_#{id} time: #{duration}"
debug message
end
end

def duration event
'(%.1fms)' % event.duration
end
Expand Down
Loading