Fixed previous next feature for content entries (issue #61 / 2nd attempt)

did committed Mar 01, 2016
commit 1ff34aacada6292425496488c2266b1a2e59ad40
Showing 2 changed files with 50 additions and 41 deletions
locomotive/steam/repositories/content_entry_repository.rb b/lib/locomotive/steam/repositories/content_entry_repository.rb +1 -1
@@ @@ -171,7 +171,7 @@ module Locomotive
conditions = prepare_conditions({ k(name, op) => i18n_value_of(entry, name) })
- first do
+ public_send(asc_op == 'gt' ? :first : :last) do
where(conditions).order_by(order_by)
end
end
spec/unit/repositories/content_entry_repository_spec.rb +49 -40
@@ @@ -123,10 +123,11 @@ describe Locomotive::Steam::ContentEntryRepository do
end
- describe '#next' do
+ describe '#next or #previous' do
let(:direction) { 'asc' }
- let(:type) { build_content_type('Articles', order_by: { _position: direction }, label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }, fields_with_default: []) }
+ let(:type) { build_content_type('Articles', order_by: { _position: direction }, label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }, fields_with_default: []) }
+ let(:entry) { nil }
let(:entries) do
[
{ content_type_id: 1, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' },
@@ @@ -136,69 +137,77 @@ describe Locomotive::Steam::ContentEntryRepository do
]
end
- let(:entry) { nil }
- subject { repository.next(entry) }
+ describe '#next' do
- it { is_expected.to eq nil }
-
- context 'being last' do
+ subject { repository.next(entry) }
- let(:entry) { instance_double('Entry', content_type: type, _position: 3) }
it { is_expected.to eq nil }
- end
+ context 'being last' do
- context 'being middle' do
+ let(:entry) { instance_double('Entry', content_type: type, _position: 3) }
+ it { is_expected.to eq nil }
- let(:entry) { instance_double('Entry', content_type: type, _position: 0) }
- it { expect(subject._position).to eq 1 }
+ end
- describe 'another example' do
+ context 'being middle' do
- let(:entry) { instance_double('Entry', content_type: type, _position: 1) }
- it { expect(subject._position).to eq 2 }
+ let(:entry) { instance_double('Entry', content_type: type, _position: 0) }
+ it { expect(subject._position).to eq 1 }
- end
+ describe 'another example' do
- context 'changing direction' do
+ let(:entry) { instance_double('Entry', content_type: type, _position: 1) }
+ it { expect(subject._position).to eq 2 }
- let(:direction) { 'desc' }
- let(:entry) { instance_double('Entry', content_type: type, _position: 2) }
- it { expect(subject._position).to eq 1 }
+ end
+
+ context 'changing direction' do
+
+ let(:direction) { 'desc' }
+ let(:entry) { instance_double('Entry', content_type: type, _position: 2) }
+ it { expect(subject._position).to eq 1 }
+
+ end
end
end
- end
+ describe '#previous' do
- describe '#previous' do
+ subject { repository.previous(entry) }
- let(:type) { build_content_type('Articles', order_by: { _position: 'asc' }, label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }, fields_with_default: []) }
- let(:entries) do
- [
- { content_type_id: 1, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' },
- { content_type_id: 1, _position: 1, _label: 'Update #2', title: { fr: 'Mise a jour #2' }, text: { en: 'bla bla', fr: 'blabbla' }, date: '2009/05/12', category: 'General' },
- { content_type_id: 1, _position: 2, _label: 'Update #3', title: { fr: 'Mise a jour #2' }, text: { en: 'bla bla', fr: 'blabbla' }, date: '2009/05/12', category: 'General' }
- ]
- end
+ it { is_expected.to eq nil }
- let(:entry) { nil }
- subject { repository.previous(entry) }
+ context 'being first' do
- it { is_expected.to eq nil }
+ let(:entry) { instance_double('Entry', content_type: type, _position: 0) }
+ it { is_expected.to eq nil }
- context 'being first' do
+ end
- let(:entry) { instance_double('Entry', content_type: type, _position: 0) }
- it { is_expected.to eq nil }
+ context 'being middle' do
- end
+ let(:entry) { instance_double('Entry', content_type: type, _position: 1) }
+ it { expect(subject._position).to eq 0 }
+
+ describe 'another example' do
+
+ let(:entry) { instance_double('Entry', content_type: type, _position: 2) }
+ it { expect(subject._position).to eq 1 }
+
+ end
- context 'being middle' do
+ context 'changing direction' do
- let(:entry) { instance_double('Entry', content_type: type, _position: 1) }
- it { expect(subject._position).to eq 0 }
+ let(:direction) { 'desc' }
+ let(:entry) { instance_double('Entry', content_type: type, _position: 2) }
+ it { expect(subject._position).to eq 3 }
+
+ end
+
+ end
end