finally removed all the references to our internal events system when parsing a liquid template (using activesupport notifications now)

did committed May 17, 2015
commit adf2045bf848d080ee4292f14489a95d23d44410
Showing 9 changed files with 29 additions and 63 deletions
locomotive/steam/liquid/tags/extends.rb b/lib/locomotive/steam/liquid/tags/extends.rb +1 -36
@@ @@ -12,13 +12,10 @@ module Locomotive
# no need to go further if the parent does not exist
raise PageNotFound.new("Page with fullpath '#{@template_name}' was not found") if parent.nil?
- if listener = options[:events_listener]
- listener.emit(:extends, page: options[:page], parent: parent)
- end
+ ActiveSupport::Notifications.instrument("steam.parse.extends", page: options[:page], parent: parent)
# the source has already been parsed before
options[:parser]._parse(parent, options.merge(page: parent))
- # parent.template || ::Liquid::Template.parse(parent.source, options.merge(page: parent))
end
end
@@ @@ -28,35 +25,3 @@ module Locomotive
end
end
end
-
- # def prepare_parsing
- # super
-
- # parent_page = @context[:parent_page]
-
- # @context[:page].merge_editable_elements_from_page(parent_page)
-
- # @context[:snippets] = parent_page.snippet_dependencies
- # @context[:templates] = ([*parent_page.template_dependencies] + [parent_page.id]).compact
- # end
-
- # if @template_name == 'parent'
- # @context[:parent_page] = @context[:cached_parent] || @context[:page].parent
- # else
- # locale = ::Mongoid::Fields::I18n.locale
-
- # @context[:parent_page] = @context[:cached_pages].try(:[], @template_name) ||
- # @context[:site].pages.where("fullpath.#{locale}" => @template_name).first
- # end
-
- # raise PageNotFound.new("Page with fullpath '#{@template_name}' was not found") if @context[:parent_page].nil?
-
- # # be sure to work with a copy of the parent template otherwise there will be conflicts
- # parent_template = @context[:parent_page].template.try(:clone)
-
- # raise PageNotTranslated.new("Page with fullpath '#{@template_name}' was not translated") if parent_template.nil?
-
- # # force the page to restore the original version of its template (from the serialized version)
- # @context[:parent_page].instance_variable_set(:@template, nil)
-
- # parent_template
locomotive/steam/liquid/tags/inherited_block.rb b/lib/locomotive/steam/liquid/tags/inherited_block.rb +10 -4
@@ @@ -6,9 +6,7 @@ module Locomotive
def parse(tokens)
super.tap do
- if listener = options[:events_listener]
- listener.emit(:inherited_block, page: options[:page], name: @name, found_super: self.contains_super?(nodelist))
- end
+ ActiveSupport::Notifications.instrument("steam.parse.inherited_block", page: options[:page], name: @name, found_super: self.contains_super?(nodelist))
end
end
@@ @@ -18,7 +16,7 @@ module Locomotive
nodes.any? do |node|
if is_node_block_super?(node)
true
- elsif node.respond_to?(:nodelist) && !node.nodelist.nil? && !node.is_a?(Locomotive::Steam::Liquid::Tags::InheritedBlock)
+ elsif is_node_with_nodelist?(node)
contains_super?(node.nodelist)
end
end
@@ @@ -30,6 +28,14 @@ module Locomotive
node.raw.strip == 'block.super'
end
+ def is_node_with_nodelist?(node)
+ if node.respond_to?(:nodelist) && !node.is_a?(Locomotive::Steam::Liquid::Tags::InheritedBlock)
+ # some blocks does not have a body like the link_to tag
+ _nodelist = node.nodelist rescue nil
+ !_nodelist.nil?
+ end
+ end
+
end
::Liquid::Template.register_tag('block'.freeze, InheritedBlock)
locomotive/steam/liquid/tags/snippet.rb b/lib/locomotive/steam/liquid/tags/snippet.rb +8 -9
@@ @@ -6,14 +6,12 @@ module Locomotive
class Snippet < ::Liquid::Include
def parse(tokens)
- if listener = options[:events_listener]
- listener.emit(:include, page: options[:page], name: @template_name)
-
- # look for editable elements
- name = evaluate_snippet_name
- if snippet = options[:snippet_finder].find(name)
- options[:parser]._parse(snippet, options.merge(snippet: name))
- end
+ ActiveSupport::Notifications.instrument("steam.parse.include", page: options[:page], name: @template_name)
+
+ # look for editable elements
+ name = evaluate_snippet_name
+ if options[:snippet_finder] && snippet = options[:snippet_finder].find(name)
+ options[:parser]._parse(snippet, options.merge(snippet: name))
end
end
@@ @@ -35,7 +33,8 @@ module Locomotive
def evaluate_snippet_name(context = nil)
context.try(:evaluate, @template_name) ||
- @template_name.send(:state).first
+ (!@template_name.is_a?(String) && @template_name.send(:state).first) ||
+ @template_name
end
end
locomotive/steam/services/liquid_parser_service.rb b/lib/locomotive/steam/services/liquid_parser_service.rb +1 -2
@@ @@ -3,10 +3,9 @@ module Locomotive
class LiquidParserService < Struct.new(:parent_finder, :snippet_finder)
- def parse(page, events_listener = nil)
+ def parse(page)
_parse(page,
page: page,
- events_listener: events_listener,
parent_finder: parent_finder,
snippet_finder: snippet_finder,
parser: self)
spec/support/liquid.rb +1 -1
@@ @@ -11,7 +11,7 @@ end
module Liquid
class SimpleEventsListener
def initialize
- ActiveSupport::Notifications.subscribe(/^steam\.parse\.editable/) do |name, start, finish, id, payload|
+ ActiveSupport::Notifications.subscribe(/^steam\.parse\./) do |name, start, finish, id, payload|
emit(name, payload)
end
end
spec/unit/liquid/tags/editable/text_spec.rb +0 -3
@@ @@ -116,9 +116,6 @@ describe Locomotive::Steam::Liquid::Tags::Editable::Text do
describe 'deprecated elements' do
- let(:listener) { Liquid::SimpleEventsListener.new }
- let(:options) { { events_listener: listener } }
-
describe 'deprecated editable_long_text' do
let(:source) { "{% editable_long_text body %}Hello world{% endeditable_long_text %}" }
spec/unit/liquid/tags/extends_spec.rb +3 -3
@@ @@ -5,10 +5,10 @@ describe Locomotive::Steam::Liquid::Tags::Extends do
let(:source) { '{% extends parent %} ' }
let(:page) { instance_double('Page', title: 'About us') }
let(:site) { instance_double('Site', default_locale: :en) }
- let(:listener) { Liquid::SimpleEventsListener.new }
+ let!(:listener) { Liquid::SimpleEventsListener.new }
let(:finder) { Locomotive::Steam::ParentFinderService.new(instance_double('PageRepository', site: site, locale: :en)) }
let(:parser) { Locomotive::Steam::LiquidParserService.new }
- let(:options) { { events_listener: listener, parent_finder: finder, page: page, parser: parser } }
+ let(:options) { { parent_finder: finder, page: page, parser: parser } }
before do
expect(finder.repository).to receive(:parent_of).with(page).and_return(parent)
@@ @@ -29,7 +29,7 @@ describe Locomotive::Steam::Liquid::Tags::Extends do
let(:parent) { instance_double('Index', localized_attributes: { source: true, template: true }, source: { en: 'Hello world!' }, template: { en: nil }) }
- it { expect(listener.event_names.first).to eq :extends }
+ it { expect(listener.event_names.first).to eq 'steam.parse.extends' }
it { expect(template.render).to eq 'Hello world!' }
it { expect(options[:page]).to eq page }
spec/unit/liquid/tags/inherited_block_spec.rb +2 -2
@@ @@ -7,9 +7,9 @@ describe Locomotive::Steam::Liquid::Tags::InheritedBlock do
let(:source) { '{% extends parent %}{% block product %}Skis{% endblock %}' }
let(:page) { instance_double('Page') }
- let(:listener) { Liquid::SimpleEventsListener.new }
+ let!(:listener) { Liquid::SimpleEventsListener.new }
let(:finder) { instance_double('Finder', find: parent) }
- let(:options) { { page: page, events_listener: listener, parent_finder: finder, parser: Locomotive::Steam::LiquidParserService.new } }
+ let(:options) { { page: page, parent_finder: finder, parser: Locomotive::Steam::LiquidParserService.new } }
let!(:template) { parse_template(source, options) }
spec/unit/liquid/tags/snippet_spec.rb +3 -3
@@ @@ -12,12 +12,12 @@ describe Locomotive::Steam::Liquid::Tags::Snippet do
describe 'parsing' do
let(:page) { instance_double('Page') }
- let(:listener) { Liquid::SimpleEventsListener.new }
- let(:options) { { events_listener: listener, page: page, snippet_finder: finder, parser: services.liquid_parser } }
+ let!(:listener) { Liquid::SimpleEventsListener.new }
+ let(:options) { { page: page, snippet_finder: finder, parser: services.liquid_parser } }
let!(:template) { parse_template(source, options) }
- it { expect(listener.event_names.first).to eq :include }
+ it { expect(listener.event_names.first).to eq 'steam.parse.include' }
# describe 'with an editable_element inside', pending: true do