translation repository (both works with MongoDB and Filesystem adapters)
did
committed Feb 27, 2015
commit 93db7d54e8e803dc0770869a19c5069fba309ee9
Showing 20
changed files with
174 additions
and 42 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb
+1
-0
| @@ | @@ -19,6 +19,7 @@ require_relative 'steam/repositories/site_repository' |
| require_relative 'steam/repositories/page_repository' | |
| require_relative 'steam/repositories/editable_element_repository' | |
| require_relative 'steam/repositories/snippet_repository' | |
| + | require_relative 'steam/repositories/translation_repository' |
| require_relative 'steam/services' | |
locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb
+2
-1
| @@ | @@ -6,6 +6,7 @@ require_relative 'filesystem/yaml_loader' |
| require_relative 'filesystem/yaml_loaders/site' | |
| require_relative 'filesystem/yaml_loaders/page' | |
| require_relative 'filesystem/yaml_loaders/snippet' | |
| + | require_relative 'filesystem/yaml_loaders/translation' |
| require_relative 'filesystem/sanitizer' | |
| require_relative 'filesystem/sanitizers/simple' | |
| @@ | @@ -78,7 +79,7 @@ module Locomotive::Steam |
| end | |
| def build_yaml_loaders | |
| - | %i(sites pages snippets).inject({}) do |memo, name| |
| + | %i(sites pages snippets translations).inject({}) do |memo, name| |
| memo[name] = build_klass('YAMLLoaders', name).new(site_path) | |
| memo | |
| end | |
locomotive/steam/adapters/filesystem/sanitizer.rb b/lib/locomotive/steam/adapters/filesystem/sanitizer.rb
+1
-0
| @@ | @@ -33,6 +33,7 @@ module Locomotive::Steam |
| end | |
| def apply_to_entity(entity) | |
| + | attach_site_to(entity) |
| entity | |
| end | |
locomotive/steam/adapters/filesystem/sanitizers/page.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb
+1
-1
| @@ | @@ -16,7 +16,7 @@ module Locomotive::Steam |
| end | |
| def apply_to_entity(entity) | |
| - | attach_site_to(entity) |
| + | super |
| record_id(entity) # required to get the parent_id | |
locomotive/steam/adapters/filesystem/sanitizers/snippet.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/snippet.rb
+2
-1
| @@ | @@ -8,7 +8,8 @@ module Locomotive::Steam |
| include Adapters::Filesystem::Sanitizer | |
| def apply_to_entity(entity) | |
| - | attach_site_to(entity) |
| + | super |
| + | |
| use_default_template_if_missing_locale(entity) | |
| end | |
locomotive/steam/adapters/filesystem/yaml_loaders/translation.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/translation.rb
+37
-0
| @@ | @@ -1,3 +1,40 @@ |
| + | module Locomotive |
| + | module Steam |
| + | module Adapters |
| + | module Filesystem |
| + | module YAMLLoaders |
| + | |
| + | class Translation |
| + | |
| + | include Adapters::Filesystem::YAMLLoader |
| + | |
| + | def load(scope) |
| + | super |
| + | load_array |
| + | end |
| + | |
| + | private |
| + | |
| + | def load_array |
| + | [].tap do |array| |
| + | _load(path).each do |key, values| |
| + | array << { key: key.to_s, values: HashConverter.to_string(values) } |
| + | end |
| + | end |
| + | end |
| + | |
| + | def path |
| + | File.join(site_path, 'config', 'translations.yml') |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
| # module Locomotive | |
| # module Steam | |
| # module Repositories | |
locomotive/steam/entities/snippet.rb b/lib/locomotive/steam/entities/snippet.rb
+0
-25
| @@ | @@ -13,28 +13,3 @@ module Locomotive::Steam |
| end | |
| end | |
| - | |
| - | |
| - | # module Locomotive |
| - | # module Steam |
| - | |
| - | # class SnippetRepository |
| - | # module Filesystem |
| - | # module Models |
| - | |
| - | # class Snippet < Base |
| - | |
| - | # set_localized_attributes [:template, :template_path] |
| - | |
| - | # def initialize(attributes) |
| - | # super({ template: {} }.merge(attributes)) |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | |
| - | # end |
| - | # end |
locomotive/steam/entities/translation.rb b/lib/locomotive/steam/entities/translation.rb
+15
-0
| @@ | @@ -0,0 +1,15 @@ |
| + | module Locomotive::Steam |
| + | |
| + | class Translation |
| + | |
| + | include Locomotive::Steam::Models::Entity |
| + | |
| + | def initialize(attributes = {}) |
| + | super({ |
| + | values: {} |
| + | }.merge(attributes)) |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
locomotive/steam/repositories/snippet_repository.rb b/lib/locomotive/steam/repositories/snippet_repository.rb
+0
-2
| @@ | @@ -5,8 +5,6 @@ module Locomotive |
| include Models::Repository | |
| - | # set_collection model: Filesystem::Models::Snippet, sanitizer: Filesystem::Sanitizers::Snippet |
| - | |
| mapping :snippets, entity: Snippet do | |
| localized_attributes :template_path, :template | |
| end | |
locomotive/steam/repositories/translation_repository.rb b/lib/locomotive/steam/repositories/translation_repository.rb
+18
-0
| @@ | @@ -0,0 +1,18 @@ |
| + | module Locomotive |
| + | module Steam |
| + | |
| + | class TranslationRepository |
| + | |
| + | include Models::Repository |
| + | |
| + | mapping :translations, entity: Translation do |
| + | localized_attributes :template_path, :template |
| + | end |
| + | |
| + | def by_key(key) |
| + | query { where(key: key) }.first |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
locomotive_translations.bson b/spec/fixtures/mongodb/locomotive_translations.bson
+0
-0
locomotive_translations.metadata.json b/spec/fixtures/mongodb/locomotive_translations.metadata.json
+1
-0
| @@ | @@ -0,0 +1 @@ |
| + | { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "locomotive_engine_2_5_dev.locomotive_translations" } ] } |
| \ No newline at end of file | |
sessions.bson b/spec/fixtures/mongodb/sessions.bson
+0
-0
system.indexes.bson b/spec/fixtures/mongodb/system.indexes.bson
+0
-0
spec/integration/mongodb_helper.rb
+3
-3
| @@ | @@ -1,5 +1,5 @@ |
| require File.join(File.dirname(__FILE__), '..', 'spec_helper') | |
| - | # RSpec.configure do |config| |
| - | # config.before(:all) { restore_mongodb } |
| - | # end |
| + | RSpec.configure do |config| |
| + | config.before(:all) { restore_mongodb } |
| + | end |
spec/integration/repositories/page_repository_spec.rb
+3
-3
| @@ | @@ -5,7 +5,7 @@ require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb' |
| describe Locomotive::Steam::PageRepository do | |
| - | shared_examples_for 'page repository' do |
| + | shared_examples_for 'a repository' do |
| let(:site) { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) } | |
| let(:locale) { :en } | |
| @@ | @@ -71,7 +71,7 @@ describe Locomotive::Steam::PageRepository do |
| context 'MongoDB' do | |
| - | it_should_behave_like 'page repository' do |
| + | it_should_behave_like 'a repository' do |
| let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') } | |
| let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) } | |
| @@ | @@ -82,7 +82,7 @@ describe Locomotive::Steam::PageRepository do |
| context 'Filesystem' do | |
| - | it_should_behave_like 'page repository' do |
| + | it_should_behave_like 'a repository' do |
| let(:site_id) { 1 } | |
| let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) } | |
spec/integration/repositories/site_repository_spec.rb
+3
-3
| @@ | @@ -7,7 +7,7 @@ describe Locomotive::Steam::SiteRepository do |
| let(:repository) { described_class.new(adapter) } | |
| - | shared_examples_for 'site repository' do |
| + | shared_examples_for 'a repository' do |
| describe '#all' do | |
| subject { repository.all } | |
| @@ | @@ -25,7 +25,7 @@ describe Locomotive::Steam::SiteRepository do |
| let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) } | |
| - | it_behaves_like 'site repository' |
| + | it_behaves_like 'a repository' |
| end | |
| @@ | @@ -33,7 +33,7 @@ describe Locomotive::Steam::SiteRepository do |
| let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) } | |
| - | it_behaves_like 'site repository' |
| + | it_behaves_like 'a repository' |
| end | |
spec/integration/repositories/snippet_repository_spec.rb
+3
-3
| @@ | @@ -5,7 +5,7 @@ require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb' |
| describe Locomotive::Steam::SnippetRepository do | |
| - | shared_examples_for 'snippet repository' do |
| + | shared_examples_for 'a repository' do |
| let(:site) { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) } | |
| let(:locale) { :en } | |
| @@ | @@ -25,7 +25,7 @@ describe Locomotive::Steam::SnippetRepository do |
| context 'MongoDB' do | |
| - | it_should_behave_like 'snippet repository' do |
| + | it_should_behave_like 'a repository' do |
| let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') } | |
| let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) } | |
| @@ | @@ -36,7 +36,7 @@ describe Locomotive::Steam::SnippetRepository do |
| context 'Filesystem' do | |
| - | it_should_behave_like 'snippet repository' do |
| + | it_should_behave_like 'a repository' do |
| let(:site_id) { 1 } | |
| let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) } | |
spec/integration/repositories/translation_repository_spec.rb
+50
-0
| @@ | @@ -0,0 +1,50 @@ |
| + | require File.join(File.dirname(__FILE__), '..', 'mongodb_helper') |
| + | |
| + | require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| + | require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb' |
| + | |
| + | describe Locomotive::Steam::TranslationRepository do |
| + | |
| + | shared_examples_for 'a repository' do |
| + | |
| + | let(:site) { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) } |
| + | let(:locale) { :en } |
| + | let(:repository) { described_class.new(adapter, site, locale) } |
| + | |
| + | describe '#all' do |
| + | subject { repository.all } |
| + | it { expect(subject.size).to eq 1 } |
| + | end |
| + | |
| + | describe '#by_key' do |
| + | subject { repository.by_key('powered_by') } |
| + | it { expect(subject.values).to eq({ 'en' => 'Powered by', 'fr' => 'Propulsé par' }) } |
| + | end |
| + | |
| + | end |
| + | |
| + | context 'MongoDB' do |
| + | |
| + | it_should_behave_like 'a repository' do |
| + | |
| + | let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') } |
| + | let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | context 'Filesystem' do |
| + | |
| + | it_should_behave_like 'a repository' do |
| + | |
| + | let(:site_id) { 1 } |
| + | let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) } |
| + | |
| + | after(:all) { Locomotive::Steam::Adapters::Filesystem::SimpleCacheStore.new.clear } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
spec/unit/repositories/translation_repository_spec.rb
+34
-0
| @@ | @@ -0,0 +1,34 @@ |
| + | require 'spec_helper' |
| + | |
| + | require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| + | |
| + | describe Locomotive::Steam::TranslationRepository do |
| + | |
| + | let(:translations) { [{ key: 'powered_by', values: { 'en' => 'Powered by Steam', 'fr' => 'Propulsé par Steam' } }] } |
| + | let(:locale) { :en } |
| + | let(:site) { instance_double('Site', _id: 1, default_locale: :en, locales: [:en, :fr]) } |
| + | let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(nil) } |
| + | let(:repository) { described_class.new(adapter, site, locale) } |
| + | |
| + | before do |
| + | allow(adapter).to receive(:collection).and_return(translations) |
| + | adapter.cache = NoCacheStore.new |
| + | end |
| + | |
| + | describe '#by_key' do |
| + | |
| + | let(:key) { nil } |
| + | subject { repository.by_key(key) } |
| + | |
| + | it { is_expected.to eq nil } |
| + | |
| + | context 'existing translation' do |
| + | |
| + | let(:key) { 'powered_by' } |
| + | it { expect(subject.values).to eq({ 'en' => 'Powered by Steam', 'fr' => 'Propulsé par Steam' }) } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | end |