refactor the code related to sections + fix the specs
Didier Lafforgue
committed Apr 29, 2018
commit 268102759b0fc029fdacb86bf54d0c16486f4abb
Showing 8
changed files with
34 additions
and 40 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb
+2
-1
| @@ | @@ -13,7 +13,8 @@ require_relative 'steam/services' |
| module Locomotive | |
| module Steam | |
| - | FRONTMATTER_REGEXP = /^(?<yaml>(---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/mo.freeze |
| + | FRONTMATTER_REGEXP = /^(?<yaml>(---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/mo.freeze |
| + | JSON_FRONTMATTER_REGEXP = /^---\s*\n(?<json>(.*?\n?))?^(---\s*$\n?)(?<template>.*)/mo.freeze |
| WILDCARD = 'content_type_template'.freeze | |
locomotive/steam/adapters/filesystem/sanitizers/section.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb
+8
-5
| @@ | @@ -14,11 +14,14 @@ module Locomotive::Steam |
| private | |
| def parse_json(entity) | |
| - | json_formatter = /^---(?<json>(\s*\n.*?\n?))^---/mo |
| - | file_path = entity.template_path |
| - | file_content = File.read(file_path) |
| - | json = file_content.match(json_formatter) |
| - | entity.definition = JSON.parse(json[:json]) |
| + | content = File.read(entity.template_path) |
| + | |
| + | if match = content.match(JSON_FRONTMATTER_REGEXP) |
| + | json, template = match[:json], match[:template] |
| + | |
| + | entity.definition = JSON.parse(json) |
| + | entity.template = template |
| + | end |
| end | |
| end | |
| end | |
locomotive/steam/adapters/filesystem/yaml_loaders/section.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/section.rb
+7
-30
| @@ | @@ -4,7 +4,7 @@ module Locomotive |
| module Filesystem | |
| module YAMLLoaders | |
| class Section | |
| - | |
| + | |
| include Adapters::Filesystem::YAMLLoader | |
| def load(scope) | |
| @@ | @@ -15,43 +15,20 @@ module Locomotive |
| private | |
| def load_list | |
| - | {}.tap do |hash| |
| - | each_file do |filepath, slug, locale| |
| - | _locale = locale || default_locale |
| - | |
| - | if element = hash[slug] |
| - | update(element, filepath, _locale) |
| - | else |
| - | element = build(filepath, slug, _locale) |
| - | end |
| - | |
| - | hash[slug] = element |
| - | end |
| - | end.values |
| + | Dir.glob(File.join(path, "*.{#{template_extensions.join(',')}}")).map do |filepath| |
| + | slug = File.basename(filepath).split('.').first |
| + | build(filepath, slug.permalink) |
| + | end |
| end | |
| - | def build(filepath, slug, locale) |
| + | def build(filepath, slug) |
| { | |
| name: slug.humanize, | |
| slug: slug, | |
| - | template_path: { locale => filepath } |
| + | template_path: filepath |
| } | |
| end | |
| - | def update(element, filepath, locale) |
| - | element[:template_path][locale] = filepath |
| - | end |
| - | |
| - | def each_file(&block) |
| - | Dir.glob(File.join(path, "*.{#{template_extensions.join(',')}}")).each do |filepath| |
| - | |
| - | slug, locale = File.basename(filepath).split('.')[0..1] |
| - | locale = default_locale if template_extensions.include?(locale) |
| - | |
| - | yield(filepath, slug.permalink, locale.to_sym) |
| - | end |
| - | end |
| - | |
| def path | |
| @path ||= File.join(site_path, 'app', 'views', 'sections') | |
| end | |
locomotive/steam/adapters/filesystem/yaml_loaders/snippet.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/snippet.rb
+0
-1
| @@ | @@ -45,7 +45,6 @@ module Locomotive |
| def each_file(&block) | |
| Dir.glob(File.join(path, "*.{#{template_extensions.join(',')}}")).each do |filepath| | |
| - | |
| slug, locale = File.basename(filepath).split('.')[0..1] | |
| locale = default_locale if template_extensions.include?(locale) | |
locomotive/steam/entities/section.rb b/lib/locomotive/steam/entities/section.rb
+2
-2
| @@ | @@ -5,8 +5,8 @@ module Locomotive::Steam |
| def initialize(attributes = {}) | |
| super({ | |
| - | template: nil, |
| - | source: nil, |
| + | template: nil, |
| + | source: nil, |
| definition: nil | |
| }.merge(attributes)) | |
| end | |
locomotive_sections.bson b/spec/fixtures/mongodb/locomotive_sections.bson
+0
-0
locomotive_sections.metadata.json b/spec/fixtures/mongodb/locomotive_sections.metadata.json
+1
-0
| @@ | @@ -0,0 +1 @@ |
| + | {"options":{},"indexes":[{"v":2,"key":{"_id":1},"name":"_id_","ns":"locomotive_engine_dev.locomotive_sections"}]} |
| \ No newline at end of file | |
spec/unit/adapters/filesystem/sanitizers/section_spec.rb
+14
-1
| @@ | @@ -18,7 +18,20 @@ describe Locomotive::Steam::Adapters::Filesystem::Sanitizers::Section do |
| describe '#apply_to_entity' do | |
| subject { sanitizer.apply_to_entity(entity) } | |
| it 'sanitize entity' do | |
| - | expect(entity).to receive(:definition=).with(hash_including({"name" => 'header'})) |
| + | expect(entity).to receive(:definition=).with(hash_including({ 'name' => 'header' })) |
| + | expect(entity).to receive(:template=).with((<<-LIQUID |
| + | <h1> {{ section.settings.brand }} </h1> |
| + | <ul> |
| + | {% for block in section.blocks %} |
| + | <li> |
| + | <a href="{{ block.settings.url }}" target="{% if block.settings.new_tab %}_blank{% endif %}"> |
| + | {{ block.settings.label }} |
| + | </a> |
| + | </li> |
| + | {% endfor %} |
| + | </ul> |
| + | LIQUID |
| + | ).gsub /^$\n/, '') |
| expect(entity).to receive(:[]=).with(:site_id, 1) | |
| subject | |
| end | |