Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
neoteric-navigator / spec / navigator_spec.rb
Size: Mime:
require 'spec_no_rails_helper'
require 'neoteric-navigator'

class SomePostClass < ActiveRecord::Base
  include Neoteric::Navigator
  navigate :scope => :published,
           :field => :pubdate,
           :order => 'pubdate DESC, title'

  scope :published, where('pubdate <= ? and (drafted IS NULL or drafted = ?)',
                           Date.today,
                           false)
end

module Neoteric
  describe Navigator do
    let(:post) { SomePostClass.new }

    describe "#next" do
      let!(:post) do
        SomePostClass.create!(:pubdate => Date.yesterday
                                               .advance(:days => -1))
      end

      let!(:drafted_next_post) do
        SomePostClass.create!(:pubdate => Date.yesterday,
                              :drafted => true)
      end

      let!(:next_post) do
        SomePostClass.create!(:pubdate => Date.yesterday)
      end

      let!(:newest_post_a) do
        SomePostClass.create!(:pubdate => Date.today,
                              :title   => 'A')
      end

      let!(:newest_post_b) do
        SomePostClass.create!(:pubdate => Date.today,
                              :title   => 'B')
      end

      it "returns the next post by newer date" do
        post.next.should == next_post

        next_post.next.should == newest_post_b
      end

      it "grabs the next post in the results if the pubdate is the same" do
        newest_post_b.next.should == newest_post_a
      end

      it "returns nil if there are none" do
        newest_post_a.next.should be_nil
      end

      it "does not return unpublished posts" do
        SomePostClass.create!(:pubdate => Date.tomorrow)

        newest_post_a.next.should be_nil
      end
    end

    describe "#prev" do
      let!(:post) do
        SomePostClass.create!(:pubdate => Date.today)
      end

      let!(:drafted_prev_post) do
        SomePostClass.create!(:pubdate => Date.yesterday,
                              :drafted => true)
      end

      let!(:prev_post) do
        SomePostClass.create!(:pubdate => Date.yesterday)
      end

      let!(:old_post_a) do
        SomePostClass.create!(:pubdate => Date.yesterday
                                              .advance(:days => -1),
                              :title   => 'A')
      end

      let!(:old_post_b) do
        SomePostClass.create!(:pubdate => Date.yesterday
                                              .advance(:days => -1),
                              :title   => 'B')
      end

      it "returns the previous post by older date" do
        post.prev.should == prev_post

        prev_post.prev.should == old_post_a
      end

      it "returns the previous post in the results on matching pubdate" do
        old_post_a.prev.should == old_post_b
      end

      it "returns nil if there are none" do
        old_post_b.prev.should be_nil
      end
    end
  end
end