Skip to content

Commit

Permalink
better will_paginate support for Neo4j::Rails::Model [#194 state:reso…
Browse files Browse the repository at this point in the history
…lved]
  • Loading branch information
andreasronge committed Nov 2, 2011
1 parent 0f32d2c commit b498bf5
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/neo4j.rb
Expand Up @@ -70,6 +70,7 @@ module Neo4j

require 'neo4j/version'
require 'neo4j/neo4j'
require 'neo4j/paginate'
require 'neo4j/node'
require 'neo4j/relationship'
require 'neo4j/relationship_set'
Expand Down
25 changes: 25 additions & 0 deletions lib/neo4j/paginate.rb
@@ -0,0 +1,25 @@
module Neo4j
module Paginate
def self.included(base)
base.send(:include, WillPaginate::Finders::Base)
end


def wp_query(options, pager, args, &block) #:nodoc:
page = pager.current_page || 1
per_page = pager.per_page
to = per_page * page
from = to - per_page
i = 0
res = []
each do |node|
res << node.wrapper if i >= from
i += 1
break if i >= to
end
pager.replace res
pager.total_entries ||= count
end

end
end
4 changes: 4 additions & 0 deletions lib/neo4j/rails/relationship.rb
Expand Up @@ -88,6 +88,10 @@ def reset_attributes
@properties = {}
end

def wrapper
self
end

# --------------------------------------
# Public Class Methods
# --------------------------------------
Expand Down
1 change: 1 addition & 0 deletions lib/neo4j/rails/relationships/node_dsl.rb
Expand Up @@ -9,6 +9,7 @@ module Relationships
#
class NodesDSL
include Enumerable
include Neo4j::Paginate

def initialize(storage, dir)
@storage = storage
Expand Down
1 change: 1 addition & 0 deletions lib/neo4j/rails/relationships/rels_dsl.rb
Expand Up @@ -18,6 +18,7 @@ module Relationships
#
class RelsDSL
include Enumerable
include Neo4j::Paginate

def initialize(storage, dir=:both)
@storage = storage
Expand Down
18 changes: 1 addition & 17 deletions lib/neo4j/traversal/traverser.rb
Expand Up @@ -31,7 +31,7 @@ def evaluate(path)
class Traverser
include Enumerable
include ToJava
include WillPaginate::Finders::Base
include Neo4j::Paginate


def initialize(from, type = nil, dir=nil)
Expand Down Expand Up @@ -85,22 +85,6 @@ def to_s
end


def wp_query(options, pager, args, &block) #:nodoc:
page = pager.current_page || 1
per_page = pager.per_page
to = per_page * page
from = to - per_page
i = 0
res = []
iterator.each do |node|
res << node.wrapper if i >= from
i += 1
break if i >= to
end
pager.replace res
pager.total_entries ||= count
end

def <<(other_node)
new(other_node)
self
Expand Down
101 changes: 101 additions & 0 deletions spec/rails/paginate_spec.rb
@@ -0,0 +1,101 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Neo4j::Rails::Model, "#paginate" do
context "pagination of #all" do
before(:all) do
@class = create_model do
property :flavour
end
end
before(:each) do
7.times{|i| @class.create(:flavour => "flavour_#{i}")}
end

it "can return the first page" do
res = @class.all.paginate(:page => 1, :per_page => 3)
res.map{|x| x.flavour}.should == %w[flavour_0 flavour_1 flavour_2]
end

it "can return the next page" do
res = @class.all.paginate(:page => 2, :per_page => 3)
res.map{|x| x.flavour}.should == %w[flavour_3 flavour_4 flavour_5]
end

it "can return the last page" do
res = @class.all.paginate(:page => 3, :per_page => 3)
res.map{|x| x.flavour}.should == %w[flavour_6]
end

end

context "pagination of #find result" do
class ModelWithFulltextIndex < Neo4j::Rails::Model

This comment has been minimized.

Copy link
@endeepak

endeepak Nov 8, 2011

Contributor

Hi Andreas,

This test model does not have any fulltext index. Is it even possible to apply sort on fulltext index?

This comment has been minimized.

Copy link
@andreasronge

andreasronge Nov 8, 2011

Author Member

I don't know, never tried it. It would be great if it was possible.

property :flavour
property :number
index :flavour
index :number
end

before(:each) do
7.times { |i| ModelWithFulltextIndex.create!(:flavour => "vanilla", :number => i.to_s) }
end

it "can return the first page" do
res = ModelWithFulltextIndex.all("flavour: vanilla").desc(:number).paginate(:page => 1, :per_page => 3)
res.map { |x| x.number }.should == %w[6 5 4]
end

end


context "pagination of #has_n nodes" do
before(:all) do
@class = create_model do
property :name
has_n :friends
end
end
before(:each) do
@model = @class.create!(:name => 'base')
7.times{|i| @model.friends << @class.create(:name => "name_#{i}")}
@model.save!
end

it "can return the first page" do
res = @model.friends.paginate(:page => 1, :per_page => 3)
res.map{|x| x.name}.should == %w[name_0 name_1 name_2]
end

it "can return the next page" do
res = @model.friends.paginate(:page => 2, :per_page => 3)
res.map{|x| x.name}.should == %w[name_3 name_4 name_5]
end

it "can return the last page" do
res = @model.friends.paginate(:page => 3, :per_page => 3)
res.map{|x| x.name}.should == %w[name_6]
end

end


context "pagination of #has_n relationships" do
before(:all) do
@class = create_model do
property :name
end
end
before(:each) do
@model = @class.create!(:name => 'base')
7.times{|i| Neo4j::Rails::Relationship.create!(:knows, @model, @class.create(:name => "name_#{i}"), :number => i)}
end

it "can return the first page" do
res = @model.rels(:knows, :outgoing).paginate(:page => 1, :per_page => 3)
res.map{|x| x[:number]}.should == [0, 1, 2]
end


end

end

0 comments on commit b498bf5

Please sign in to comment.