content type repository (both works with MongoDB and Filesystem adapters)
did
committed Feb 28, 2015
commit d7f0739f2a243f37d1292e2014c999b1ee84d6c2
Showing 29
changed files with
593 additions
and 261 deletions
Rakefile
+1
-1
| @@ | @@ -26,7 +26,7 @@ end |
| RSpec::Core::RakeTask.new('spec:unit') do |spec| | |
| # spec.pattern = 'spec/unit/**/*_spec.rb' | |
| spec.pattern = 'spec/unit/{services,core_ext,middlewares,decorators,adapters,entities,models,repositories}/**/*_spec.rb' | |
| + | # spec.pattern = 'spec/unit/{adapters,entities,models,repositories}/**/*_spec.rb' |
| end | |
| - | |
| task default: :spec | |
locomotive/steam.rb b/lib/locomotive/steam.rb
+3
-0
| @@ | @@ -21,6 +21,9 @@ require_relative 'steam/repositories/editable_element_repository' |
| require_relative 'steam/repositories/snippet_repository' | |
| require_relative 'steam/repositories/translation_repository' | |
| require_relative 'steam/repositories/theme_asset_repository' | |
| + | require_relative 'steam/repositories/content_type_repository' |
| + | require_relative 'steam/repositories/content_type_field_repository' |
| + | require_relative 'steam/repositories/content_type_field_select_option_repository' |
| require_relative 'steam/services' | |
locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb
+5
-11
| @@ | @@ -3,16 +3,10 @@ require_relative 'memory' |
| require_relative 'filesystem/simple_cache_store' | |
| 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/yaml_loaders/theme_asset' |
| + | require_relative_all 'filesystem/yaml_loaders' |
| require_relative 'filesystem/sanitizer' | |
| - | require_relative 'filesystem/sanitizers/simple' |
| - | require_relative 'filesystem/sanitizers/page' |
| - | require_relative 'filesystem/sanitizers/snippet' |
| + | require_relative_all 'filesystem/sanitizers' |
| module Locomotive::Steam | |
| @@ | @@ -69,7 +63,7 @@ module Locomotive::Steam |
| def populate_dataset(dataset, mapper, scope) | |
| sanitizers[mapper.name].with(scope) do |sanitizer| | |
| collection(mapper, scope).each do |attributes| | |
| - | entity = mapper.to_entity(attributes) |
| + | entity = mapper.to_entity(attributes.dup) |
| dataset.insert(entity) | |
| sanitizer.apply_to(entity) | |
| @@ | @@ -84,7 +78,7 @@ module Locomotive::Steam |
| end | |
| def build_yaml_loaders | |
| - | %i(sites pages snippets translations theme_assets).inject({}) do |memo, name| |
| + | %i(sites pages content_types snippets translations theme_assets).inject({}) do |memo, name| |
| memo[name] = build_klass('YAMLLoaders', name).new(site_path) | |
| memo | |
| end | |
| @@ | @@ -92,7 +86,7 @@ module Locomotive::Steam |
| def build_sanitizers | |
| hash = Hash.new { build_klass('Sanitizers', :simple).new } | |
| - | %i(pages snippets).inject(hash) do |memo, name| |
| + | %i(pages content_types snippets).inject(hash) do |memo, name| |
| memo[name] = build_klass('Sanitizers', name).new | |
| memo | |
| end | |
locomotive/steam/adapters/filesystem/sanitizers/content_type.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/content_type.rb
+15
-43
| @@ | @@ -1,49 +1,21 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module Sanitizers |
| + | module Locomotive::Steam |
| + | module Adapters |
| + | module Filesystem |
| + | module Sanitizers |
| - | # class ContentType < Struct.new(:default_locale, :locales) |
| + | class ContentType |
| - | # def apply_to(collection) |
| - | # collection.each do |content_type| |
| - | # if list = content_type.attributes[:fields] |
| - | # content_type[:slug] = content_type[:slug].to_s |
| - | # content_type.fields = build_fields(list) |
| - | # end |
| - | # build_fields_by_name_shortcut(content_type) |
| - | # end |
| - | # end |
| + | include Adapters::Filesystem::Sanitizer |
| - | # def build_fields(list) |
| - | # list.map do |attributes| |
| - | # name, _attributes = attributes.keys.first, attributes.values.first |
| + | def apply_to_entity(entity) |
| + | super |
| - | # _attributes[:name] = name.to_sym |
| + | entity[:slug] = entity[:slug].to_s |
| + | end |
| - | # if _attributes[:label].blank? |
| - | # _attributes[:label] = name.to_s.humanize |
| - | # end |
| + | end |
| - | # _attributes[:type] = _attributes[:type].try(:to_sym) |
| - | |
| - | # Filesystem::Models::ContentTypeField.new(_attributes) |
| - | # end |
| - | # end |
| - | |
| - | # def build_fields_by_name_shortcut(content_type) |
| - | # content_type.fields_by_name = {} |
| - | |
| - | # (content_type.fields || []).each do |field| |
| - | # content_type.fields_by_name[field.name] = field |
| - | # end |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| + | end |
| + | end |
| + | end |
| + | end |
locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb
+96
-42
| @@ | @@ -1,42 +1,96 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module YAMLLoaders |
| - | |
| - | # class ContentType < Struct.new(:root_path, :cache) |
| - | |
| - | # include YAMLLoaders::Concerns::Common |
| - | |
| - | # def list_of_attributes |
| - | # cache.fetch('app/content_types') { load_list } |
| - | # end |
| - | |
| - | # private |
| - | |
| - | # def load_list |
| - | # [].tap do |array| |
| - | # each_file do |filepath, slug| |
| - | # array << { slug: slug }.merge(load(filepath)) |
| - | # end |
| - | # end |
| - | # end |
| - | |
| - | # def each_file(&block) |
| - | # Dir.glob(File.join(path, "*.yml")).each do |filepath| |
| - | # slug = File.basename(filepath, '.yml') |
| - | # yield(filepath, slug) |
| - | # end |
| - | # end |
| - | |
| - | # def path |
| - | # File.join(root_path, 'app', 'content_types') |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| + | module Locomotive |
| + | module Steam |
| + | module Adapters |
| + | module Filesystem |
| + | module YAMLLoaders |
| + | |
| + | class ContentType |
| + | |
| + | include Adapters::Filesystem::YAMLLoader |
| + | |
| + | def load(scope) |
| + | super |
| + | load_list |
| + | end |
| + | |
| + | private |
| + | |
| + | def load_list |
| + | [].tap do |array| |
| + | each_file do |filepath, slug| |
| + | attributes = _load(filepath) |
| + | attributes[:entries_custom_fields] = build_fields(attributes.delete(:fields)) |
| + | array << { slug: slug }.merge(attributes) |
| + | end |
| + | end |
| + | end |
| + | |
| + | def build_fields(list) |
| + | list.map do |attributes| |
| + | build_field(attributes.keys.first, attributes.values.first) |
| + | end |
| + | end |
| + | |
| + | def build_field(name, attributes) |
| + | attributes.tap do |attributes| |
| + | attributes[:name] = name.to_s |
| + | attributes[:type] = attributes[:type].try(:to_s) |
| + | |
| + | if attributes[:label].blank? |
| + | attributes[:label] = name.to_s.humanize |
| + | end |
| + | |
| + | if select_options = attributes.delete(:select_options) |
| + | attributes[:select_options] = build_select_options(select_options) |
| + | end |
| + | end |
| + | end |
| + | |
| + | def build_select_options(options) |
| + | if options.is_a?(Hash) |
| + | build_select_options_from_hash(options) |
| + | else |
| + | build_select_options_from_array(options) |
| + | end |
| + | end |
| + | |
| + | def build_select_options_from_hash(options) |
| + | [].tap do |list| |
| + | options.each do |locale, values| |
| + | values.each_with_index do |name, position| |
| + | if (option = list.at(position)).nil? |
| + | list << { name: { locale => name }, position: position } |
| + | else |
| + | option[name][locale] = name |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
| + | def build_select_options_from_array(options) |
| + | [].tap do |list| |
| + | options.each_with_index do |name, position| |
| + | list << { name: name, position: position } |
| + | end |
| + | end |
| + | end |
| + | |
| + | def each_file(&block) |
| + | Dir.glob(File.join(path, "*.yml")).each do |filepath| |
| + | slug = File.basename(filepath, '.yml') |
| + | yield(filepath, slug) |
| + | end |
| + | end |
| + | |
| + | def path |
| + | File.join(site_path, 'app', 'content_types') |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
locomotive/steam/adapters/memory.rb b/lib/locomotive/steam/adapters/memory.rb
+3
-1
| @@ | @@ -32,7 +32,9 @@ module Locomotive::Steam |
| def dataset(mapper, scope) | |
| Locomotive::Steam::Adapters::Memory::Dataset.new(mapper.name).tap do |dataset| | |
| collection.each do |attributes| | |
| - | entity = mapper.to_entity(attributes) |
| + | # Note: very important to not manipulate the original attributes |
| + | # since the attributes might be modified further by the to_entity method |
| + | entity = mapper.to_entity(attributes.dup) |
| dataset.insert(entity) | |
| end | |
| end | |
locomotive/steam/entities/content_type.rb b/lib/locomotive/steam/entities/content_type.rb
+29
-0
| @@ | @@ -0,0 +1,29 @@ |
| + | module Locomotive::Steam |
| + | |
| + | class ContentType |
| + | |
| + | include Locomotive::Steam::Models::Entity |
| + | |
| + | def initialize(attributes = {}) |
| + | super({ |
| + | order_by: '_position', |
| + | order_direction: 'asc' |
| + | }.merge(attributes)) |
| + | end |
| + | |
| + | def fields |
| + | # Note: this returns an instance of the ContentTypeFieldRepository class |
| + | self.entries_custom_fields |
| + | end |
| + | |
| + | def label_field_name |
| + | (self[:label_field_name] || fields.first.name).to_sym |
| + | end |
| + | |
| + | def order_by |
| + | name = self[:order_by] == 'manually' ? '_position' : self[:order_by] |
| + | [name, self.order_direction] |
| + | end |
| + | |
| + | end |
| + | end |
locomotive/steam/entities/content_type_field.rb b/lib/locomotive/steam/entities/content_type_field.rb
+34
-0
| @@ | @@ -0,0 +1,34 @@ |
| + | module Locomotive::Steam |
| + | |
| + | class ContentTypeField |
| + | |
| + | include Locomotive::Steam::Models::Entity |
| + | |
| + | attr_accessor :content_type |
| + | |
| + | def initialize(attributes = {}) |
| + | super({ |
| + | type: :string, |
| + | localized: false, |
| + | required: false, |
| + | unique: false |
| + | }.merge(attributes)) |
| + | end |
| + | |
| + | def class_name |
| + | self[:class_name] || self[:target] |
| + | end |
| + | |
| + | def required?; self[:required]; end |
| + | def localized?; self[:localized]; end |
| + | |
| + | class SelectOption |
| + | |
| + | include Locomotive::Steam::Models::Entity |
| + | |
| + | attr_accessor :field |
| + | |
| + | end |
| + | |
| + | end |
| + | end |
locomotive/steam/models/repository.rb b/lib/locomotive/steam/models/repository.rb
+0
-16
| @@ | @@ -35,22 +35,6 @@ module Locomotive::Steam |
| alias :all :query | |
| - | # def create(entity) |
| - | # entity.id = adapter.create(collection_name, entity) |
| - | # end |
| - | |
| - | # def persisted?(entity) |
| - | # !!entity.id && adapter.persisted?(collection_name, entity) |
| - | # end |
| - | |
| - | # def update(entity) |
| - | # adapter.update(collection_name, entity) |
| - | # end |
| - | |
| - | # def destroy(entity) |
| - | # adapter.destroy(collection_name, entity) |
| - | # end |
| - | |
| def mapper | |
| name, options, block = mapper_options | |
| @mapper ||= Mapper.new(name, options, self, &block) | |
locomotive/steam/repositories/content_type_field_repository.rb b/lib/locomotive/steam/repositories/content_type_field_repository.rb
+39
-0
| @@ | @@ -0,0 +1,39 @@ |
| + | module Locomotive |
| + | module Steam |
| + | |
| + | class ContentTypeFieldRepository |
| + | |
| + | include Models::Repository |
| + | |
| + | attr_accessor :content_type |
| + | |
| + | # Entity mapping |
| + | mapping :content_type_fields, entity: ContentTypeField do |
| + | default_attribute :content_type, -> (repository) { repository.content_type } |
| + | |
| + | # embedded association |
| + | association :select_options, ContentTypeFieldSelectOptionRepository |
| + | end |
| + | |
| + | def unique |
| + | query { where(unique: true) }.all.inject({}) do |memo, field| |
| + | memo[field.name] = field |
| + | memo |
| + | end |
| + | end |
| + | |
| + | def localized_names |
| + | query { where(localized: true) }.all.map(&:name) |
| + | end |
| + | |
| + | def select_options(name) |
| + | if field = first { where(name: name, type: 'select') } |
| + | field.select_options.all |
| + | else |
| + | nil |
| + | end |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
locomotive/steam/repositories/content_type_field_select_option_repository.rb b/lib/locomotive/steam/repositories/content_type_field_select_option_repository.rb
+23
-0
| @@ | @@ -0,0 +1,23 @@ |
| + | module Locomotive |
| + | module Steam |
| + | |
| + | class ContentTypeFieldSelectOptionRepository |
| + | |
| + | include Models::Repository |
| + | |
| + | attr_accessor :content_type_field |
| + | |
| + | # Entity mapping |
| + | mapping :content_type_field_select_options, entity: ContentTypeField::SelectOption do |
| + | default_attribute :field, -> (repository) { repository.content_type_field } |
| + | |
| + | localized_attributes :name |
| + | end |
| + | |
| + | def all |
| + | query { order_by(position: :asc) }.all |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
locomotive/steam/repositories/content_type_repository.rb b/lib/locomotive/steam/repositories/content_type_repository.rb
+39
-0
| @@ | @@ -0,0 +1,39 @@ |
| + | module Locomotive |
| + | module Steam |
| + | |
| + | class ContentTypeRepository |
| + | |
| + | include Models::Repository |
| + | |
| + | # Entity mapping |
| + | mapping :content_types, entity: ContentType do |
| + | # embedded association |
| + | association :entries_custom_fields, ContentTypeFieldRepository |
| + | end |
| + | |
| + | def by_slug(slug_or_content_type) |
| + | if slug_or_content_type.is_a?(String) |
| + | query { where(slug: slug_or_content_type) }.first |
| + | else |
| + | slug_or_content_type |
| + | end |
| + | end |
| + | |
| + | def look_for_unique_fields(content_type) |
| + | return nil if content_type.nil? |
| + | content_type.fields.unique |
| + | end |
| + | |
| + | def fields_for(content_type) |
| + | return nil if content_type.nil? |
| + | content_type.fields |
| + | end |
| + | |
| + | def select_options(content_type, name) |
| + | return nil if content_type.nil? || name.nil? |
| + | content_type.fields.select_options(name.to_s) |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
locomotive/steam/repositories/editable_element_repository.rb b/lib/locomotive/steam/repositories/editable_element_repository.rb
+1
-0
| @@ | @@ -7,6 +7,7 @@ module Locomotive |
| attr_accessor :page | |
| + | # Entity mapping |
| mapping :editable_elements, entity: EditableElement do | |
| localized_attributes :content, :source, :default_content, :default_source_url | |
locomotive/steam/repositories/filesystem/models/content_type.rb b/lib/locomotive/steam/repositories/filesystem/models/content_type.rb
+0
-41
| @@ | @@ -1,41 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module Models |
| - | |
| - | # class ContentType < Base |
| - | |
| - | # attr_accessor :fields, :fields_by_name |
| - | |
| - | # def initialize(attributes = {}) |
| - | # super({ |
| - | # order_by: '_position', |
| - | # order_direction: 'asc' |
| - | # }.merge(attributes)) |
| - | # end |
| - | |
| - | # def label_field_name |
| - | # (self[:label_field_name] || fields.first.name).to_sym |
| - | # end |
| - | |
| - | # def localized_fields_names |
| - | # query_fields { where(localized: true) }.all.map(&:name) |
| - | # end |
| - | |
| - | # def order_by |
| - | # name = self[:order_by] == 'manually' ? '_position' : self[:order_by] |
| - | # "#{name} #{self.order_direction}" |
| - | # end |
| - | |
| - | # def query_fields(&block) |
| - | # Filesystem::MemoryAdapter::Query.new(fields, &block) |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/models/content_type_field.rb b/lib/locomotive/steam/repositories/filesystem/models/content_type_field.rb
+0
-31
| @@ | @@ -1,31 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module Models |
| - | |
| - | # class ContentTypeField < Base |
| - | |
| - | # def initialize(attributes) |
| - | # super({ |
| - | # type: :string, |
| - | # localized: false, |
| - | # required: false, |
| - | # unique: false |
| - | # }.merge(attributes)) |
| - | # end |
| - | |
| - | # def class_name |
| - | # self[:class_name] || self[:target] |
| - | # end |
| - | |
| - | # def required?; self[:required]; end |
| - | # def localized?; self[:localized]; end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/site_repository.rb b/lib/locomotive/steam/repositories/site_repository.rb
+1
-0
| @@ | @@ -5,6 +5,7 @@ module Locomotive |
| include Models::Repository | |
| + | # Entity mapping |
| mapping :sites, entity: Site do | |
| localized_attributes :seo_title, :meta_description, :meta_keywords | |
| end | |
locomotive/steam/repositories/snippet_repository.rb b/lib/locomotive/steam/repositories/snippet_repository.rb
+1
-0
| @@ | @@ -5,6 +5,7 @@ module Locomotive |
| include Models::Repository | |
| + | # Entity mapping |
| mapping :snippets, entity: Snippet do | |
| localized_attributes :template_path, :template | |
| end | |
locomotive/steam/repositories/theme_asset_repository.rb b/lib/locomotive/steam/repositories/theme_asset_repository.rb
+1
-4
| @@ | @@ -5,16 +5,13 @@ module Locomotive |
| include Models::Repository | |
| + | # Entity mapping |
| mapping :theme_assets, entity: ThemeAsset | |
| - | # Engine: ['', 'sites', site._id.to_s, 'theme', path].join('/') |
| - | # Wagon: '/' + path |
| def url_for(path) | |
| [adapter.theme_assets_base_url(scope), path].join('/') | |
| end | |
| - | # Engine: site.theme_assets.checksums |
| - | # Wagon: {} |
| def checksums | |
| query { only(:checksum, :local_path) }.all.inject({}) do |memo, asset| | |
| memo[asset.local_path] = asset.checksum | |
locomotive/steam/repositories/translation_repository.rb b/lib/locomotive/steam/repositories/translation_repository.rb
+1
-0
| @@ | @@ -5,6 +5,7 @@ module Locomotive |
| include Models::Repository | |
| + | # Entity mapping |
| mapping :translations, entity: Translation | |
| def by_key(key) | |
spec/integration/repositories/content_type_repository_spec.rb
+69
-0
| @@ | @@ -0,0 +1,69 @@ |
| + | 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::ContentTypeRepository 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 5 } |
| + | end |
| + | |
| + | describe '#by_slug' do |
| + | subject { repository.by_slug('bands') } |
| + | it { expect(subject.description).to eq 'List of bands' } |
| + | end |
| + | |
| + | describe '#fields_for' do |
| + | let(:type) { repository.by_slug('bands') } |
| + | subject { repository.fields_for(type) } |
| + | it { expect(subject.first.hint).to eq 'Name of the band' } |
| + | end |
| + | |
| + | describe '#look_for_unique_fields' do |
| + | let(:type) { repository.by_slug('bands') } |
| + | subject { repository.look_for_unique_fields(type) } |
| + | it { expect(subject.size).to eq 0 } |
| + | end |
| + | |
| + | describe '#select_options' do |
| + | let(:type) { repository.by_slug('updates') } |
| + | subject { repository.select_options(type, :category) } |
| + | it { expect(subject.size).to eq 4 } |
| + | it { expect(subject.first.name.translations).to eq({ 'en' => 'General', 'fr' => 'Général' }) } |
| + | 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/adapters/filesystem/yaml_loaders/content_type_spec.rb
+18
-13
| @@ | @@ -1,20 +1,25 @@ |
| - | # require 'spec_helper' |
| + | require 'spec_helper' |
| - | # describe Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::ContentType do |
| + | require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_loader.rb' |
| + | require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb' |
| - | # let(:root_path) { default_fixture_site_path } |
| - | # let(:cache) { NoCacheStore.new } |
| - | # let(:loader) { Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::ContentType.new(root_path, cache) } |
| + | describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::ContentType do |
| - | # describe '#list_of_attributes' do |
| + | let(:site_path) { default_fixture_site_path } |
| + | let(:loader) { described_class.new(site_path) } |
| - | # subject { loader.list_of_attributes.sort { |a, b| a[:slug] <=> b[:slug] } } |
| + | describe '#load' do |
| - | # it 'tests various stuff' do |
| - | # expect(subject.size).to eq 5 |
| - | # expect(subject.first[:slug]).to eq('bands') |
| - | # end |
| + | let(:scope) { instance_double('Scope', locale: :en) } |
| - | # end |
| + | subject { loader.load(scope).sort { |a, b| a[:slug] <=> b[:slug] } } |
| - | # end |
| + | it 'tests various stuff' do |
| + | expect(subject.size).to eq 5 |
| + | expect(subject.first[:slug]).to eq('bands') |
| + | expect(subject.first[:entries_custom_fields].size).to eq 5 |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
spec/unit/adapters/filesystem/yaml_loaders/page_spec.rb
+1
-1
| @@ | @@ -6,7 +6,7 @@ require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_l |
| describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::Page do | |
| let(:site_path) { default_fixture_site_path } | |
| - | let(:loader) { Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::Page.new(site_path) } |
| + | let(:loader) { described_class.new(site_path) } |
| describe '#load' do | |
spec/unit/adapters/filesystem/yaml_loaders/site_spec.rb
+1
-1
| @@ | @@ -6,7 +6,7 @@ require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_l |
| describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::Site do | |
| let(:site_path) { default_fixture_site_path } | |
| - | let(:loader) { Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::Site.new(site_path) } |
| + | let(:loader) { described_class.new(site_path) } |
| describe '#load' do | |
spec/unit/adapters/filesystem/yaml_loaders/snippet_spec.rb
+19
-16
| @@ | @@ -1,23 +1,26 @@ |
| - | # require 'spec_helper' |
| + | require 'spec_helper' |
| - | # describe Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Snippet do |
| + | require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_loader.rb' |
| + | require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_loaders/snippet.rb' |
| - | # 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 Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::Snippet do |
| - | # describe '#list_of_attributes' do |
| + | let(:site_path) { default_fixture_site_path } |
| + | let(:loader) { described_class.new(site_path) } |
| - | # subject { loader.list_of_attributes.sort { |a, b| a[:name] <=> b[:name] } } |
| + | describe '#load' do |
| - | # it 'tests various stuff' do |
| - | # expect(subject.size).to eq 4 |
| - | # expect(subject.first[:slug]).to eq('a_complicated-one') |
| - | # expect(subject[1][:name]).to eq('Footer') |
| - | # expect(subject[1][:slug]).to eq('footer') |
| - | # end |
| + | let(:scope) { instance_double('Scope', locale: :en) } |
| - | # end |
| + | subject { loader.load(scope).sort { |a, b| a[:name] <=> b[:name] } } |
| - | # end |
| + | it 'tests various stuff' do |
| + | expect(subject.size).to eq 4 |
| + | expect(subject.first[:slug]).to eq('a_complicated-one') |
| + | expect(subject[1][:name]).to eq('Footer') |
| + | expect(subject[1][:slug]).to eq('footer') |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
spec/unit/adapters/filesystem/yaml_loaders/translation_spec.rb
+18
-14
| @@ | @@ -1,21 +1,25 @@ |
| - | # require 'spec_helper' |
| + | require 'spec_helper' |
| - | # describe Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Translation do |
| + | require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_loader.rb' |
| + | require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_loaders/translation.rb' |
| - | # let(:root_path) { default_fixture_site_path } |
| - | # let(:cache) { NoCacheStore.new } |
| - | # let(:loader) { Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Translation.new(root_path, cache) } |
| + | describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::Translation do |
| - | # describe '#list_of_attributes' do |
| + | let(:site_path) { default_fixture_site_path } |
| + | let(:loader) { described_class.new(site_path) } |
| - | # subject { loader.list_of_attributes } |
| + | describe '#load' do |
| - | # 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 |
| + | let(:scope) { instance_double('Scope', locale: :en) } |
| - | # end |
| + | subject { loader.load(scope) } |
| - | # end |
| + | 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 |
spec/unit/entities/content_type_spec.rb
+48
-0
| @@ | @@ -0,0 +1,48 @@ |
| + | require 'spec_helper' |
| + | |
| + | describe Locomotive::Steam::ContentType do |
| + | |
| + | let(:fields) { [instance_double('Field1', label: 'Title', name: :title, localized: false), instance_double('Field2', label: 'Author', name: :author, localized: true)] } |
| + | let(:content_type) { described_class.new(name: 'Articles') } |
| + | |
| + | before { allow(content_type).to receive(:fields).and_return(fields) } |
| + | |
| + | describe '#label_field_name' do |
| + | |
| + | subject { content_type.label_field_name } |
| + | it { is_expected.to eq :title } |
| + | |
| + | context 'defined within the content type itself' do |
| + | |
| + | before { allow(content_type.attributes).to receive(:[]).with(:label_field_name).and_return('author') } |
| + | it { is_expected.to eq :author } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | # describe '#localized_fields_names' do |
| + | |
| + | # subject { content_type.localized_fields_names } |
| + | # it { is_expected.to eq [:author] } |
| + | |
| + | # end |
| + | |
| + | describe '#order_by' do |
| + | |
| + | subject { content_type.order_by } |
| + | it { is_expected.to eq ['_position', 'asc'] } |
| + | |
| + | context 'specifying manually' do |
| + | |
| + | before do |
| + | content_type.attributes[:order_by] = 'manually' |
| + | content_type.attributes[:order_direction] = 'desc' |
| + | end |
| + | it { is_expected.to eq ['_position', 'desc'] } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
spec/unit/repositories/content_type_repository_spec.rb
+126
-0
| @@ | @@ -0,0 +1,126 @@ |
| + | require 'spec_helper' |
| + | |
| + | require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| + | |
| + | describe Locomotive::Steam::ContentTypeRepository do |
| + | |
| + | let(:fields) { [{ name: :title, hint: 'Title of the article', type: 'string' }, { name: :author, type: 'string', label: 'Fullname of the author' }] } |
| + | let(:types) { [{ slug: 'articles', name: 'Articles', entries_custom_fields: fields }] } |
| + | let(:locale) { :en } |
| + | let(:site) { instance_double('Site', _id: 1, default_locale: :en, locales: %i(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(types) |
| + | adapter.cache = NoCacheStore.new |
| + | end |
| + | |
| + | describe '#all' do |
| + | |
| + | subject { repository.all } |
| + | |
| + | it { expect(subject.size).to eq 1 } |
| + | |
| + | describe 'first element' do |
| + | |
| + | subject { repository.all.first } |
| + | |
| + | it { expect(subject.class).to eq Locomotive::Steam::ContentType } |
| + | it { expect(subject.fields.all.size).to eq 2 } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | describe '#by_slug' do |
| + | |
| + | let(:slug) { nil } |
| + | subject { repository.by_slug(slug) } |
| + | |
| + | it { is_expected.to eq nil } |
| + | |
| + | context 'existing content type' do |
| + | |
| + | let(:slug) { 'articles' } |
| + | it { expect(subject.name).to eq 'Articles' } |
| + | |
| + | end |
| + | |
| + | context 'slug is already a content type' do |
| + | |
| + | let(:slug) { instance_double('ContentType') } |
| + | it { is_expected.to eq slug } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | describe '#fields_for' do |
| + | |
| + | let(:type) { nil } |
| + | subject { repository.fields_for(type) } |
| + | |
| + | it { is_expected.to eq nil } |
| + | |
| + | context 'with fields' do |
| + | |
| + | let(:type) { instance_double('ContentType', fields: [true]) } |
| + | it { is_expected.to eq([true]) } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | describe '#look_for_unique_fields' do |
| + | |
| + | let(:type) { nil } |
| + | subject { repository.look_for_unique_fields(type) } |
| + | |
| + | it { is_expected.to eq nil } |
| + | |
| + | context 'with fields' do |
| + | |
| + | let(:field) { instance_double('Field', name: :title) } |
| + | let(:target) { instance_double('ContentTypeFieldRepository', unique: { title: field }) } |
| + | let(:type) { instance_double('ContentType', fields: target) } |
| + | |
| + | it { expect(subject).to eq(title: field) } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | describe '#select_options' do |
| + | |
| + | let(:type) { repository.by_slug('articles') } |
| + | let(:name) { nil } |
| + | |
| + | subject { repository.select_options(type, name) } |
| + | |
| + | it { is_expected.to eq nil } |
| + | |
| + | context 'a select field' do |
| + | |
| + | let(:fields) do |
| + | [ |
| + | { name: 'title', hint: 'Title of the article', type: 'string' }, |
| + | { name: 'category', type: 'select', select_options: [{ name: { en: 'cooking', fr: 'cuisine' }, position: 0 }, { name: { en: 'bread', fr: 'pain' }, position: 1 }] } |
| + | ] |
| + | end |
| + | let(:name) { :category } |
| + | |
| + | it { expect(subject.map { |o| o.name[:en] }).to eq %w(cooking bread) } |
| + | |
| + | context 'not a select field' do |
| + | |
| + | let(:name) { :title } |
| + | it { is_expected.to eq nil } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
spec/unit/repositories/filesystem/theme_asset_spec.rb
+0
-25
| @@ | @@ -1,25 +0,0 @@ |
| - | # require 'spec_helper' |
| - | |
| - | # describe Locomotive::Steam::Repositories::Filesystem::ThemeAsset do |
| - | |
| - | # let(:site) { instance_double('Site', default_locale: :en, locales: [:en, :fr]) } |
| - | # let(:repository) { Locomotive::Steam::Repositories::Filesystem::ThemeAsset.new(site) } |
| - | |
| - | # describe '#url_for' do |
| - | |
| - | # let(:path) { 'main.css' } |
| - | # subject { repository.url_for(path) } |
| - | |
| - | # it { is_expected.to eq '/main.css' } |
| - | |
| - | # end |
| - | |
| - | # describe '#checksums' do |
| - | |
| - | # subject { repository.checksums } |
| - | |
| - | # it { is_expected.to eq({}) } |
| - | |
| - | # end |
| - | |
| - | # end |
spec/unit/repositories/page_repository_spec.rb
+1
-1
| @@ | @@ -1,7 +1,7 @@ |
| require 'spec_helper' | |
| require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' | |
| - | require_relative '../../../lib/locomotive/steam/repositories/editable_element_repository.rb' |
| + | # require_relative '../../../lib/locomotive/steam/repositories/editable_element_repository.rb' |
| describe Locomotive::Steam::PageRepository do | |