fixed the last failed specs (hooray)

did committed Mar 14, 2015
commit ea64f059a49fc66e213a8c2c9638cc0a7c231d4c
Showing 5 changed files with 43 additions and 29 deletions
locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb +5 -7
@@ @@ -36,7 +36,9 @@ module Locomotive::Steam
def create(mapper, scope, entity)
sanitizers[mapper.name].with(scope) do |sanitizer|
- insert_to_dataset(entity, memoized_dataset(mapper, scope), sanitizer)
+ dataset = memoized_dataset(mapper, scope)
+ dataset.insert(entity)
+ sanitizer.apply_to_entity_with_dataset(entity, dataset)
end
end
@@ @@ -75,18 +77,14 @@ module Locomotive::Steam
collection(mapper, scope).each do |attributes|
entity = mapper.to_entity(attributes.dup)
- insert_to_dataset(entity, dataset, sanitizer)
+ dataset.insert(entity)
+ sanitizer.apply_to(entity)
end
sanitizer.apply_to(dataset)
end
end
- def insert_to_dataset(entity, dataset, sanitizer)
- dataset.insert(entity)
- sanitizer.apply_to(entity)
- end
-
def collection(mapper, scope)
yaml_loaders[mapper.name].load(scope)
end
locomotive/steam/adapters/filesystem/sanitizer.rb b/lib/locomotive/steam/adapters/filesystem/sanitizer.rb +4 -0
@@ @@ -37,6 +37,10 @@ module Locomotive::Steam
entity
end
+ def apply_to_entity_with_dataset(entity, dataset)
+ entity
+ end
+
def attach_site_to(entity)
entity[:site_id] = scope.site._id if scope.site
end
locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb +30 -18
@@ @@ -13,41 +13,53 @@ module Locomotive::Steam
end
def apply_to_dataset(dataset)
- dataset.all.each do |entry|
- set_slug(entry, dataset)
- set_id(entry)
+ dataset.all.each do |entity|
+ _apply_to_dataset(entity, dataset)
end
end
+ def apply_to_entity_with_dataset(entity, dataset)
+ # Note: this statement attaches the site to the entity
+ apply_to_entity(entity)
+
+ # make sure it gets an unique slug and an _id
+ _apply_to_dataset(entity, dataset)
+ end
+
private
- def add_label(entry)
- value = entry.attributes.delete(:_label)
- name = entry.content_type.label_field_name
+ def _apply_to_dataset(entity, dataset)
+ set_slug(entity, dataset)
+ set_id(entity)
+ end
+
+ def add_label(entity)
+ value = entity.attributes.delete(:_label)
+ name = entity.content_type.label_field_name
- if entry.attributes[name].respond_to?(:translations) # localized?
- entry.attributes[name][default_locale] = value
+ if entity.attributes[name].respond_to?(:translations) # localized?
+ entity.attributes[name][default_locale] = value
else
- entry.attributes[name] ||= value
+ entity.attributes[name] ||= value
end
end
- def set_id(entry)
- if (slug = entry[:_slug]).respond_to?(:translations)
- entry[:_id] = slug[locale]
+ def set_id(entity)
+ if (slug = entity[:_slug]).respond_to?(:translations)
+ entity[:_id] = slug[locale]
else
- entry[:_id] = slug
+ entity[:_id] = slug
end
end
- def set_slug(entry, dataset)
- if entry._label.respond_to?(:translations) # localized?
- entry._label.each do |locale, label|
- entry[:_slug][locale] ||= slugify(entry._id, label, dataset, locale)
+ def set_slug(entity, dataset)
+ if entity._label.respond_to?(:translations) # localized?
+ entity._label.each do |locale, label|
+ entity[:_slug][locale] ||= slugify(entity._id, label, dataset, locale)
end
else
# Note: replace the translations of the I18nField by a string
- entry[:_slug].translations = slugify(entry._id, entry._label, dataset)
+ entity[:_slug].translations = slugify(entity._id, entity._label, dataset)
end
end
locomotive/steam/services/entry_submission_service.rb b/lib/locomotive/steam/services/entry_submission_service.rb +2 -2
@@ @@ -26,7 +26,7 @@ module Locomotive
return nil if type.nil?
- i18n_decorate { repository.by_slug(type, slug) }
+ i18n_decorate { repository.with(type).by_slug(slug) }
end
def to_json(entry)
@@ @@ -36,7 +36,7 @@ module Locomotive
hash = { _slug: entry._slug, content_type_slug: entry.content_type_slug }
# dynamic attributes
- content_type_repository.fields_for(entry.content_type).each do |field|
+ entry.content_type.fields.all.each do |field|
next if %w(belongs_to has_many many_to_many).include?(field.type.to_s)
hash[field.name] = entry.send(field.name)
spec/unit/services/entry_submission_service_spec.rb +2 -2
@@ @@ -30,7 +30,7 @@ describe Locomotive::Steam::EntrySubmissionService do
before do
allow(type_repository).to receive(:by_slug).and_return(type)
- allow(entry_repository).to receive(:by_slug).with(type, 'hello-world').and_return(entry)
+ allow(entry_repository).to receive(:by_slug).with('hello-world').and_return(entry)
end
it { is_expected.to eq entry }
@@ @@ -53,7 +53,7 @@ describe Locomotive::Steam::EntrySubmissionService do
let(:type) { instance_double('Articles') }
let(:entry) { instance_double('DecoratedEntry', _slug: 'hello-world', title: 'Hello world', content_type: type, content_type_slug: :articles, errors: errors) }
- before { allow(type_repository).to receive(:fields_for).with(type).and_return(fields) }
+ before { allow(type).to receive(:fields).and_return(instance_double('FieldRepository', all: fields)) }
it { is_expected.to eq '{"_slug":"hello-world","content_type_slug":"articles","title":"Hello world"}' }