improve the section middleware

Didier Lafforgue committed May 27, 2018
commit f4f63cf5dc3531ee82479303c9c03b839a7251d7
Showing 7 changed files with 70 additions and 21 deletions
locomotive/steam/liquid/drops/section.rb b/lib/locomotive/steam/liquid/drops/section.rb +1 -1
@@ @@ -77,7 +77,7 @@ module Locomotive
# are we inside a block?
if matches && variable_name = matches[:name]
block = @context[variable_name]
- prefix += "-block.#{@context['forloop.index0']}"
+ prefix += "-block.#{block.id}"
end
# only string and text inputs can synced
locomotive/steam/liquid/tags/section.rb b/lib/locomotive/steam/liquid/tags/section.rb +10 -3
@@ @@ -19,10 +19,17 @@ module Locomotive
# 2. get the section
section = find_section(context)
- # 3. since it's considered as static, get the content from the current site.
- section_content = context['site']&.sections_content&.fetch(@section_type, nil)
+ # 3. if the tag is called by the Section middleware, use the content
+ # from the request.
+ section_content = context.registers[:_section_content]
- # 4. enhance the context by setting the "section" variable
+ # 4. since it's considered as static and if no content, get the
+ # content from the current site.
+ section_content ||= context['site']&.sections_content&.fetch(@section_type, nil)
+
+ puts section_content.inspect
+
+ # 5. enhance the context by setting the "section" variable
context['section'] = Locomotive::Steam::Liquid::Drops::Section.new(
section,
section_content
locomotive/steam/middlewares/concerns/liquid_context.rb b/lib/locomotive/steam/middlewares/concerns/liquid_context.rb +1 -1
@@ @@ -17,7 +17,7 @@ module Locomotive::Steam
services: services,
repositories: services.repositories,
logger: Locomotive::Common::Logger,
- live_editing: !!env['steam.live_editing'],
+ live_editing: live_editing?,
session: request.session
}
end
locomotive/steam/middlewares/section.rb b/lib/locomotive/steam/middlewares/section.rb +28 -8
@@ @@ -6,28 +6,48 @@ module Locomotive::Steam
include Concerns::LiquidContext
def _call
- if section_id = get_section_id(env['PATH_INFO'])
- html = render(section_id)
+ if section_type = get_section_type(env['PATH_INFO'])
+ html = render(section_type)
render_response(html, 200)
end
end
private
- def get_section_id(path_info)
- matchs = path_info.match(/^\/_sections\/(?<section_id>[a-z0-9]+$)/)
- matchs['section_id'] if matchs
+ def get_section_type(path_info)
+ matchs = path_info.match(/^\/_sections\/(?<section_type>[a-z0-9]+$)/)
+ matchs['section_type'] if matchs
end
def section_finder
services.section_finder
end
- def render(section_id)
- liquid_source = "{% section '#{section_id}' %}"
- document = Liquid::Template.parse liquid_source
+ def render(section_type)
+ document = Liquid::Template.parse(liquid_source(section_type))
document.render(liquid_context)
end
+
+ def liquid_source(section_type)
+ "{% section '#{section_type}' %}"
+ end
+
+ def liquid_registers
+ super.merge(_section_content: section_content)
+ end
+
+ def section_content
+ if (data = request.body.read).present?
+ JSON.parse(data)['section_content']
+ else
+ nil
+ end
+ end
+
+ def live_editing?
+ true
+ end
+
end
end
end
locomotive/steam/server.rb b/lib/locomotive/steam/server.rb +1 -1
@@ @@ -54,7 +54,6 @@ module Locomotive::Steam
[
Middlewares::DefaultEnv,
Middlewares::Site,
- Middlewares::Section,
Middlewares::Logging,
Middlewares::UrlRedirection,
Middlewares::Robots,
@@ @@ -67,6 +66,7 @@ module Locomotive::Steam
Middlewares::PrivateAccess,
Middlewares::Path,
Middlewares::Page,
+ Middlewares::Section,
Middlewares::Sitemap,
Middlewares::TemplatizedPage
]
spec/unit/liquid/tags/section_spec.rb +2 -2
@@ @@ -29,7 +29,7 @@ describe Locomotive::Steam::Liquid::Tags::Section do
],
default: {
settings: { brand: 'NoCoffee', image: 'foo.png' },
- blocks: [{ id: 1, type: 'menu_item', settings: { title: 'Home', image: 'foo.png' } }] }
+ blocks: [{ id: 42, type: 'menu_item', settings: { title: 'Home', image: 'foo.png' } }] }
}.deep_stringify_keys }
let(:section) { instance_double(
@@ @@ -70,7 +70,7 @@ describe Locomotive::Steam::Liquid::Tags::Section do
let(:liquid_source) { '{% for foo in section.blocks %}<a href="/">{{ foo.settings.title }}</a>{% endfor %}' }
- it { is_expected.to eq 'Locomotive <div id="locomotive-section-header" class="locomotive-section my-awesome-header"><a href="/" data-locomotive-editor-setting="section-header-block.0.title">Home</a></div>' }
+ it { is_expected.to eq 'Locomotive <div id="locomotive-section-header" class="locomotive-section my-awesome-header"><a href="/" data-locomotive-editor-setting="section-header-block.42.title">Home</a></div>' }
context 'with a non string type input' do
spec/unit/middlewares/section_spec.rb +27 -5
@@ @@ -11,9 +11,9 @@ describe Locomotive::Steam::Middlewares::Section do
let(:url) { 'http://example.com/_sections/header' }
let(:env) { env_for(url, 'steam.site' => site) }
- let(:drop) { liquid_instance_double('SiteDrop', sections_content: {}) }
+ let(:drop) { liquid_instance_double('SiteDrop', sections_content: { 'header' => { 'settings' => { 'name' => 'HTML' } } }) }
let(:site) { instance_double('Site', default_locale: 'en', locales: ['en'], to_liquid: drop) }
- let(:section) { instance_double('Section', type: 'fancy_section', definition: {}, liquid_source: 'Here some HTML') }
+ let(:section) { instance_double('Section', type: 'fancy_section', definition: {}, liquid_source: 'Here some {{ section.settings.name }}') }
let(:section_finder) { instance_double('SectionFinderService') }
let(:repositories) { instance_double('Repositories')}
@@ @@ -28,8 +28,8 @@ describe Locomotive::Steam::Middlewares::Section do
env['steam.page'] = nil
env['steam.services'] = services
env['steam.locale'] = :en
- env['steam.request'] = Rack::Request.new(env)
env['steam.liquid_assigns'] = {}
+ env['steam.request'] = Rack::Request.new(env)
allow(section_finder).to receive(:find).with('header').and_return(section)
end
@@ @@ -38,8 +38,30 @@ describe Locomotive::Steam::Middlewares::Section do
middleware.call(env)
end
- it 'works' do
- is_expected.to eq [200, {"Content-Type"=>"text/html"}, [%(<div id="locomotive-section-fancy_section" class="locomotive-section ">Here some HTML</div>)]]
+ it 'renders the HTML code related to the section' do
+ is_expected.to eq [
+ 200,
+ { "Content-Type" => "text/html" },
+ [%(<div id="locomotive-section-fancy_section" class="locomotive-section ">Here some HTML</div>)]
+ ]
+ end
+
+ context "the content of the section is in the request body" do
+
+ before do
+ allow(env['steam.request']).to receive(:body).and_return(StringIO.new(
+ %({ "section_content": { "settings": { "name": "modified HTML" } } })
+ ))
+ end
+
+ it 'renders the HTML code related to the section' do
+ is_expected.to eq [
+ 200,
+ { "Content-Type" => "text/html" },
+ [%(<div id="locomotive-section-fancy_section" class="locomotive-section ">Here some modified HTML</div>)]
+ ]
+ end
+
end
end