fix the group_by_select_options method of the content_entry_repository + fix issues when casting a date/time/select attribute of a content entry

did committed Mar 06, 2015
commit 58102879c26608703a6b66f9dbb2d810775de0df
Showing 7 changed files with 76 additions and 53 deletions
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb +3 -3
@@ @@ -101,11 +101,11 @@ module Locomotive::Steam
end
def _cast_date(field)
- _cast_time(field.name, :to_date)
+ _cast_time(field, :to_date)
end
def _cast_date_time(field)
- _cast_time(field.name, :to_date)
+ _cast_time(field, :to_date)
end
def _cast_time(field, end_method)
@@ @@ -116,7 +116,7 @@ module Locomotive::Steam
def _cast_select(field)
_cast_convertor(:"#{field.name}_id") do |value|
- field.select_options.find(value).name
+ field.select_options.find(value).try(:name)
end
end
locomotive/steam/entities/content_type_field.rb b/lib/locomotive/steam/entities/content_type_field.rb +4 -0
@@ @@ -58,6 +58,10 @@ module Locomotive::Steam
attr_accessor :field
+ def name
+ self[:name]
+ end
+
end
end
locomotive/steam/repositories/content_entry_repository.rb b/lib/locomotive/steam/repositories/content_entry_repository.rb +13 -9
@@ @@ -69,18 +69,15 @@ module Locomotive
def group_by_select_option(name)
return {} if name.nil? || content_type.nil? || content_type.fields_by_name[name].type != :select
+ # a big one request to get them grouped by the field
_groups = all.group_by { |entry| i18n_value_of(entry, name) }
- groups = content_type_repository.select_options(content_type, name).map do |option|
- { name: i18n_value_of(option, :name), entries: _groups.delete(option) || [] }
+ groups_to_array(name, _groups).tap do |groups|
+ # entries with a not existing select_option value?
+ unless _groups.blank?
+ groups << { name: nil, entries: _groups.values.flatten }
+ end
end
-
- unless _groups.blank?
- groups << (empty = { name: nil, entries: [] })
- _groups.values.each { |list| empty[:entries] += list }
- end
-
- groups
end
private
@@ @@ -129,6 +126,13 @@ module Locomotive
first { where(conditions) }
end
+ def groups_to_array(name, groups)
+ content_type_repository.select_options(content_type, name).map do |option|
+ option_name = i18n_value_of(option, :name)
+ { name: option_name, entries: groups.delete(option_name) || [] }
+ end
+ end
+
end
end
spec/integration/repositories/content_entry_repository_spec.rb +33 -33
@@ @@ -13,42 +13,42 @@ describe Locomotive::Steam::ContentEntryRepository do
let(:repository) { described_class.new(adapter, site, locale, type_repository).with(type) }
let(:type) { type_repository.by_slug('bands') }
- # describe '#all' do
- # subject { repository.all }
- # it { expect(subject.size).to eq 3 }
- # end
-
- # describe '#by_slug' do
- # 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 '#all' do
+ subject { repository.all }
+ it { expect(subject.size).to eq 3 }
+ end
+
+ describe '#by_slug' do
+ 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]) }
+ it { expect(subject.map { |h| h[:name] }).to eq(%w(grunge rock country)) }
+ it { expect(subject.map { |h| h[:entries].size }).to eq([2, 1, 0]) }
end
end
spec/unit/entities/content_entry_spec.rb +2 -2
@@ @@ -144,9 +144,9 @@ describe Locomotive::Steam::ContentEntry do
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 }
+ let(:attributes) { { my_field_id: 42 } }
before { expect(field.select_options).to receive(:find).with(42).and_return(option) }
- it { is_expected.to eq(option) }
+ it { is_expected.to eq({ en: 'Category #1', fr: 'Categorie #1' }) }
end
end
spec/unit/entities/content_type_spec.rb +2 -1
@@ @@ -25,7 +25,8 @@ describe Locomotive::Steam::ContentType do
describe '#fields_by_name' do
subject { content_type.fields_by_name }
- it { expect(subject.keys).to eq [:title, :author] }
+ it { expect(subject.keys).to eq ['title', 'author'] }
+ it { expect(subject[:title]).to eq(subject['title']) }
end
spec/unit/repositories/content_entry_repository_spec.rb +19 -5
@@ @@ -171,16 +171,30 @@ describe Locomotive::Steam::ContentEntryRepository do
let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_names: [:title, :category], fields_by_name: fields) }
let(:name) { :category }
+ let(:options) {
+ [
+ instance_double('SelectOption1', name: 'cooking'),
+ instance_double('SelectOption2', name: 'wine'),
+ instance_double('SelectOption3', name: 'bread')
+ ]
+ }
+
let(:entries) do
[
- { content_type_id: 1, _position: 0, _label: 'Recipe #1', category: 'cooking' },
- { content_type_id: 1, _position: 1, _label: 'Recipe #2', category: 'bread' },
- { content_type_id: 1, _position: 2, _label: 'Recipe #3', category: 'bread' },
- { content_type_id: 1, _position: 3, _label: 'Recipe #4', category: 'unknown' }
+ { content_type_id: 1, _position: 0, _label: 'Recipe #1', category_id: 'cooking' },
+ { content_type_id: 1, _position: 1, _label: 'Recipe #2', category_id: 'bread' },
+ { content_type_id: 1, _position: 2, _label: 'Recipe #3', category_id: 'bread' },
+ { content_type_id: 1, _position: 3, _label: 'Recipe #4', category_id: 'unknown' }
]
end
- before { allow(content_type_repository).to receive(:select_options).and_return(%w(cooking wine bread)) }
+ before {
+ allow(content_type_repository).to receive(:select_options).and_return(options)
+ %w(cooking wine bread).each_with_index do |name, i|
+ allow(fields[:category].select_options).to receive(:find).with(name).and_return(options.at(i))
+ end
+ allow(fields[:category].select_options).to receive(:find).with('unknown').and_return(nil)
+ }
it { expect(subject.size).to eq 4 }
it { expect(subject.map { |h| h[:name] }).to eq ['cooking', 'wine', 'bread', nil] }