first try to add sections
Julien Girard
committed Feb 02, 2018
commit cc551893d0af563e9ab5dc385f5ec55fbb119ac4
Showing 16
changed files with
366 additions
and 8 deletions
Gemfile
+4
-2
| @@ | @@ -3,11 +3,13 @@ source 'https://rubygems.org' |
| gemspec | |
| group :development do | |
| + | gem 'pry' |
| + | gem 'pry-byebug' |
| # gem 'locomotivecms_common', github: 'locomotivecms/common', ref: '257047b' | |
| - | # gem 'locomotivecms_common', path: '../common' |
| + | gem 'locomotivecms_common', path: '../common' |
| # gem 'locomotivecms_models', '~> 0.0.1', path: '../models' | |
| # gem 'locomotivecms_models', '0.0.1.pre.alpha' | |
| - | # gem 'locomotivecms-liquid', path: '/Users/didier/Documents/LocomotiveCMS/gems/liquid' |
| + | gem 'locomoticecms_solid', path: '../solid' |
| # gem 'duktape', path: '/Users/didier/Documents/NoCoffee/ProjectsX/duktape.rb' | |
| # gem 'duktape', github: 'judofyr/duktape.rb', ref: '20ef6a5' | |
| # gem 'thin' | |
Rakefile
+1
-1
| @@ | @@ -4,7 +4,7 @@ require 'bundler/gem_tasks' |
| require 'rake' | |
| require 'rspec' | |
| - | |
| + | require 'pry' |
| # === Gems install tasks === | |
| Bundler::GemHelper.install_tasks | |
bin/steam.rb
+1
-0
| @@ | @@ -2,6 +2,7 @@ |
| require 'rubygems' | |
| require 'bundler/setup' | |
| + | require 'pry' |
| Bundler.require | |
locomotive/steam.rb b/lib/locomotive/steam.rb
+1
-0
| @@ | @@ -1,5 +1,6 @@ |
| require 'locomotive/common' | |
| + | |
| require_relative 'steam/configuration' | |
| require_relative_all 'steam/decorators' | |
| require_relative 'steam/liquid' | |
locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb
+16
-5
| @@ | @@ -1,3 +1,4 @@ |
| + | require 'pry' |
| require_relative 'memory' | |
| require_relative 'filesystem/simple_cache_store' | |
| @@ | @@ -5,9 +6,13 @@ require_relative 'filesystem/simple_cache_store' |
| require_relative 'filesystem/yaml_loader' | |
| require_relative_all 'filesystem/yaml_loaders' | |
| + | # require_relative 'filesystem/json_loader' |
| + | # require_relative_all 'filesystem/json_loaders' |
| + | |
| require_relative 'filesystem/sanitizer' | |
| require_relative_all 'filesystem/sanitizers' | |
| + | |
| module Locomotive::Steam | |
| class FilesystemAdapter | |
| @@ | @@ -23,7 +28,8 @@ module Locomotive::Steam |
| register(:yaml_loaders) { build_yaml_loaders } | |
| register(:sanitizers) { build_sanitizers } | |
| - | |
| + | # register(:json_loaders) { build_json_loaders } |
| + | |
| def all(mapper, scope) | |
| memoized_dataset(mapper, scope) | |
| end | |
| @@ | @@ -116,15 +122,22 @@ module Locomotive::Steam |
| end | |
| def build_yaml_loaders | |
| - | %i(sites pages content_types content_entries snippets translations theme_assets).inject({}) do |memo, name| |
| + | %i(sites pages content_types content_entries snippets sections translations theme_assets).inject({}) do |memo, name| |
| memo[name] = build_klass('YAMLLoaders', name).new(site_path) | |
| memo | |
| end | |
| end | |
| + | # def build_json_loaders |
| + | # %i(sections).inject({}) do |memo, name| |
| + | # memo[name] = build_klass('JSONLoaders', name).new(site_path) |
| + | # memo |
| + | # end |
| + | # end |
| + | |
| def build_sanitizers | |
| hash = Hash.new { build_klass('Sanitizers', :simple).new } | |
| - | %i(sites pages content_types content_entries snippets).inject(hash) do |memo, name| |
| + | %i(sites pages content_types content_entries snippets sections).inject(hash) do |memo, name| |
| memo[name] = build_klass('Sanitizers', name).new | |
| memo | |
| end | |
| @@ | @@ -142,5 +155,3 @@ module Locomotive::Steam |
| end | |
| end | |
| - | |
| - | |
locomotive/steam/adapters/filesystem/json_loader.rb b/lib/locomotive/steam/adapters/filesystem/json_loader.rb
+57
-0
| @@ | @@ -0,0 +1,57 @@ |
| + | module Locomotive::Steam |
| + | module Adapters |
| + | module Filesystem |
| + | |
| + | module JSONLoader |
| + | |
| + | attr_reader :site_path |
| + | |
| + | def initialize(site_path) |
| + | @site_path = site_path |
| + | end |
| + | |
| + | def load(scope = nil) |
| + | @scope = scope |
| + | end |
| + | |
| + | def default_locale |
| + | @scope.default_locale |
| + | end |
| + | |
| + | def _load(path, frontmatter = false, &block) |
| + | if File.exists?(path) |
| + | json = File.open(path).read.force_encoding('utf-8') |
| + | template = nil |
| + | |
| + | if frontmatter && match = json.match(FRONTMATTER_REGEXP) |
| + | json, template = match[:json], match[:template] |
| + | end |
| + | |
| + | safe_json_load(json, template, path, &block) |
| + | else |
| + | Locomotive::Common::Logger.error "No #{path} file found" |
| + | {} |
| + | end |
| + | end |
| + | |
| + | def safe_json_load(json, template, path, &block) |
| + | return {} if json.blank? |
| + | |
| + | begin |
| + | HashConverter.to_sym(JSON.load(json)).tap do |attributes| |
| + | block.call(attributes, template) if block_given? |
| + | end |
| + | rescue Exception => e |
| + | raise "Malformed JSON in this file #{path}, error: #{e.message}" |
| + | end |
| + | end |
| + | |
| + | def template_extensions |
| + | @extensions ||= %w(liquid haml) |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
locomotive/steam/adapters/filesystem/json_loaders/section.rb b/lib/locomotive/steam/adapters/filesystem/json_loaders/section.rb
+66
-0
| @@ | @@ -0,0 +1,66 @@ |
| + | module Locomotive |
| + | module Steam |
| + | module Adapters |
| + | module Filesystem |
| + | module JSONLoaders |
| + | |
| + | class Section |
| + | |
| + | include Adapters::Filesystem::JSONLoader |
| + | |
| + | def load(scope) |
| + | super |
| + | load_list |
| + | end |
| + | |
| + | 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 |
| + | end |
| + | |
| + | def build(filepath, slug, locale) |
| + | { |
| + | name: slug.humanize, |
| + | slug: slug, |
| + | template_path: { locale => 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', 'section') |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
locomotive/steam/adapters/filesystem/sanitizers/section.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb
+41
-0
| @@ | @@ -0,0 +1,41 @@ |
| + | require 'pry' |
| + | module Locomotive::Steam |
| + | module Adapters |
| + | module Filesystem |
| + | module Sanitizers |
| + | |
| + | class Section |
| + | |
| + | include Adapters::Filesystem::Sanitizer |
| + | |
| + | def apply_to_entity(entity) |
| + | super |
| + | |
| + | json_formatter = /^---(?<json>(\s*\n.*?\n?))^---/mo |
| + | |
| + | file_path = entity[:template_path].translations[:en] |
| + | file_content = File.read(file_path) |
| + | json = file_content.match(json_formatter) |
| + | entity.definition = JSON.parse(json[:json]) |
| + | |
| + | use_default_template_if_missing_locale(entity) |
| + | end |
| + | |
| + | private |
| + | |
| + | def use_default_template_if_missing_locale(entity) |
| + | # if there a missing template in one of the locales, |
| + | # then use the one from the default locale |
| + | default = entity.template_path[default_locale] |
| + | locales.each do |locale| |
| + | next if locale == default_locale |
| + | entity.template_path[locale] ||= default |
| + | end |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| + | end |
locomotive/steam/adapters/filesystem/yaml_loaders/section.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/section.rb
+66
-0
| @@ | @@ -0,0 +1,66 @@ |
| + | module Locomotive |
| + | module Steam |
| + | module Adapters |
| + | module Filesystem |
| + | module YAMLLoaders |
| + | |
| + | class Section |
| + | |
| + | include Adapters::Filesystem::YAMLLoader |
| + | |
| + | def load(scope) |
| + | super |
| + | load_list |
| + | end |
| + | |
| + | 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 |
| + | end |
| + | |
| + | def build(filepath, slug, locale) |
| + | { |
| + | name: slug.humanize, |
| + | slug: slug, |
| + | template_path: { locale => 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 |
| + | |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
locomotive/steam/entities/section.rb b/lib/locomotive/steam/entities/section.rb
+21
-0
| @@ | @@ -0,0 +1,21 @@ |
| + | module Locomotive::Steam |
| + | |
| + | class Section |
| + | |
| + | include Locomotive::Steam::Models::Entity |
| + | |
| + | def initialize(attributes = {}) |
| + | super({ |
| + | template: nil, |
| + | source: nil, |
| + | definition: nil |
| + | }.merge(attributes)) |
| + | end |
| + | |
| + | def source |
| + | self[:template] |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
locomotive/steam/liquid/errors.rb b/lib/locomotive/steam/liquid/errors.rb
+2
-0
| @@ | @@ -5,6 +5,8 @@ module Locomotive |
| class SnippetNotFound < ::Liquid::Error; end | |
| + | class SectionNotFound < ::Liquid::Error; end |
| + | |
| class PageNotTranslated < ::Liquid::Error; end | |
| end | |
| end | |
locomotive/steam/liquid/tags/section.rb b/lib/locomotive/steam/liquid/tags/section.rb
+44
-0
| @@ | @@ -0,0 +1,44 @@ |
| + | module Locomotive |
| + | module Steam |
| + | module Liquid |
| + | module Tags |
| + | class Section < ::Liquid::Include |
| + | |
| + | def render(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 |
| + | end |
| + | begin |
| + | super |
| + | rescue Locomotive::Steam::ParsingRenderingError => e |
| + | e.file = @template_name + ' [Section]' |
| + | raise e |
| + | end |
| + | end |
| + | |
| + | private |
| + | |
| + | def read_template_from_file_system(context) |
| + | section = find_section(context) |
| + | |
| + | raise SectionNotFound.new("Section with slug '#{@template_name}' was not found") if section.nil? |
| + | |
| + | section.liquid_source |
| + | end |
| + | |
| + | private |
| + | |
| + | def find_section(context) |
| + | context.registers[:services].section_finder.find(@template_name) |
| + | end |
| + | |
| + | end |
| + | |
| + | |
| + | ::Liquid::Template.register_tag('section'.freeze, Section) |
| + | end |
| + | end |
| + | end |
| + | end |
locomotive/steam/repositories.rb b/lib/locomotive/steam/repositories.rb
+4
-0
| @@ | @@ -25,6 +25,10 @@ module Locomotive |
| SnippetRepository.new(adapter, current_site, locale) | |
| end | |
| + | register :section do |
| + | SectionRepository.new(adapter, current_site, locale) |
| + | end |
| + | |
| register :content_type do | |
| ContentTypeRepository.new(adapter, current_site, locale) | |
| end | |
locomotive/steam/repositories/section_repository.rb b/lib/locomotive/steam/repositories/section_repository.rb
+19
-0
| @@ | @@ -0,0 +1,19 @@ |
| + | module Locomotive |
| + | module Steam |
| + | |
| + | class SectionRepository |
| + | |
| + | include Models::Repository |
| + | |
| + | # Entity mapping |
| + | mapping :sections, entity: Section do |
| + | localized_attributes :template_path, :template, :source |
| + | end |
| + | |
| + | def by_slug(slug) |
| + | first { where(slug: slug) } |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
locomotive/steam/services.rb b/lib/locomotive/steam/services.rb
+4
-0
| @@ | @@ -61,6 +61,10 @@ module Locomotive |
| register :snippet_finder do | |
| Steam::SnippetFinderService.new(repositories.snippet) | |
| end | |
| + | |
| + | register :section_finder do |
| + | Steam::SectionFinderService.new(repositories.section) |
| + | end |
| register :action do | |
| Steam::ActionService.new(current_site, email, content_entry: content_entry, api: external_api, redirection: page_redirection) | |
locomotive/steam/services/section_finder_service.rb b/lib/locomotive/steam/services/section_finder_service.rb
+19
-0
| @@ | @@ -0,0 +1,19 @@ |
| + | module Locomotive |
| + | module Steam |
| + | |
| + | class SectionFinderService |
| + | |
| + | include Locomotive::Steam::Services::Concerns::Decorator |
| + | |
| + | attr_accessor_initialize :repository |
| + | |
| + | def find(slug) |
| + | decorate do |
| + | repository.by_slug(slug) |
| + | end |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | end |