introducing the queryable module for filesystem repositories

did committed Feb 12, 2015
commit e264a890f2e3d9a0e9ef28db6eed02128abb33a3
Showing 6 changed files with 62 additions and 29 deletions
locomotive/steam/repositories/filesystem/page.rb b/lib/locomotive/steam/repositories/filesystem/page.rb +4 -16
@@ @@ -5,6 +5,10 @@ module Locomotive
class Page < Struct.new(:loader, :site, :current_locale)
+ include Locomotive::Steam::Repositories::Filesystem::Concerns::Queryable
+
+ set_collection model: Filesystem::Models::Page, sanitizer: Filesystem::Sanitizers::Page
+
# Engine: site.pages.ordered_pages(conditions)
def all(conditions = {})
raise 'TODO all'
@@ @@ -66,22 +70,6 @@ module Locomotive
end
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::Page.new(attributes)
- end
-
- Filesystem::Sanitizers::Page.new(@collection, site.locales).apply
- end
-
end
end
locomotive/steam/repositories/filesystem/sanitizers/page.rb b/lib/locomotive/steam/repositories/filesystem/sanitizers/page.rb +8 -8
@@ @@ -4,18 +4,17 @@ module Locomotive
module Filesystem
module Sanitizers
- class Page < Struct.new(:collection, :locales)
+ class Page < Struct.new(:default_locale, :locales)
- def initialize(collection, locales)
+ def initialize(default_locale, locales)
super
-
@content_types = {}
@localized = {}
locales.each { |locale| @localized[locale] = {} }
end
- def apply
- sorted_collection.each do |page|
+ def apply_to(collection)
+ sorted_collection(collection).each do |page|
locales.each do |locale|
set_fullpath_for(page, locale)
modify_if_templatized(page, locale)
@@ @@ -71,16 +70,17 @@ module Locomotive
return page.depth if page.depth
page.depth = page[:_fullpath].split('/').size
+ slug = (page.slug || {}).try(:values).compact.first
- if page.depth == 1 && (page.slug == 'index' || page.slug == '404')
+ if page.depth == 1 && %w(index 404).include?(slug)
page.depth = 0
end
page.depth
end
- def sorted_collection
- collection.sort { |a, b| depth(a) <=> depth(b) }
+ def sorted_collection(collection)
+ collection.sort_by { |page| depth(page) }
end
def parent_fullpath(page)
locomotive/steam/services/parent_finder.rb b/lib/locomotive/steam/services/parent_finder.rb +2 -0
@@ @@ -1,3 +1,5 @@
+ require_relative 'page_finder'
+
module Locomotive
module Steam
module Services
spec/integration/server/basic_spec.rb +5 -5
@@ @@ -18,11 +18,11 @@ describe Locomotive::Steam::Server do
# expect(last_response.body).to match(/Upcoming events/)
# end
- # it 'shows the 404 page' do
- # get '/void'
- # expect(last_response.status).to eq(404)
- # expect(last_response.body).to match /page not found/
- # end
+ it 'shows the 404 page' do
+ get '/void'
+ expect(last_response.status).to eq(404)
+ expect(last_response.body).to match /page not found/
+ end
# it 'shows the 404 page with 200 status code when its called explicitly', pending: true do
# get '/404'
spec/support/cache_store.rb +3 -0
@@ @@ -0,0 +1,3 @@
+ class NoCacheStore
+ def fetch(name, options = nil, &block); yield; end
+ end
spec/unit/repositories/filesystem/page_spec.rb +40 -0
@@ @@ -0,0 +1,40 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Repositories::Filesystem::Page do
+
+ let(:loader) { instance_double('Loader', list_of_attributes: [{ title: { en: 'Home' }, slug: { en: 'index' }, _fullpath: 'index', template_path: { en: 'index.liquid' } }]) }
+ let(:site) { instance_double('Site', default_locale: :en, locales: [:en, :fr]) }
+ let(:locale) { :en }
+
+ let(:repository) { Locomotive::Steam::Repositories::Filesystem::Page.new(loader, site, locale) }
+
+ describe '#collection' do
+
+ subject { repository.send(:collection).first }
+
+ it { expect(subject.class).to eq Locomotive::Steam::Repositories::Filesystem::Models::Page }
+
+ it 'applies the sanitizer' do
+ expect(subject[:fullpath]).to eq({ en: 'index' })
+ expect(subject.depth).to eq 0
+ end
+
+ end
+
+ describe '#by_fullpath' do
+
+ let(:path) { nil }
+ subject { repository.by_fullpath(path) }
+
+ it { is_expected.to eq nil }
+
+ context 'existing snippet' do
+
+ let(:path) { 'index' }
+ it { expect(subject.title).to eq({ en: 'Home' }) }
+
+ end
+
+ end
+
+ end