Permalink
Browse files

goodbye for now, Active Resource pagination

  • Loading branch information...
mislav committed Jul 27, 2011
1 parent dfd206b commit 05a8dfce8b3a763901be3bc01695ba4b06ee0ff3
Showing with 0 additions and 103 deletions.
  1. +0 −51 lib/will_paginate/finders/active_resource.rb
  2. +0 −52 spec/finders/active_resource_spec.rb
@@ -1,51 +0,0 @@
-require 'will_paginate/finders/base'
-require 'active_resource'
-
-module WillPaginate::Finders
- # Paginate your ActiveResource models.
- #
- # @posts = Post.paginate :all, :params => {
- # :page => params[:page], :order => 'created_at DESC'
- # }
- #
- module ActiveResource
- include WillPaginate::Finders::Base
-
- protected
-
- def wp_query(options, pager, args, &block) #:nodoc:
- unless args.empty? or args.first == :all
- raise ArgumentError, "finder arguments other than :all are not supported for pagination (#{args.inspect} given)"
- end
- params = (options[:params] ||= {})
- params[:page] = pager.current_page
- params[:per_page] = pager.per_page
-
- pager.replace find_every(options, &block)
- end
-
- # Takes the format that Hash.from_xml produces out of an unknown type
- # (produced by WillPaginate::Collection#to_xml_with_collection_type),
- # parses it into a WillPaginate::Collection,
- # and forwards the result to the former +instantiate_collection+ method.
- # It only does this for hashes that have a :type => "collection".
- def instantiate_collection_with_collection(collection, prefix_options = {}) #:nodoc:
- if collection.is_a?(Hash) && collection["type"] == "collection"
- collectables = collection.values.find{ |c| c.is_a?(Hash) || c.is_a?(Array) }
- collectables = [collectables].compact unless collectables.kind_of?(Array)
- instantiated_collection = WillPaginate::Collection.create(collection["current_page"], collection["per_page"], collection["total_entries"]) do |pager|
- pager.replace instantiate_collection_without_collection(collectables, prefix_options)
- end
- else
- instantiate_collection_without_collection(collection, prefix_options)
- end
- end
- end
-end
-
-ActiveResource::Base.class_eval do
- extend WillPaginate::Finders::ActiveResource
- class << self
- # alias_method_chain :instantiate_collection, :collection
- end
-end
@@ -1,52 +0,0 @@
-require 'spec_helper'
-require 'will_paginate/finders/active_resource'
-require 'active_resource/http_mock'
-
-class AresProject < ActiveResource::Base
- self.site = 'http://localhost:4000'
-end
-
-describe WillPaginate::Finders::ActiveResource do
-
- before :all do
- # ActiveResource::HttpMock.respond_to do |mock|
- # mock.get "/ares_projects.xml?page=1&per_page=5", {}, [].to_xml
- # end
- end
-
- it "should integrate with ActiveResource::Base" do
- ActiveResource::Base.should respond_to(:paginate)
- end
-
- it "should error when no parameters for #paginate" do
- lambda { AresProject.paginate }.should raise_error(ArgumentError)
- end
-
- it "should paginate" do
- AresProject.expects(:find_every).with(:params => { :page => 1, :per_page => 5 }).returns([])
- AresProject.paginate(:page => 1, :per_page => 5)
- end
-
- it "should have 30 per_page as default" do
- AresProject.expects(:find_every).with(:params => { :page => 1, :per_page => 30 }).returns([])
- AresProject.paginate(:page => 1)
- end
-
- it "should support #paginate(:all)" do
- lambda { AresProject.paginate(:all) }.should raise_error(ArgumentError)
- end
-
- it "should error #paginate(:other)" do
- lambda { AresProject.paginate(:first) }.should raise_error(ArgumentError)
- end
-
- protected
-
- def create(page = 2, limit = 5, total = nil, &block)
- if block_given?
- WillPaginate::Collection.create(page, limit, total, &block)
- else
- WillPaginate::Collection.new(page, limit, total)
- end
- end
-end

0 comments on commit 05a8dfc

Please sign in to comment.