patch for the next/previous content entry functionality (PR #61)

did committed Mar 01, 2016
commit 774c24e65fa4fc56cbc526dfece8867cbe5adfd6
Showing 2 changed files with 23 additions and 6 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) })
- public_send(op == 'gt' ? :last : :first) do
+ first do
where(conditions).order_by(order_by)
end
end
spec/unit/repositories/content_entry_repository_spec.rb +22 -5
@@ @@ -125,12 +125,14 @@ describe Locomotive::Steam::ContentEntryRepository do
describe '#next' do
- 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(: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(: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' }
+ { content_type_id: 1, _position: 2, _label: 'Update #3', title: { fr: 'Mise a jour #3' }, text: { en: 'bla bla', fr: 'blabbla' }, date: '2009/05/12', category: 'General' },
+ { content_type_id: 1, _position: 3, _label: 'Update #4', title: { fr: 'Mise a jour #4' }, text: { en: 'bla bla', fr: 'blabbla' }, date: '2009/05/12', category: 'General' }
]
end
@@ @@ -141,15 +143,30 @@ describe Locomotive::Steam::ContentEntryRepository do
context 'being last' do
- let(:entry) { instance_double('Entry', content_type: type, _position: 2) }
+ let(:entry) { instance_double('Entry', content_type: type, _position: 3) }
it { is_expected.to eq nil }
end
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 }
+
+ describe 'another example' do
+
+ let(:entry) { instance_double('Entry', content_type: type, _position: 1) }
+ it { expect(subject._position).to eq 2 }
+
+ 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