fix the filesystem adapter
did
committed Feb 23, 2015
commit 3a1c69148b966780de42e8e427ac2d07541b03fe
Showing 30
changed files with
149 additions
and 43 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb
+1
-0
| @@ | @@ -17,6 +17,7 @@ require_relative_all 'steam/entities' |
| # TODO | |
| require_relative 'steam/repositories/site_repository' | |
| require_relative 'steam/repositories/page_repository' | |
| + | require_relative 'steam/repositories/editable_element_repository' |
| require_relative 'steam/services' | |
locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb
+6
-2
| @@ | @@ -40,11 +40,15 @@ module Locomotive::Steam |
| def query(mapper, scope, &block) | |
| _query(mapper, scope, &block).tap do |default| | |
| if scope.site | |
| - | default.where(site_id: scope.site.id) |
| + | default.where(site_id: scope.site._id) |
| end | |
| end | |
| end | |
| + | def find(mapper, scope, id) |
| + | _query(mapper, scope) { where(_id: id) }.first |
| + | end |
| + | |
| private | |
| def _query(mapper, scope, &block) | |
| @@ | @@ -81,7 +85,7 @@ module Locomotive::Steam |
| end | |
| def build_yaml_loaders(cache) | |
| - | %i(site page).inject({}) do |memo, name| |
| + | %i(sites pages).inject({}) do |memo, name| |
| memo[name] = build_klass('YAMLLoaders', name).new(site_path, cache) | |
| memo | |
| end | |
locomotive/steam/adapters/filesystem/sanitizers/page.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb
+39
-19
| @@ | @@ -11,25 +11,25 @@ module Locomotive::Steam |
| # super | |
| # @content_types = {} | |
| # @localized = {} | |
| - | # locales.each { |locale| @localized[locale] = {} } |
| + | # |
| # end | |
| def setup(scope) | |
| super.tap do | |
| @ids, @parent_ids = {}, {} | |
| @content_types = {} | |
| - | @localized = Hash.new { {} } |
| + | @localized = locales.inject({}) { |m, l| m[l] = {}; m } |
| end | |
| end | |
| def apply_to_entity(entity) | |
| - | entity[:site_id] = scope.site.id if scope.site |
| + | entity[:site_id] = scope.site._id if scope.site |
| - | # required to get the parent_id |
| - | @ids[entity[:_fullpath]] = entity._id |
| + | record_id(entity) # required to get the parent_id |
| locales.each do |locale| | |
| set_default_redirect_type(entity, locale) | |
| + | modify_if_templatized(entity, locale) |
| end | |
| end | |
| @@ | @@ -40,9 +40,10 @@ module Locomotive::Steam |
| set_fullpath_for(page, locale) | |
| set_parent_id(page) | |
| - | modify_if_templatized(page, locale) |
| use_default_locale_template_path(page, locale) | |
| end | |
| + | |
| + | modify_if_nested_templatized(page) |
| end | |
| end | |
| @@ | @@ -73,23 +74,31 @@ module Locomotive::Steam |
| end | |
| end | |
| - | def modify_if_templatized(page, locale) |
| - | if page.templatized? |
| - | # change the slug of a templatized page |
| - | page[:slug][locale] = 'content-type-template' |
| - | |
| - | # this also means to change the fullpath |
| - | if page[:fullpath][locale] |
| - | page[:fullpath][locale].gsub!(/[^\/]+$/, 'content-type-template') |
| - | end |
| - | |
| - | # make sure its children will have its content type |
| - | set_content_type(page._fullpath, page.content_type) |
| - | elsif content_type = fetch_content_type(parent_fullpath(page)) |
| + | def modify_if_nested_templatized(page) |
| + | if content_type = fetch_content_type(parent_fullpath(page)) |
| # not a templatized page but it becomes one because | |
| # its parent is one of them | |
| page[:content_type] = content_type | |
| end | |
| + | |
| + | # end |
| + | |
| + | # if page.templatized? |
| + | # # change the slug of a templatized page |
| + | # page[:slug][locale] = 'content-type-template' |
| + | |
| + | # # this also means to change the fullpath |
| + | # if page[:fullpath][locale] |
| + | # page[:fullpath][locale].gsub!(/[^\/]+$/, 'content-type-template') |
| + | # end |
| + | |
| + | # # make sure its children will have its content type |
| + | # set_content_type(page._fullpath, page.content_type) |
| + | # elsif content_type = fetch_content_type(parent_fullpath(page)) |
| + | # # not a templatized page but it becomes one because |
| + | # # its parent is one of them |
| + | # page[:content_type] = content_type |
| + | # end |
| end | |
| def set_fullpath_for(page, locale) | |
| @@ | @@ -150,6 +159,17 @@ module Locomotive::Steam |
| @localized[locale][fullpath] = value | |
| end | |
| + | def record_id(entity) |
| + | @ids[entity[:_fullpath]] = entity._id |
| + | end |
| + | |
| + | def modify_if_templatized(page, locale) |
| + | if page.templatized? |
| + | page[:slug][locale] = 'content-type-template' |
| + | set_content_type(page[:_fullpath], page.content_type) |
| + | end |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/adapters/mongodb/query.rb b/lib/locomotive/steam/adapters/mongodb/query.rb
+1
-1
| @@ | @@ -25,7 +25,7 @@ module Locomotive::Steam |
| private | |
| def apply_default_scope | |
| - | where(site_id: @scope.site.id) if @scope.site |
| + | where(site_id: @scope.site._id) if @scope.site |
| end | |
| end | |
spec/fixtures/default/app/content_types/events.yml
+2
-2
| @@ | @@ -21,5 +21,5 @@ fields: |
| type: tags | |
| label: List of tags | |
| - price: | |
| - | type: Float |
| - | label: Price of the event |
| \ No newline at end of file | |
| + | type: float |
| + | label: Price of the event |
spec/fixtures/default/config/deploy.yml
+2
-2
| @@ | @@ -1,7 +1,7 @@ |
| development: | |
| - | host: development.example.com |
| + | host: sample.lvh.me:3000 |
| email: john@doe.net | |
| - | password: easyone |
| + | api_key: a9ac1e08c2c22c1b6f3da6db77a70cac4a615bd7 |
| staging: | |
| host: staging.example.com | |
| email: john@doe.net | |
locomotive_accounts.bson b/spec/fixtures/mongodb/locomotive_accounts.bson
+0
-0
locomotive_accounts.metadata.json b/spec/fixtures/mongodb/locomotive_accounts.metadata.json
+1
-1
| @@ | @@ -1 +1 @@ |
| - | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_dev.locomotive_accounts" } ] } |
| \ No newline at end of file | |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.locomotive_accounts" } ] } |
| \ No newline at end of file | |
locomotive_content_assets.bson b/spec/fixtures/mongodb/locomotive_content_assets.bson
+0
-0
locomotive_content_assets.metadata.json b/spec/fixtures/mongodb/locomotive_content_assets.metadata.json
+1
-1
| @@ | @@ -1 +1 @@ |
| - | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_dev.locomotive_content_assets" } ] } |
| \ No newline at end of file | |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.locomotive_content_assets" } ] } |
| \ No newline at end of file | |
locomotive_content_entries.bson b/spec/fixtures/mongodb/locomotive_content_entries.bson
+0
-0
locomotive_content_entries.metadata.json b/spec/fixtures/mongodb/locomotive_content_entries.metadata.json
+1
-1
| @@ | @@ -1 +1 @@ |
| - | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_dev.locomotive_content_entries" } ] } |
| \ No newline at end of file | |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.locomotive_content_entries" } ] } |
| \ No newline at end of file | |
locomotive_content_types.bson b/spec/fixtures/mongodb/locomotive_content_types.bson
+0
-0
locomotive_content_types.metadata.json b/spec/fixtures/mongodb/locomotive_content_types.metadata.json
+1
-1
| @@ | @@ -1 +1 @@ |
| - | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_dev.locomotive_content_types" } ] } |
| \ No newline at end of file | |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.locomotive_content_types" } ] } |
| \ No newline at end of file | |
locomotive_pages.bson b/spec/fixtures/mongodb/locomotive_pages.bson
+0
-0
locomotive_pages.metadata.json b/spec/fixtures/mongodb/locomotive_pages.metadata.json
+1
-1
| @@ | @@ -1 +1 @@ |
| - | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_dev.locomotive_pages" } ] } |
| \ No newline at end of file | |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.locomotive_pages" } ] } |
| \ No newline at end of file | |
locomotive_sites.bson b/spec/fixtures/mongodb/locomotive_sites.bson
+0
-0
locomotive_sites.metadata.json b/spec/fixtures/mongodb/locomotive_sites.metadata.json
+1
-1
| @@ | @@ -1 +1 @@ |
| - | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_dev.locomotive_sites" } ] } |
| \ No newline at end of file | |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.locomotive_sites" } ] } |
| \ No newline at end of file | |
locomotive_snippets.bson b/spec/fixtures/mongodb/locomotive_snippets.bson
+0
-0
locomotive_snippets.metadata.json b/spec/fixtures/mongodb/locomotive_snippets.metadata.json
+1
-1
| @@ | @@ -1 +1 @@ |
| - | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_dev.locomotive_snippets" } ] } |
| \ No newline at end of file | |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.locomotive_snippets" } ] } |
| \ No newline at end of file | |
locomotive_theme_assets.bson b/spec/fixtures/mongodb/locomotive_theme_assets.bson
+0
-0
locomotive_theme_assets.metadata.json b/spec/fixtures/mongodb/locomotive_theme_assets.metadata.json
+1
-1
| @@ | @@ -1 +1 @@ |
| - | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_dev.locomotive_theme_assets" } ] } |
| \ No newline at end of file | |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.locomotive_theme_assets" } ] } |
| \ No newline at end of file | |
sessions.bson b/spec/fixtures/mongodb/sessions.bson
+0
-0
sessions.metadata.json b/spec/fixtures/mongodb/sessions.metadata.json
+1
-1
| @@ | @@ -1 +1 @@ |
| - | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_dev.sessions" } ] } |
| \ No newline at end of file | |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.sessions" } ] } |
| \ No newline at end of file | |
system.indexes.bson b/spec/fixtures/mongodb/system.indexes.bson
+0
-0
spec/integration/repositories/page_repository_spec.rb
+54
-0
| @@ | @@ -0,0 +1,54 @@ |
| + | require 'spec_helper' |
| + | |
| + | require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| + | require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb' |
| + | |
| + | describe Locomotive::Steam::PageRepository do |
| + | |
| + | let(:site) { Locomotive::Steam::Site.new(_id: 1, locales: %w(en fr nb)) } |
| + | let(:locale) { :en } |
| + | let(:repository) { Locomotive::Steam::PageRepository.new(adapter, site, locale) } |
| + | |
| + | # context 'MongoDB' do |
| + | |
| + | # let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) } |
| + | |
| + | # describe '#all' do |
| + | # subject { repository.all } |
| + | # it { expect(subject.size).to eq 1 } |
| + | # end |
| + | |
| + | # describe '#query' do |
| + | # subject { repository.query { where(handle: 'acme') }.first } |
| + | # it { expect(subject.name).to eq 'My portfolio' } |
| + | # end |
| + | |
| + | # end |
| + | |
| + | context 'Filesystem' do |
| + | |
| + | let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) } |
| + | |
| + | describe '#all' do |
| + | subject { repository.all } |
| + | it { expect(subject.size).to eq 21 } |
| + | end |
| + | |
| + | describe '#query' do |
| + | subject { repository.query { where(fullpath: 'index') }.first } |
| + | it { expect(subject.title[:en]).to eq 'Home page' } |
| + | end |
| + | |
| + | describe '#by_handle' do |
| + | subject { repository.by_handle('our-music') } |
| + | it { expect(subject.title[:en]).to eq 'Music' } |
| + | end |
| + | |
| + | describe '#by_fullpath' do |
| + | subject { repository.by_fullpath('archives/news') } |
| + | it { expect(subject.title[:en]).to eq 'News archive' } |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
spec/integration/repositories/site_repository_spec.rb
+2
-2
| @@ | @@ -17,8 +17,8 @@ describe Locomotive::Steam::SiteRepository do |
| end | |
| describe '#query' do | |
| - | subject { repository.query { where(handle: 'acme') }.first } |
| - | it { expect(subject.name).to eq 'My portfolio' } |
| + | subject { repository.query { where(subdomain: 'sample') }.first } |
| + | it { expect(subject.name).to eq 'Sample website' } |
| end | |
| end | |
spec/support/helpers.rb
+2
-2
| @@ | @@ -20,8 +20,8 @@ module Spec |
| def restore_mongodb | |
| path = File.join(File.expand_path(File.dirname(__FILE__)), '..', 'fixtures', 'mongodb') | |
| - | # `mongo steam_test --eval "db.dropDatabase()"` |
| - | # `mongorestore -d steam_test #{path}` |
| + | `mongo steam_test --eval "db.dropDatabase()"` |
| + | `mongorestore -d steam_test #{path}` |
| end | |
| def run_server | |
spec/unit/adapters/filesystem_adapter_spec.rb
+2
-2
| @@ | @@ -28,12 +28,12 @@ describe Locomotive::Steam::FilesystemAdapter do |
| context 'scoped by a site' do | |
| - | let(:site) { instance_double('Site', id: 42) } |
| + | let(:site) { instance_double('Site', _id: 42) } |
| it { expect(subject.first.name).to eq 'Hello world' } | |
| context 'unknown site id' do | |
| - | let(:site) { instance_double('Site', id: 1) } |
| + | let(:site) { instance_double('Site', _id: 1) } |
| it { expect(subject.first).to eq nil } | |
| end | |
spec/unit/repositories/page_repository_spec.rb
+29
-2
| @@ | @@ -6,9 +6,8 @@ require_relative '../../../lib/locomotive/steam/repositories/editable_element_re |
| describe Locomotive::Steam::PageRepository do | |
| let(:pages) { [{ title: { en: 'Home' }, handle: 'home', slug: { en: 'index' }, _fullpath: 'index', template_path: { en: 'index.liquid' } }] } | |
| - | |
| let(:locale) { :en } | |
| - | let(:site) { instance_double('Site', id: 1, default_locale: :en, locales: %i(en fr)) } |
| + | let(:site) { instance_double('Site', _id: 1, default_locale: :en, locales: %i(en fr)) } |
| let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(nil) } | |
| let(:repository) { Locomotive::Steam::PageRepository.new(adapter, site, locale) } | |
| @@ | @@ -51,6 +50,34 @@ describe Locomotive::Steam::PageRepository do |
| end | |
| + | describe 'templatized pages' do |
| + | |
| + | let(:pages) do |
| + | [ |
| + | { title: { en: 'Comments' }, slug: { en: 'comments' }, _fullpath: 'articles/template/comments' }, |
| + | { title: { en: 'Template Article' }, slug: { en: 'template' }, content_type: :articles, _fullpath: 'articles/template' }, |
| + | { title: { en: 'Articles' }, slug: { en: 'articles' }, _fullpath: 'articles' }, |
| + | { title: { en: 'Home' }, slug: { en: 'index' }, _fullpath: 'index', template_path: { en: 'index.liquid' } } |
| + | ] |
| + | end |
| + | |
| + | it { expect(repository.all.size).to eq 4 } |
| + | it { expect(repository.find(2).templatized?).to eq true } |
| + | |
| + | describe 'nested templatized page' do |
| + | |
| + | subject { repository.find(1) } |
| + | |
| + | it { expect(subject.templatized?).to eq true } |
| + | it { expect(subject.content_type).to eq :articles } |
| + | it { expect(subject.slug[:en]).to eq 'comments' } |
| + | it { expect(subject.slug[:fr]).to eq nil } |
| + | it { expect(subject.fullpath[:en]).to eq 'articles/content-type-template/comments' } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| describe '#by_fullpath' do | |
| let(:path) { nil } | |