Integrated next and previous methods for content in content types

This commit is contained in:
Karl Brightman 2011-09-16 11:35:40 +08:00
parent 13b82e66fe
commit 75f4835d3e
3 changed files with 45 additions and 0 deletions

View File

@ -50,6 +50,14 @@ class ContentInstance
def visible?
self._visible || self._visible.nil?
end
def next
content_type.contents.where(_position_in_list: _position_in_list + 1).first()
end
def previous
content_type.contents.where(_position_in_list: _position_in_list - 1).first()
end
def errors_to_hash
Hash.new.replace(self.errors)

View File

@ -8,6 +8,14 @@ module Locomotive
def _id
self._source._id.to_s
end
def next
self._source.next.to_liquid
end
def previous
self._source.previous.to_liquid
end
def before_method(meth)
return '' if self._source.nil?

View File

@ -41,6 +41,35 @@ describe ContentInstance do
end
end
describe "#navigation" do
before(:each) do
%w(first second third).each_with_index do |item,index|
content = build_content({:title => item.to_s})
content._position_in_list = index
instance_variable_set "@#{item}", content
end
end
it 'should find previous item when available' do
puts @second.previous
@second.previous.custom_field_1.should == "first"
@second.previous._position_in_list.should == 0
end
it 'should find next item when available' do
@second.next.custom_field_1.should == "third"
@second.next._position_in_list.should == 2
end
it 'should return nil when fetching previous item on first in list' do
@first.previous.should == nil
end
it 'should return nil when fetching next item on last in list' do
@third.next.should == nil
end
end
describe '#permalink' do