write first specs for the filesystem repositories + translations fs loader + refactoring + fix bugs

did committed Feb 12, 2015
commit 6038d066b5eb8a1def269890db593757c4aa9085
Showing 34 changed files with 421 additions and 146 deletions
Gemfile.lock +2 -0
@@ @@ -21,6 +21,7 @@ PATH
rack_csrf (~> 2.5.0)
sprockets (~> 2.12.3)
sprockets-sass (~> 1.3.1)
+ stringex (~> 2.5.2)
GEM
remote: https://rubygems.org/
@@ @@ -185,6 +186,7 @@ GEM
sprockets-sass (1.3.1)
sprockets (~> 2.0)
tilt (~> 1.1)
+ stringex (2.5.2)
term-ansicolor (1.3.0)
tins (~> 1.0)
thin (1.6.3)
locomotive/steam/core_ext/string.rb b/lib/locomotive/steam/core_ext/string.rb +35 -6
@@ @@ -1,8 +1,37 @@
- class String
- def to_bool
- return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
- return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i)
-
- raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
+ unless String.public_instance_methods.include?(:to_bool)
+ class String
+
+ def to_bool
+ return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
+ return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i)
+
+ raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
+ end
+
+ end
+ end
+
+ unless String.public_instance_methods.include?(:permalink)
+ require 'stringex'
+
+ class String
+
+ def permalink(underscore = false)
+ # if the slug includes one "_" at least, we consider that the "_" is used instead of "-".
+ _permalink = if !self.index('_').nil?
+ self.to_url(replace_whitespace_with: '_')
+ else
+ self.to_url
+ end
+
+ underscore ? _permalink.underscore : _permalink
+ end
+
+ def permalink!(underscore = false)
+ replace(self.permalink(underscore))
+ end
+
+ alias :parameterize! :permalink!
+
end
end
locomotive/steam/liquid/tags/editable/base.rb b/lib/locomotive/steam/liquid/tags/editable/base.rb +3 -2
@@ @@ -37,8 +37,9 @@ module Locomotive
if element = repository.editable_element_for(page, block, @slug)
render_element(context, element)
else
- Locomotive::Common::Logger.error "[#{page.fullpath}] missing #{@tag_name} \"#{@slug}\" (#{context['block'].try(:name) || 'default'})"
- ''
+ # Locomotive::Common::Logger.error "[#{page.fullpath}] missing #{@tag_name} \"#{@slug}\" (#{context['block'].try(:name) || 'default'})"
+ # render_default_content
+ super
end
end
locomotive/steam/liquid/tags/snippet.rb b/lib/locomotive/steam/liquid/tags/snippet.rb +8 -3
@@ @@ -10,7 +10,7 @@ module Locomotive
listener.emit(:include, page: options[:page], name: @template_name)
# look for editable elements
- if snippet = options[:snippet_finder].find(@template_name)
+ if snippet = options[:snippet_finder].find(snippet_name)
options[:parser]._parse(snippet, options.merge(snippet: @template_name))
end
end
@@ @@ -20,13 +20,18 @@ module Locomotive
def read_template_from_file_system(context)
service = context.registers[:services]
- snippet = service.snippet_finder.find(@template_name)
+ snippet = service.snippet_finder.find(snippet_name)
- raise SnippetNotFound.new("Snippet with slug '#{@template_name}' was not found") if snippet.nil?
+ raise SnippetNotFound.new("Snippet with slug '#{snippet_name}' was not found") if snippet.nil?
snippet.liquid_source
end
+ def snippet_name(context = nil)
+ context.try(:evaluate, @template_name) ||
+ @template_name.send(:state).first
+ end
+
end
::Liquid::Template.register_tag('include'.freeze, Snippet)
locomotive/steam/repositories/filesystem.rb b/lib/locomotive/steam/repositories/filesystem.rb +1 -1
@@ @@ -1,5 +1,5 @@
Dir[File.join(File.dirname(__FILE__), 'filesystem', 'yaml_loaders', 'concerns', '*.rb')].each { |lib| require lib }
- %w(memory_adapter yaml_loaders sanitizers models .).each do |name|
+ %w(concerns memory_adapter yaml_loaders sanitizers models .).each do |name|
Dir[File.join(File.dirname(__FILE__), 'filesystem', name, '*.rb')].each { |lib| require lib }
end
locomotive/steam/repositories/filesystem/concerns/queryable.rb b/lib/locomotive/steam/repositories/filesystem/concerns/queryable.rb +47 -0
@@ @@ -0,0 +1,47 @@
+ module Locomotive
+ module Steam
+ module Repositories
+ module Filesystem
+ module Concerns
+
+ module Queryable
+
+ extend ActiveSupport::Concern
+
+ def query(&block)
+ MemoryAdapter::Query.new(collection, current_locale, &block)
+ end
+
+ private
+
+ def collection
+ return @collection if @collection
+
+ @collection = loader.list_of_attributes.map do |attributes|
+ collection_options[:model].new(attributes)
+ end
+
+ if sanitizer = collection_options[:sanitizer]
+ sanitizer.new(site.default_locale, site.locales).apply_to(@collection)
+ else
+ @collection
+ end
+ end
+
+ module ClassMethods
+
+ def set_collection(options = {})
+ class_eval do
+ define_method(:collection_options) { options }
+ end
+ end
+
+ end
+
+ end
+
+ end
+ end
+ end
+ end
+ end
locomotive/steam/repositories/filesystem/models/base.rb b/lib/locomotive/steam/repositories/filesystem/models/base.rb +8 -0
@@ @@ -20,6 +20,14 @@ module Locomotive
end
end
+ def []=(name, value)
+ attributes[name.to_sym] = value
+ end
+
+ def [](name)
+ attributes[name.to_sym]
+ end
+
def self.set_localized_attributes(list)
singleton = class << self; self; end
singleton.class_eval do
locomotive/steam/repositories/filesystem/models/editable_element.rb b/lib/locomotive/steam/repositories/filesystem/models/editable_element.rb +14 -0
@@ @@ -0,0 +1,14 @@
+ module Locomotive
+ module Steam
+ module Repositories
+ module Filesystem
+ module Models
+
+ class EditableElement < Struct.new(:block, :slug, :content)
+ end
+
+ end
+ end
+ end
+ end
+ end
locomotive/steam/repositories/filesystem/models/page.rb b/lib/locomotive/steam/repositories/filesystem/models/page.rb +13 -7
@@ @@ -6,19 +6,25 @@ module Locomotive
class Page < Base
- set_localized_attributes [:title, :slug, :permalink, :template, :template_path, :fullpath, :seo, :meta_description, :meta_keywords]
+ set_localized_attributes [:title, :slug, :permalink, :editable_elements, :template, :template_path, :fullpath, :seo, :meta_description, :meta_keywords]
+
+ attr_accessor :depth, :_fullpath
def initialize(attributes)
super({
- listed: true,
- published: false,
- fullpath: {},
- content_type: nil,
- position: 100,
- template: {}
+ listed: true,
+ published: false,
+ fullpath: {},
+ content_type: nil,
+ position: 100,
+ template: {},
+ editable_elements: {}
}.merge(attributes))
end
+ def listed?; !!listed; end
+ def published?; !!published; end
+
def templatized?
!!content_type
end
locomotive/steam/repositories/filesystem/models/translation.rb b/lib/locomotive/steam/repositories/filesystem/models/translation.rb +14 -0
@@ @@ -0,0 +1,14 @@
+ module Locomotive
+ module Steam
+ module Repositories
+ module Filesystem
+ module Models
+
+ class Translation < Struct.new(:key, :values)
+ end
+
+ end
+ end
+ end
+ end
+ end
locomotive/steam/repositories/filesystem/page.rb b/lib/locomotive/steam/repositories/filesystem/page.rb +22 -18
@@ @@ -5,26 +5,22 @@ module Locomotive
class Page < Struct.new(:loader, :site, :current_locale)
+ # Engine: site.pages.ordered_pages(conditions)
def all(conditions = {})
raise 'TODO all'
- # site.pages.ordered_pages(conditions)
end
+ # Engine: site.pages.where(handle: handle).first
def by_handle(handle)
raise 'TODO by_handle'
- # site.pages.where(handle: handle).first
end
def by_fullpath(path)
- MemoryAdapter::Query.new(collection, current_locale) do
- where(fullpath: path)
- end.first
+ query { where(fullpath: path) }.first
end
def matching_fullpath(list)
- MemoryAdapter::Query.new(collection, current_locale) do
- where('fullpath.in' => list)
- end.all
+ query { where('fullpath.in' => list) }.all
end
def template_for(entry, handle = nil)
@@ @@ -37,37 +33,45 @@ module Locomotive
end
def root
- raise 'TODO root'
- # site.pages.root.first
+ query { where(depth: 1, 'slug.ne' => nil) }.all
end
+ # Engine: page.parent
def parent_of(page)
raise 'TODO parent_of'
- # page.parent
end
+ # Engine: page.ancestors_and_self
def ancestors_of(page)
raise 'TODO ancestors_of'
- # page.ancestors_and_self
end
+ # Engine: page.children
def children_of(page)
- raise 'TODO children_of'
- # page.children
+ query { where(depth: 1, 'slug.ne' => nil) }.all
end
+ # Engine: page.editable_elements
def editable_elements_of(page)
- raise 'TODO editable_elements_of'
- # page.editable_elements
+ page.editable_elements.values
end
+ # Engine: page.editable_elements.where(block: block, slug: slug).first
def editable_element_for(page, block, slug)
- raise 'TODO editable_element_for'
- # page.editable_elements.where(block: block, slug: slug).first
+ if elements = page.editable_elements
+ name = [block, slug].compact.join('/')
+ elements[name]
+ else
+ nil
+ end
end
private
+ def query(&block)
+ MemoryAdapter::Query.new(collection, current_locale, &block)
+ end
+
def collection
return @collection if @collection
locomotive/steam/repositories/filesystem/sanitizers/page.rb b/lib/locomotive/steam/repositories/filesystem/sanitizers/page.rb +31 -7
@@ @@ -17,42 +17,66 @@ module Locomotive
def apply
sorted_collection.each do |page|
locales.each do |locale|
- modify_if_templatized(page, locale)
set_fullpath_for(page, locale)
+ modify_if_templatized(page, locale)
+ build_editable_elements(page, locale)
end
end
end
+ def build_editable_elements(page, locale)
+ elements = page.editable_elements[locale] || {}
+ elements.stringify_keys!
+
+ elements.each do |name, content|
+ segments = name.split('/')
+ block, slug = segments[0..-2].join('/'), segments.last
+ block = nil if block.blank?
+
+ elements[name] = Filesystem::Models::EditableElement.new(block, slug, content)
+ end
+ end
+
def modify_if_templatized(page, locale)
content_type = fetch_content_type(parent_fullpath(page))
if page.templatized? && content_type.nil?
# change the slug of a templatized page
- page.attributes[:slug][locale] = 'content_type_template'
+ page[:slug][locale] = 'content_type_template'
# make sure its children will have its content type
set_content_type(page._fullpath, page.content_type)
else
- page.attributes[:content_type] = content_type
+ page[:content_type] = content_type
end
end
def set_fullpath_for(page, locale)
- slug = fullpath = page.attributes[:slug][locale].try(:dasherize)
+ page._fullpath ||= page.attributes.delete(:_fullpath)
+
+ slug = fullpath = page.slug[locale].try(:dasherize)
return if slug.blank?
- if depth(page) > 1
+ if page.depth > 1
base = parent_fullpath(page)
fullpath = (fetch_localized_fullpath(base, locale) || base) + '/' + slug
end
set_localized_fullpath(page._fullpath, fullpath, locale)
- page.attributes[:fullpath][locale] = fullpath
+ page[:fullpath][locale] = fullpath
end
def depth(page)
- page._fullpath.split('/').size
+ return page.depth if page.depth
+
+ page.depth = page[:_fullpath].split('/').size
+
+ if page.depth == 1 && (page.slug == 'index' || page.slug == '404')
+ page.depth = 0
+ end
+
+ page.depth
end
def sorted_collection
locomotive/steam/repositories/filesystem/sanitizers/snippet.rb b/lib/locomotive/steam/repositories/filesystem/sanitizers/snippet.rb +2 -2
@@ @@ -4,9 +4,9 @@ module Locomotive
module Filesystem
module Sanitizers
- class Snippet < Struct.new(:collection, :default_locale, :locales)
+ class Snippet < Struct.new(:default_locale, :locales)
- def apply
+ def apply_to(collection)
collection.each do |snippet|
# if there a missing template in one of the locales,
# then use the one from the default locale
locomotive/steam/repositories/filesystem/snippet.rb b/lib/locomotive/steam/repositories/filesystem/snippet.rb +4 -14
@@ @@ -5,22 +5,12 @@ module Locomotive
class Snippet < Struct.new(:loader, :site, :current_locale)
- def by_slug(slug)
- MemoryAdapter::Query.new(collection, current_locale) do
- where(slug: slug)
- end.first
- end
-
- private
+ include Locomotive::Steam::Repositories::Filesystem::Concerns::Queryable
- def collection
- return @collection if @collection
+ set_collection model: Filesystem::Models::Snippet, sanitizer: Filesystem::Sanitizers::Snippet
- @collection = loader.list_of_attributes.map do |attributes|
- Filesystem::Models::Snippet.new(attributes)
- end
-
- Filesystem::Sanitizers::Snippet.new(@collection, site.default_locale, site.locales).apply
+ def by_slug(slug)
+ query { where(slug: slug) }.first
end
end
locomotive/steam/repositories/filesystem/translation.rb b/lib/locomotive/steam/repositories/filesystem/translation.rb +20 -2
@@ @@ -5,9 +5,27 @@ module Locomotive
class Translation < Struct.new(:site)
+ # include Concerns::Queryable
+
+ # Engine: site.translations.where(key: input).first
def find(key)
- # site.translations.where(key: input).first
- raise 'TODO find'
+ query { where(key: key) }
+ end
+
+ private
+
+ def query(&block)
+ MemoryAdapter::Query.new(collection, current_locale, &block)
+ end
+
+ def collection
+ return @collection if @collection
+
+ @collection = loader.list_of_attributes.map do |attributes|
+ Filesystem::Models::Translation.new(attributes)
+ end
+
+ Filesystem::Sanitizers::Page.new(@collection, site.locales).apply
end
end
locomotive/steam/repositories/filesystem/yaml_loaders/concerns/common.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/concerns/common.rb +11 -7
@@ @@ -8,15 +8,19 @@ module Locomotive
module Common
def load(path, frontmatter = false)
- yaml = File.open(path).read.force_encoding('utf-8')
+ if File.exists?(path)
+ yaml = File.open(path).read.force_encoding('utf-8')
- if frontmatter
- yaml =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m
- yaml = $1
- end
+ if frontmatter
+ yaml =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m
+ yaml = $1
+ end
- raw_data = YAML.load(yaml)
- HashConverter.to_sym(raw_data)
+ HashConverter.to_sym(YAML.load(yaml))
+ else
+ Locomotive::Common::Logger.error "No #{path} file found"
+ {}
+ end
end
def template_extensions
locomotive/steam/repositories/filesystem/yaml_loaders/page.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/page.rb +11 -4
@@ @@ -34,8 +34,8 @@ module Locomotive
end
def build(filepath, fullpath, locale)
- slug = fullpath.split('/').last
- attributes = load(filepath, true)
+ slug = fullpath.split('/').last
+ attributes = get_attributes(filepath, fullpath)
{
title: { locale => attributes.delete(:title) || (default_locale == locale ? slug.humanize : nil) },
@@ @@ -47,8 +47,8 @@ module Locomotive
end
def update(leaf, filepath, fullpath, locale)
- slug = fullpath.split('/').last
- attributes = load(filepath, true)
+ slug = fullpath.split('/').last
+ attributes = get_attributes(filepath, fullpath)
leaf[:title][locale] = attributes.delete(:title) || slug.humanize
leaf[:slug][locale] = attributes.delete(:slug) || slug
@@ @@ -58,6 +58,13 @@ module Locomotive
leaf.merge!(attributes)
end
+ def get_attributes(filepath, fullpath)
+ load(filepath, true).tap do |attributes|
+ # make sure index/404 are the slugs of the index/404 pages
+ attributes.delete(:slug) if %w(index 404).include?(fullpath)
+ end
+ end
+
def each_file(&block)
Dir.glob(File.join(path, '**', '*')).each do |filepath|
next unless is_liquid_file?(filepath)
locomotive/steam/repositories/filesystem/yaml_loaders/site.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/site.rb +1 -1
@@ @@ -9,7 +9,7 @@ module Locomotive
include YAMLLoaders::Concerns::Common
def attributes
- cache.fetch('config/site.yml') do
+ cache.fetch('config/site') do
load(File.join(root_path, 'config', 'site.yml'))
end
end
locomotive/steam/repositories/filesystem/yaml_loaders/snippet.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/snippet.rb +5 -5
@@ @@ -9,7 +9,7 @@ module Locomotive
include YAMLLoaders::Concerns::Common
def list_of_attributes
- cache.fetch('app/views/pages') { load_list }
+ cache.fetch('app/views/snippets') { load_list }
end
private
@@ @@ -34,12 +34,12 @@ module Locomotive
{
name: slug.humanize,
slug: slug,
- template_path: { _locale => filepath }
+ template_path: { locale => filepath }
}
end
- def update(element, filepath, slug, locale)
- element[:template_path][_locale] = filepath
+ def update(element, filepath, locale)
+ element[:template_path][locale] = filepath
end
def each_file(&block)
@@ @@ -48,7 +48,7 @@ module Locomotive
slug, locale = File.basename(filepath).split('.')[0..1]
locale = default_locale if template_extensions.include?(locale)
- yield(filepath, slug, locale.to_sym)
+ yield(filepath, slug.permalink, locale.to_sym)
end
end
locomotive/steam/repositories/filesystem/yaml_loaders/translation.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/translation.rb +35 -0
@@ @@ -0,0 +1,35 @@
+ module Locomotive
+ module Steam
+ module Repositories
+ module Filesystem
+ module YAMLLoaders
+
+ class Translation < Struct.new(:root_path, :cache)
+
+ include YAMLLoaders::Concerns::Common
+
+ def list_of_attributes
+ cache.fetch('config/translations') { load_array }
+ end
+
+ private
+
+ def load_array
+ [].tap do |array|
+ load(path).each do |key, values|
+ array << { key: key.to_s, values: values }
+ end
+ end
+ end
+
+ def path
+ File.join(root_path, 'config', 'translations.yml')
+ end
+
+ end
+
+ end
+ end
+ end
+ end
+ end
locomotive/steam/services.rb b/lib/locomotive/steam/services.rb +3 -1
@@ @@ -1,4 +1,6 @@
- Dir[File.join(File.dirname(__FILE__), 'services', '**/*.rb')].each { |lib| require lib }
+ %w(concerns .).each do |name|
+ Dir[File.join(File.dirname(__FILE__), 'services', name, '*.rb')].each { |lib| require lib }
+ end
require 'morphine'
locomotive/steam/services/page_finder.rb b/lib/locomotive/steam/services/page_finder.rb +1 -1
@@ @@ -4,7 +4,7 @@ module Locomotive
class PageFinder < Struct.new(:repository)
- include Concerns::Decorator
+ include Locomotive::Steam::Services::Concerns::Decorator
WILDCARD = 'content-type-template'
locomotive/steam/services/snippet_finder.rb b/lib/locomotive/steam/services/snippet_finder.rb +1 -1
@@ @@ -4,7 +4,7 @@ module Locomotive
class SnippetFinder < Struct.new(:repository)
- include Concerns::Decorator
+ include Locomotive::Steam::Services::Concerns::Decorator
def find(slug)
decorate do
locomotivecms_steam.gemspec +1 -0
@@ @@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '~> 10.4.2'
spec.add_dependency 'activesupport', '~> 4.2.0'
+ spec.add_dependency 'stringex', '~> 2.5.2'
spec.add_dependency 'morphine', '~> 0.1.1'
spec.add_dependency 'httparty', '~> 0.13.3'
spec/support.rb +1 -0
@@ @@ -1,4 +1,5 @@
require_relative 'support/helpers'
+ require_relative 'support/cache_store'
require_relative 'support/liquid'
require_relative 'support/matchers/hash'
require_relative 'support/examples/matching_locale'
spec/unit/decorators/page_decorator_spec.rb +0 -58
@@ @@ -1,58 +0,0 @@
- # require 'spec_helper'
-
- # describe 'Locomotive::Steam::Decorators::PageDecorator' do
-
- # before { skip }
-
- # let(:locale) { :en }
-
- # it 'builds an empty decorator' do
- # build_page.should_not be_nil
- # end
-
- # describe '#safe_fullpath' do
- # let(:index_page) { build_page(fullpath: { en: 'index' }) }
- # let(:not_found_page) { build_page(fullpath: { en: '404' }) }
- # let(:about_page) { build_page(fullpath: { en: 'about_me'}, parent: index_page) }
- # let(:products_page) { build_page(fullpath: { en: 'products'}, parent: index_page, templatized: true) }
-
- # context 'not templatized' do
- # context 'index or 404' do
- # it { decorated(index_page).safe_fullpath.should eq 'index' }
- # it { decorated(not_found_page).safe_fullpath.should eq '404' }
- # end
-
- # context 'other' do
- # it { decorated(about_page).safe_fullpath.should eq 'about-me' }
- # end
- # end
-
- # context 'templatized' do
- # subject { decorated build_page(fullpath: { en: 'products' }, parent: index_page, templatized: true) }
- # # its(:safe_fullpath) { should eq '*' }
- # end
-
- # context 'templatized with not templatized parent' do
- # subject { decorated build_page(fullpath: { en: 'about_me/contact' }, parent: about_page, templatized: true) }
- # # its(:safe_fullpath) { should eq 'about-me/*' }
- # end
-
- # context 'templatized parent' do
- # subject { decorated build_page(fullpath: { en: 'products/detail' }, parent: products_page) }
- # # its(:safe_fullpath) { should eq '*/detail' }
- # end
- # end
-
- # def decorated(page)
- # Locomotive::Steam::Decorators::PageDecorator.new(
- # Locomotive::Decorators::I18nDecorator.new(
- # page, locale
- # )
- # )
- # end
-
- # def build_page(attributes = {})
- # Locomotive::Steam::Entities::Page.new(attributes)
- # end
-
- # end
spec/unit/liquid/tags/editable/control_spec.rb +2 -2
@@ @@ -81,10 +81,10 @@ describe Locomotive::Steam::Liquid::Tags::Editable::Control do
it { is_expected.to eq 'false' }
- context 'no element found' do
+ context 'no element found, render the default content' do
let(:element) { nil }
- it { is_expected.to eq '' }
+ it { is_expected.to eq 'false' }
end
spec/unit/liquid/tags/editable/file_spec.rb +2 -2
@@ @@ -80,10 +80,10 @@ describe Locomotive::Steam::Liquid::Tags::Editable::File do
it { is_expected.to eq 'http://www.placehold.it/500x500' }
- context 'no element found' do
+ context 'no element found, render the default content' do
let(:element) { nil }
- it { is_expected.to eq '' }
+ it { is_expected.to eq 'http://www.placehold.it/500x500' }
end
spec/unit/liquid/tags/editable/text_spec.rb +2 -2
@@ @@ -83,10 +83,10 @@ describe Locomotive::Steam::Liquid::Tags::Editable::Text do
it { is_expected.to eq 'Hello world' }
- context 'no element found' do
+ context 'no element found, render the default content' do
let(:element) { nil }
- it { is_expected.to eq '' }
+ it { is_expected.to eq 'Hello world' }
end
spec/unit/repositories/filesystem/snippet_spec.rb +39 -0
@@ @@ -0,0 +1,39 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Repositories::Filesystem::Snippet do
+
+ let(:loader) { instance_double('Loader', list_of_attributes: [{ name: 'Simple', slug: 'simple', template_path: { en: 'simple.yml' } }]) }
+ let(:site) { instance_double('Site', default_locale: :en, locales: [:en, :fr]) }
+ let(:locale) { :en }
+
+ let(:repository) { Locomotive::Steam::Repositories::Filesystem::Snippet.new(loader, site, locale) }
+
+ describe '#collection' do
+
+ subject { repository.send(:collection).first }
+
+ it { expect(subject.class).to eq Locomotive::Steam::Repositories::Filesystem::Models::Snippet }
+
+ it 'applies the sanitizer' do
+ expect(subject[:template_path]).to eq({ en: 'simple.yml', fr: 'simple.yml' })
+ end
+
+ end
+
+ describe '#by_slug' do
+
+ let(:name) { nil }
+ subject { repository.by_slug(name) }
+
+ it { is_expected.to eq nil }
+
+ context 'existing snippet' do
+
+ let(:name) { 'simple' }
+ it { expect(subject.name).to eq 'Simple' }
+
+ end
+
+ end
+
+ end
spec/unit/repositories/filesystem/yaml_loaders/page_spec.rb +21 -0
@@ @@ -0,0 +1,21 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Page do
+
+ let(:root_path) { default_fixture_site_path }
+ let(:default_locale) { :en }
+ let(:cache) { NoCacheStore.new }
+ let(:loader) { Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Page.new(root_path, default_locale, cache) }
+
+ describe '#list_of_attributes' do
+
+ subject { loader.list_of_attributes }
+
+ it 'tests various stuff' do
+ expect(subject.size).to eq 20
+ expect(subject.first[:title]).to eq({ en: 'Page not found' })
+ end
+
+ end
+
+ end
spec/unit/repositories/filesystem/yaml_loaders/site_spec.rb +17 -0
@@ @@ -0,0 +1,17 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Site do
+
+ let(:root_path) { default_fixture_site_path }
+ let(:cache) { NoCacheStore.new }
+ let(:loader) { Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Site.new(root_path, cache) }
+
+ describe '#attributes' do
+
+ subject { loader.attributes }
+
+ it { expect(subject[:name]).to eq 'Sample website' }
+
+ end
+
+ end
spec/unit/repositories/filesystem/yaml_loaders/snippet_spec.rb +23 -0
@@ @@ -0,0 +1,23 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Snippet do
+
+ let(:root_path) { default_fixture_site_path }
+ let(:default_locale) { :en }
+ let(:cache) { NoCacheStore.new }
+ let(:loader) { Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Snippet.new(root_path, default_locale, cache) }
+
+ describe '#list_of_attributes' do
+
+ subject { loader.list_of_attributes }
+
+ it 'tests various stuff' do
+ expect(subject.size).to eq 4
+ expect(subject.first[:name]).to eq('Song')
+ expect(subject.first[:slug]).to eq('song')
+ expect(subject[1][:slug]).to eq('a_complicated-one')
+ end
+
+ end
+
+ end
spec/unit/repositories/filesystem/yaml_loaders/translation_spec.rb +21 -0
@@ @@ -0,0 +1,21 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Translation do
+
+ let(:root_path) { default_fixture_site_path }
+ let(:cache) { NoCacheStore.new }
+ let(:loader) { Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Translation.new(root_path, cache) }
+
+ describe '#list_of_attributes' do
+
+ subject { loader.list_of_attributes }
+
+ it 'tests various stuff' do
+ expect(subject.size).to eq 1
+ expect(subject.first[:key]).to eq('powered_by')
+ expect(subject.first[:values]).to eq({ en: 'Powered by', fr: 'Propulsé par' })
+ end
+
+ end
+
+ end