load sections content from mongo

Julien Girard committed Apr 30, 2018
commit a2f92229e27b265bba4398305adf69eae897f672
Showing 5 changed files with 30 additions and 8 deletions
locomotive/steam/adapters/filesystem/sanitizers/section.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb +8 -3
@@ @@ -15,16 +15,21 @@ module Locomotive::Steam
def parse_json(entity)
content = File.read(entity.template_path)
- match = content.match(JSON_FRONTMATTER_REGEXP)
- raise raise_parsing_error(entity, content) if match.nil?
+ match = content.match(JSON_FRONTMATTER_REGEXP)
+
+ raise_parsing_error(entity, content) if match.nil?
+
json, template = match[:json], match[:template]
+
entity.definition = JSON.parse(json)
entity.template = template
end
def raise_parsing_error(entity, content)
- raise Locomotive::Steam::ParsingRenderingError.new('Your section requires a valid JSON header', entity.template_path, content, 0, nil)
+ message = 'Your section requires a valid JSON header'
+ raise Locomotive::Steam::ParsingRenderingError.new(message, entity.template_path, content, 0, nil)
end
+
end
end
end
locomotive/steam/entities/site.rb b/lib/locomotive/steam/entities/site.rb +1 -0
@@ @@ -19,6 +19,7 @@ module Locomotive::Steam
password: nil,
metafields_schema: {},
metafields: nil,
+ sections: nil,
asset_host: nil
}.merge(attributes))
end
locomotive/steam/liquid/drops/site.rb b/lib/locomotive/steam/liquid/drops/site.rb +2 -1
@@ @@ -4,7 +4,8 @@ module Locomotive
module Drops
class Site < I18nBase
- delegate :name, :handle, :domains, :seo_title, :meta_keywords, :meta_description, :asset_host, to: :@_source
+ delegate :name, :handle, :domains, :seo_title, :meta_keywords,
+ :meta_description, :asset_host, :sections, to: :@_source
def index
@index ||= repository.root.to_liquid
locomotive/steam/liquid/tags/section.rb b/lib/locomotive/steam/liquid/tags/section.rb +18 -3
@@ @@ -5,12 +5,27 @@ module Locomotive
class Section < ::Liquid::Include
def render(context)
- @template_name = evaluate_snippet_name(context)
# @options doesn't include the page key if cache is on
@options[:page] = context.registers[:page]
- if find_section(context).definition[:default]
- context['section'] = find_section(context).definition[:default] # | mongoDB content if exists
+
+ # 1. get the name/slug of the section
+ @template_name = evaluate_snippet_name(context)
+
+ # 2. get the section
+ section = find_section(context)
+
+ # 3. because it's considered as a static section, go get the content from
+ # the current site. If it doesn't exist, use the default attribute of
+ # the section
+ section_content = context["site"].sections[@template_name]
+
+ if section_content.blank?
+ section_content = section.definition[:default] || {}
end
+
+ # 4. enhance the context by setting the "section" variable
+ context['section'] = section_content
+
begin
super
rescue Locomotive::Steam::ParsingRenderingError => e
locomotive/steam/repositories/site_repository.rb b/lib/locomotive/steam/repositories/site_repository.rb +1 -1
@@ @@ -7,7 +7,7 @@ module Locomotive
# Entity mapping
mapping :sites, entity: Site do
- localized_attributes :seo_title, :meta_description, :meta_keywords
+ localized_attributes :seo_title, :meta_description, :meta_keywords, :sections
end
def by_domain(domain)