Permalink
Browse files

Add Sequel support.

Extended Sequel::Dataset::Pagination to walk and talk like WillPaginate::Collection
  • Loading branch information...
1 parent f203183 commit 7b5a65155d80717e321587f6ee79031abc63fb96 @mislav mislav committed Jan 8, 2009
View
@@ -25,6 +25,7 @@ lib/will_paginate/finders/active_record/named_scope_patch.rb
lib/will_paginate/finders/active_resource.rb
lib/will_paginate/finders/base.rb
lib/will_paginate/finders/data_mapper.rb
+lib/will_paginate/finders/sequel.rb
lib/will_paginate/version.rb
lib/will_paginate/view_helpers
lib/will_paginate/view_helpers.rb
@@ -44,6 +45,8 @@ spec/finders/active_resource_spec.rb
spec/finders/activerecord_test_connector.rb
spec/finders/data_mapper_spec.rb
spec/finders/data_mapper_test_connector.rb
+spec/finders/sequel_spec.rb
+spec/finders/sequel_test_connector.rb
spec/finders_spec.rb
spec/fixtures
spec/fixtures/admin.rb
@@ -36,7 +36,8 @@ def self.enable_named_scope(patch = true)
if defined?(Merb::Plugins)
require 'will_paginate/view_helpers/merb'
- if adapter = { :datamapper => 'data_mapper', :activerecord => 'active_record' }[Merb.orm]
+ # auto-load the right ORM adapter
+ if adapter = { :datamapper => 'data_mapper', :activerecord => 'active_record', :sequel => 'sequel' }[Merb.orm]
require "will_paginate/finders/#{adapter}"
end
end
@@ -0,0 +1,21 @@
+require 'sequel'
+
+existing_methods = Sequel::Dataset::Pagination.instance_methods
+
+Sequel::Dataset::Pagination.module_eval do
+ # it should quack like a WillPaginate::Collection
+
+ alias :total_pages :page_count unless existing_methods.include? 'total_pages'
+ alias :per_page :page_size unless existing_methods.include? 'per_page'
+ alias :previous_page :prev_page unless existing_methods.include? 'previous_page'
+ alias :total_entries :pagination_record_count unless existing_methods.include? 'total_entries'
+
+ def out_of_bounds?
+ current_page > total_pages
+ end
+
+ # Current offset of the paginated collection
+ def offset
+ (current_page - 1) * per_page
+ end
+end
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../spec_helper'
+require 'spec_helper'
require 'will_paginate/finders/data_mapper'
require File.dirname(__FILE__) + '/data_mapper_test_connector'
@@ -0,0 +1,53 @@
+require 'spec_helper'
+require 'will_paginate/finders/sequel'
+require File.dirname(__FILE__) + '/sequel_test_connector'
+
+describe Sequel::Dataset::Pagination, 'extension' do
+
+ class Car < Sequel::Model
+ end
+
+ it "should have the #paginate method" do
+ Car.should respond_to(:paginate)
+ end
+
+ it "should NOT have the #paginate_by_sql method" do
+ Car.should_not respond_to(:paginate_by_sql)
+ end
+
+ describe 'pagination' do
+ before(:all) do
+ Car.create(:name => 'Shelby', :notes => "Man's best friend")
+ Car.create(:name => 'Aston Martin', :notes => "Woman's best friend")
+ Car.create(:name => 'Corvette', :notes => 'King of the Jungle')
+ end
+
+ it "should imitate WillPaginate::Collection" do
+ result = Car.paginate(1, 2)
+
+ result.total_entries.should == 3
+ result.total_pages.should == 2
+ result.per_page.should == 2
+ result.current_page.should == 1
+ result.previous_page.should be_nil
+ result.next_page.should == 2
+ end
+
+ it "should perform" do
+ Car.paginate(1, 2).all.should == [Car[1], Car[2]]
+ end
+
+ it "should perform with #select and #order" do
+ result = Car.select("name as foo".lit).order(:name).paginate(1, 2).all
+ result.size.should == 2
+ result.first.values[:foo].should == "Aston Martin"
+ end
+
+ it "should perform with #filter" do
+ results = Car.filter(:name => 'Shelby').paginate(1, 2).all
+ results.size.should == 1
+ results.first.should == Car.find(:name => 'Shelby')
+ end
+ end
+
+end
@@ -0,0 +1,9 @@
+require 'sequel'
+
+db = Sequel.sqlite
+
+db.create_table :cars do
+ primary_key :id, :integer, :auto_increment => true
+ column :name, :text
+ column :notes, :text
+end
@@ -15,6 +15,6 @@ Gem::Specification.new do |s|
s.rdoc_options << '--inline-source' << '--charset=UTF-8'
s.extra_rdoc_files = ['README.rdoc', 'LICENSE', 'CHANGELOG.rdoc']
- s.files = %w(CHANGELOG.rdoc LICENSE README.rdoc Rakefile examples examples/apple-circle.gif examples/index.haml examples/index.html examples/pagination.css examples/pagination.sass init.rb lib lib/will_paginate lib/will_paginate.rb lib/will_paginate/array.rb lib/will_paginate/collection.rb lib/will_paginate/core_ext.rb lib/will_paginate/deprecation.rb lib/will_paginate/finders lib/will_paginate/finders.rb lib/will_paginate/finders/active_record lib/will_paginate/finders/active_record.rb lib/will_paginate/finders/active_record/named_scope.rb lib/will_paginate/finders/active_record/named_scope_patch.rb lib/will_paginate/finders/active_resource.rb lib/will_paginate/finders/base.rb lib/will_paginate/finders/data_mapper.rb lib/will_paginate/version.rb lib/will_paginate/view_helpers lib/will_paginate/view_helpers.rb lib/will_paginate/view_helpers/action_view.rb lib/will_paginate/view_helpers/base.rb lib/will_paginate/view_helpers/link_renderer.rb lib/will_paginate/view_helpers/link_renderer_base.rb lib/will_paginate/view_helpers/merb.rb spec spec/collection_spec.rb spec/console spec/console_fixtures.rb spec/database.yml spec/finders spec/finders/active_record_spec.rb spec/finders/active_resource_spec.rb spec/finders/activerecord_test_connector.rb spec/finders/data_mapper_spec.rb spec/finders/data_mapper_test_connector.rb spec/finders_spec.rb spec/fixtures spec/fixtures/admin.rb spec/fixtures/developer.rb spec/fixtures/developers_projects.yml spec/fixtures/project.rb spec/fixtures/projects.yml spec/fixtures/replies.yml spec/fixtures/reply.rb spec/fixtures/schema.rb spec/fixtures/topic.rb spec/fixtures/topics.yml spec/fixtures/user.rb spec/fixtures/users.yml spec/rcov.opts spec/spec.opts spec/spec_helper.rb spec/tasks.rake spec/view_helpers spec/view_helpers/action_view_spec.rb spec/view_helpers/base_spec.rb spec/view_helpers/link_renderer_base_spec.rb spec/view_helpers/view_example_group.rb)
- s.test_files = %w(spec/collection_spec.rb spec/console spec/console_fixtures.rb spec/database.yml spec/finders spec/finders/active_record_spec.rb spec/finders/active_resource_spec.rb spec/finders/activerecord_test_connector.rb spec/finders/data_mapper_spec.rb spec/finders/data_mapper_test_connector.rb spec/finders_spec.rb spec/fixtures spec/fixtures/admin.rb spec/fixtures/developer.rb spec/fixtures/developers_projects.yml spec/fixtures/project.rb spec/fixtures/projects.yml spec/fixtures/replies.yml spec/fixtures/reply.rb spec/fixtures/schema.rb spec/fixtures/topic.rb spec/fixtures/topics.yml spec/fixtures/user.rb spec/fixtures/users.yml spec/rcov.opts spec/spec.opts spec/spec_helper.rb spec/tasks.rake spec/view_helpers spec/view_helpers/action_view_spec.rb spec/view_helpers/base_spec.rb spec/view_helpers/link_renderer_base_spec.rb spec/view_helpers/view_example_group.rb)
+ s.files = %w(CHANGELOG.rdoc LICENSE README.rdoc Rakefile examples examples/apple-circle.gif examples/index.haml examples/index.html examples/pagination.css examples/pagination.sass init.rb lib lib/will_paginate lib/will_paginate.rb lib/will_paginate/array.rb lib/will_paginate/collection.rb lib/will_paginate/core_ext.rb lib/will_paginate/deprecation.rb lib/will_paginate/finders lib/will_paginate/finders.rb lib/will_paginate/finders/active_record lib/will_paginate/finders/active_record.rb lib/will_paginate/finders/active_record/named_scope.rb lib/will_paginate/finders/active_record/named_scope_patch.rb lib/will_paginate/finders/active_resource.rb lib/will_paginate/finders/base.rb lib/will_paginate/finders/data_mapper.rb lib/will_paginate/finders/sequel.rb lib/will_paginate/version.rb lib/will_paginate/view_helpers lib/will_paginate/view_helpers.rb lib/will_paginate/view_helpers/action_view.rb lib/will_paginate/view_helpers/base.rb lib/will_paginate/view_helpers/link_renderer.rb lib/will_paginate/view_helpers/link_renderer_base.rb lib/will_paginate/view_helpers/merb.rb spec spec/collection_spec.rb spec/console spec/console_fixtures.rb spec/database.yml spec/finders spec/finders/active_record_spec.rb spec/finders/active_resource_spec.rb spec/finders/activerecord_test_connector.rb spec/finders/data_mapper_spec.rb spec/finders/data_mapper_test_connector.rb spec/finders/sequel_spec.rb spec/finders/sequel_test_connector.rb spec/finders_spec.rb spec/fixtures spec/fixtures/admin.rb spec/fixtures/developer.rb spec/fixtures/developers_projects.yml spec/fixtures/project.rb spec/fixtures/projects.yml spec/fixtures/replies.yml spec/fixtures/reply.rb spec/fixtures/schema.rb spec/fixtures/topic.rb spec/fixtures/topics.yml spec/fixtures/user.rb spec/fixtures/users.yml spec/rcov.opts spec/spec.opts spec/spec_helper.rb spec/tasks.rake spec/view_helpers spec/view_helpers/action_view_spec.rb spec/view_helpers/base_spec.rb spec/view_helpers/link_renderer_base_spec.rb spec/view_helpers/view_example_group.rb)
+ s.test_files = %w(spec/collection_spec.rb spec/console spec/console_fixtures.rb spec/database.yml spec/finders spec/finders/active_record_spec.rb spec/finders/active_resource_spec.rb spec/finders/activerecord_test_connector.rb spec/finders/data_mapper_spec.rb spec/finders/data_mapper_test_connector.rb spec/finders/sequel_spec.rb spec/finders/sequel_test_connector.rb spec/finders_spec.rb spec/fixtures spec/fixtures/admin.rb spec/fixtures/developer.rb spec/fixtures/developers_projects.yml spec/fixtures/project.rb spec/fixtures/projects.yml spec/fixtures/replies.yml spec/fixtures/reply.rb spec/fixtures/schema.rb spec/fixtures/topic.rb spec/fixtures/topics.yml spec/fixtures/user.rb spec/fixtures/users.yml spec/rcov.opts spec/spec.opts spec/spec_helper.rb spec/tasks.rake spec/view_helpers spec/view_helpers/action_view_spec.rb spec/view_helpers/base_spec.rb spec/view_helpers/link_renderer_base_spec.rb spec/view_helpers/view_example_group.rb)
end

0 comments on commit 7b5a651

Please sign in to comment.