From 75f4835d3e5d74fb7d4c5dc7af154e9c36f0522e Mon Sep 17 00:00:00 2001 From: Karl Brightman Date: Fri, 16 Sep 2011 11:35:40 +0800 Subject: [PATCH 1/3] Integrated next and previous methods for content in content types --- app/models/content_instance.rb | 8 +++++++ lib/locomotive/liquid/drops/content.rb | 8 +++++++ spec/models/content_instance_spec.rb | 29 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/app/models/content_instance.rb b/app/models/content_instance.rb index 0322ff86..7f4a289e 100644 --- a/app/models/content_instance.rb +++ b/app/models/content_instance.rb @@ -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) diff --git a/lib/locomotive/liquid/drops/content.rb b/lib/locomotive/liquid/drops/content.rb index 1aefc759..118d4380 100644 --- a/lib/locomotive/liquid/drops/content.rb +++ b/lib/locomotive/liquid/drops/content.rb @@ -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? diff --git a/spec/models/content_instance_spec.rb b/spec/models/content_instance_spec.rb index f34e2957..ef49fcd5 100644 --- a/spec/models/content_instance_spec.rb +++ b/spec/models/content_instance_spec.rb @@ -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 From 769b0569dbceddafa0028d84f2a6a55b2761b2b9 Mon Sep 17 00:00:00 2001 From: Karl Brightman Date: Fri, 16 Sep 2011 11:37:48 +0800 Subject: [PATCH 2/3] Added examples --- lib/locomotive/liquid/drops/content.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/locomotive/liquid/drops/content.rb b/lib/locomotive/liquid/drops/content.rb index 118d4380..2e3fcb37 100644 --- a/lib/locomotive/liquid/drops/content.rb +++ b/lib/locomotive/liquid/drops/content.rb @@ -9,10 +9,28 @@ module Locomotive self._source._id.to_s end + # Returns the next content for the parent content type. + # If no content is found, nil is returned. + # + # Usage: + # + # {% if article.next %} + # Read next article + # {% endif %} + # def next self._source.next.to_liquid end + # Returns the previous content for the parent content type. + # If no content is found, nil is returned. + # + # Usage: + # + # {% if article.previous %} + # Read previous article + # {% endif %} + # def previous self._source.previous.to_liquid end From 93667018c1b36126bf238b6fd4fde31550203e38 Mon Sep 17 00:00:00 2001 From: Karl Brightman Date: Fri, 16 Sep 2011 11:55:30 +0800 Subject: [PATCH 3/3] Updated with hashrocket --- app/models/content_instance.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/content_instance.rb b/app/models/content_instance.rb index 7f4a289e..527c76ba 100644 --- a/app/models/content_instance.rb +++ b/app/models/content_instance.rb @@ -52,11 +52,11 @@ class ContentInstance end def next - content_type.contents.where(_position_in_list: _position_in_list + 1).first() + 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() + content_type.contents.where(:_position_in_list => _position_in_list - 1).first() end def errors_to_hash