add more integration specs for the content_entry_repository + fixing the group_by_select_option method [WIP]

did committed Mar 06, 2015
commit 94a46264eb7511a1329a548912b7f1816da3c194
Showing 10 changed files with 111 additions and 77 deletions
locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb +13 -6
@@ @@ -20,25 +20,32 @@ module Locomotive
each(content_type_slug) do |label, attributes, position|
_attributes = { _position: position, _label: label.to_s }.merge(attributes)
- setup_associations(_attributes)
+ modify_for_selects(_attributes)
+ modify_for_associations(_attributes)
list << _attributes
end
end
end
- def setup_associations(attributes)
+ def modify_for_selects(attributes)
+ content_type.selects.each do |field|
+ attributes[:"#{field.name}_id"] = attributes.delete(field.name.to_sym)
+ end
+ end
+
+ def modify_for_associations(attributes)
content_type.associations.each do |field|
case field.type
when :belongs_to
- setup_belongs_to_association(field, attributes)
+ modify_belongs_to_association(field, attributes)
when :many_to_many
- setup_many_to_many_association(field, attributes)
+ modify_many_to_many_association(field, attributes)
end
end
end
- def setup_belongs_to_association(field, attributes)
+ def modify_belongs_to_association(field, attributes)
# <name>_id
attributes[:"#{field.name}_id"] = attributes.delete(field.name.to_sym)
@@ @@ -46,7 +53,7 @@ module Locomotive
attributes[:"_position_in_#{field.name}"] = attributes[:_position]
end
- def setup_many_to_many_association(field, attributes)
+ def modify_many_to_many_association(field, attributes)
attributes[:"#{field.name.to_s.singularize}_ids"] = attributes.delete(field.name.to_sym)
end
locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb +2 -2
@@ @@ -59,7 +59,7 @@ module Locomotive
options.each do |locale, values|
values.each_with_index do |name, position|
if (option = list.at(position)).nil?
- list << { name: { locale => name }, position: position }
+ list << { _id: name, name: { locale => name }, position: position }
else
option[name][locale] = name
end
@@ @@ -71,7 +71,7 @@ module Locomotive
def build_select_options_from_array(options)
[].tap do |list|
options.each_with_index do |name, position|
- list << { name: name, position: position }
+ list << { _id: name, name: name, position: position }
end
end
end
locomotive/steam/adapters/mongodb.rb b/lib/locomotive/steam/adapters/mongodb.rb +4 -0
@@ @@ -22,6 +22,10 @@ module Locomotive::Steam
name.__send__(operator)
end
+ def identifier_name(mapper)
+ :_id
+ end
+
def theme_assets_base_url(scope)
['', 'sites', scope.site._id.to_s, 'theme'].join('/')
end
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb +19 -13
@@ @@ -80,40 +80,46 @@ module Locomotive::Steam
def _cast_value(field)
if private_methods.include?(:"_cast_#{field.type}")
- send(:"_cast_#{field.type}", field.name)
+ send(:"_cast_#{field.type}", field)
else
attributes[field.name]
end
end
- def _cast_integer(name)
- _cast_convertor(name, &:to_i)
+ def _cast_integer(field)
+ _cast_convertor(field.name, &:to_i)
end
- def _cast_float(name)
- _cast_convertor(name, &:to_f)
+ def _cast_float(field)
+ _cast_convertor(field.name, &:to_f)
end
- def _cast_file(name)
- _cast_convertor(name) do |value|
+ def _cast_file(field)
+ _cast_convertor(field.name) do |value|
value.present? ? { 'url' => value } : nil
end
end
- def _cast_date(name)
- _cast_time(name, :to_date)
+ def _cast_date(field)
+ _cast_time(field.name, :to_date)
end
- def _cast_date_time(name)
- _cast_time(name, :to_date)
+ def _cast_date_time(field)
+ _cast_time(field.name, :to_date)
end
- def _cast_time(name, end_method)
- _cast_convertor(name) do |value|
+ def _cast_time(field, end_method)
+ _cast_convertor(field.name) do |value|
value.is_a?(String) ? Chronic.parse(value).send(end_method) : value
end
end
+ def _cast_select(field)
+ _cast_convertor(:"#{field.name}_id") do |value|
+ field.select_options.find(value).name
+ end
+ end
+
def _cast_convertor(name, &block)
if (value = attributes[name]).respond_to?(:translations)
value.each { |l, _value| value[l] = yield(_value) }
locomotive/steam/entities/content_type.rb b/lib/locomotive/steam/entities/content_type.rb +2 -2
@@ @@ -5,7 +5,7 @@ module Locomotive::Steam
include Locomotive::Steam::Models::Entity
extend Forwardable
- def_delegators :fields, :localized_names, :associations
+ def_delegators :fields, :localized_names, :associations, :selects
def initialize(attributes = {})
super({
@@ @@ -23,7 +23,7 @@ module Locomotive::Steam
@fields_by_name ||= (fields.all.inject({}) do |memo, field|
memo[field.name] = field
memo
- end)
+ end).with_indifferent_access
end
def label_field_name
locomotive/steam/repositories/content_entry_repository.rb b/lib/locomotive/steam/repositories/content_entry_repository.rb +2 -2
@@ @@ -72,7 +72,7 @@ module Locomotive
_groups = all.group_by { |entry| i18n_value_of(entry, name) }
groups = content_type_repository.select_options(content_type, name).map do |option|
- { name: option, entries: _groups.delete(option) || [] }
+ { name: i18n_value_of(option, :name), entries: _groups.delete(option) || [] }
end
unless _groups.blank?
@@ @@ -121,7 +121,7 @@ module Locomotive
with(entry.content_type)
- name, direction = self.content_type.order_by.split
+ name, direction = self.content_type.order_by.first
op = direction == 'asc' ? asc_op : desc_op
conditions = prepare_conditions({ k(name, op) => i18n_value_of(entry, name) })
locomotive/steam/repositories/content_type_field_repository.rb b/lib/locomotive/steam/repositories/content_type_field_repository.rb +4 -0
@@ @@ -14,6 +14,10 @@ module Locomotive
embedded_association :select_options, ContentTypeFieldSelectOptionRepository
end
+ def selects
+ query { where(type: :select) }.all
+ end
+
def associations
query { where(k(:type, :in) => %i(belongs_to has_many many_to_many)) }.all
end
spec/integration/repositories/content_entry_repository_spec.rb +44 -15
@@ @@ -7,31 +7,59 @@ describe Locomotive::Steam::ContentEntryRepository 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) }
-
+ let(:site) { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) }
+ let(:locale) { :en }
let(:type_repository) { Locomotive::Steam::ContentTypeRepository.new(adapter, site, locale) }
- let(:type) { type_repository.by_slug('bands') }
+ let(:repository) { described_class.new(adapter, site, locale, type_repository).with(type) }
+ let(:type) { type_repository.by_slug('bands') }
- describe '#all' do
- subject { repository.with(type).all }
- it { expect(subject.size).to eq 3 }
- end
+ # describe '#all' do
+ # subject { repository.all }
+ # it { expect(subject.size).to eq 3 }
+ # end
# describe '#by_slug' do
- # subject { repository.by_slug('bands') }
- # it { expect(subject.description).to eq 'List of bands' }
+ # subject { repository.by_slug('alice-in-chains') }
+ # it { expect(subject.name).to eq 'Alice in Chains' }
+ # end
+
+ # describe '#exists?' do
+ # subject { repository.exists?(featured: true) }
+ # it { is_expected.to eq true }
+ # end
+
+ # describe '#find' do
+ # subject { repository.find(entry_id) }
+ # it { expect(subject.name).to eq 'Pearl Jam' }
# end
+ # describe '#next' do
+ # let(:entry) { repository.find(entry_id) }
+ # subject { repository.next(entry) }
+ # it { expect(subject.name).to eq 'The who' }
+ # end
+
+ # describe '#previous' do
+ # let(:entry) { repository.find(entry_id) }
+ # subject { repository.previous(entry) }
+ # it { expect(subject.name).to eq 'Alice in Chains' }
+ # end
+
+ describe '#group_by_select_option' do
+ subject { repository.group_by_select_option(:kind) }
+ it { expect(subject.map { |h| h[:name] }).to eq(%w(grunge rock country) << nil) }
+ it { expect(subject.map { |h| h[:entries].size }).to eq([2, 1, 0, 0]) }
+ 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']) }
+ let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') }
+ let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) }
+ let(:entry_id) { BSON::ObjectId.from_string('54eb4bbc2475804b2b00003f') }
end
@@ @@ -41,8 +69,9 @@ describe Locomotive::Steam::ContentEntryRepository do
it_should_behave_like 'a repository' do
- let(:site_id) { 1 }
- let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) }
+ let(:site_id) { 1 }
+ let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) }
+ let(:entry_id) { 'pearl-jam' }
after(:all) { Locomotive::Steam::Adapters::Filesystem::SimpleCacheStore.new.clear }
spec/unit/adapters/filesystem/yaml_loaders/content_entry_spec.rb +14 -2
@@ @@ -6,7 +6,7 @@ require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_l
describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::ContentEntry do
let(:site_path) { default_fixture_site_path }
- let(:content_type) { instance_double('Bands', _id: 42, slug: 'bands', associations: []) }
+ let(:content_type) { instance_double('Bands', _id: 42, slug: 'bands', associations: [], selects: []) }
let(:scope) { instance_double('Scope', locale: :en, context: { content_type: content_type }) }
let(:loader) { described_class.new(site_path) }
@@ @@ -23,7 +23,7 @@ describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::ContentEntry do
context 'a content type with a belongs_to field' do
let(:field) { instance_double('Field', name: 'band', type: :belongs_to) }
- let(:content_type) { instance_double('Songs', slug: 'songs', associations: [field]) }
+ let(:content_type) { instance_double('Songs', slug: 'songs', associations: [field], selects: []) }
it 'adds a new attribute for the foreign key' do
expect(subject.first[:band_id]).to eq 'pearl-jam'
@@ @@ -33,6 +33,18 @@ describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::ContentEntry do
end
+ context 'a content type with a select field' do
+
+ let(:field) { instance_double('Field', name: 'kind', type: :select) }
+ let(:content_type) { instance_double('Bands', slug: 'bands', selects: [field], associations: []) }
+
+ it 'adds a new attribute for the foreign key' do
+ expect(subject.first[:kind_id]).to eq 'grunge'
+ expect(subject.first[:kind]).to eq nil
+ end
+
+ end
+
end
end
spec/unit/entities/content_entry_spec.rb +7 -35
@@ @@ -55,15 +55,6 @@ describe Locomotive::Steam::ContentEntry do
end
- # describe '#localized_attributes' do
-
- # subject { content_entry.localized_attributes }
- # it { is_expected.to include :seo_title }
- # it { is_expected.to include :title }
- # it { is_expected.to include :_slug }
-
- # end
-
describe 'dynamic attributes' do
let(:field_type) { :string }
@@ @@ -150,32 +141,13 @@ describe Locomotive::Steam::ContentEntry do
end
end
- # context 'a belongs_to relationship' do
- # let(:field_type) { :belongs_to }
- # let(:value) { 'john-doe' }
- # it { expect(subject.type).to eq :belongs_to }
- # it { expect(subject.target_slugs).to eq ['john-doe'] }
- # it { expect(subject.source).to eq content_entry }
- # it { expect(subject.field).to eq field }
- # end
-
- # context 'a has_many relationship' do
- # let(:field_type) { :has_many }
- # let(:value) { nil }
- # it { expect(subject.type).to eq :has_many }
- # it { expect(subject.target_slugs).to eq [] }
- # it { expect(subject.source).to eq content_entry }
- # it { expect(subject.field).to eq field }
- # end
-
- # context 'a many_to_many relationship' do
- # let(:field_type) { :many_to_many }
- # let(:value) { ['john-doe', 'jane-doe'] }
- # it { expect(subject.type).to eq :many_to_many }
- # it { expect(subject.target_slugs).to eq ['john-doe', 'jane-doe'] }
- # it { expect(subject.source).to eq content_entry }
- # it { expect(subject.field).to eq field }
- # end
+ context 'a select' do
+ let(:option) { instance_double('SelectOption', name: { en: 'Category #1', fr: 'Categorie #1' }) }
+ let(:field) { instance_double('Field', name: :my_field, type: :select, select_options: instance_double('SelectOptions')) }
+ let(:value) { 42 }
+ before { expect(field.select_options).to receive(:find).with(42).and_return(option) }
+ it { is_expected.to eq(option) }
+ end
end