Skip to content

Instantly share code, notes, and snippets.

@Peeja
Created November 20, 2008 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Peeja/27084 to your computer and use it in GitHub Desktop.
Save Peeja/27084 to your computer and use it in GitHub Desktop.
CerealPagination: Demeter's Revenge for will_paginate.
# CerealPagination: Demeter's Revenge for will_paginate.
#
# For every has_many :foos association, creates a paginate_foos
# method which calls foos.paginate.
#
# TODO: Support has_and_belongs_to_many.
module CerealPagination
def self.included(base)
base.instance_eval do
class << self
def has_many_with_cereal_pagination(association_name, *args)
has_many_without_cereal_pagination(association_name, *args)
define_method("paginate_#{association_name}") do |*args|
send(association_name).paginate(*args)
end
end
alias_method_chain(:has_many, :cereal_pagination)
end
end
end
end
require File.dirname(__FILE__) + '/../spec_helper'
describe "A model with CerealPagination" do
class ActiveRecordStub
def self.has_many(name, *args); end
end
before(:each) do
@klass = Class.new ActiveRecordStub
@klass.send :include, CerealPagination
end
it "should have a paginate_foo method for each has_many association" do
@klass.send :has_many, :johnson_rods
object = @klass.new
johnson_rods = mock("johnson rods")
paginated_johnson_rods = mock("paginated johnson rods")
object.stub!(:johnson_rods).and_return(johnson_rods)
johnson_rods.stub!(:paginate).and_return(paginated_johnson_rods)
object.paginate_johnson_rods.should == paginated_johnson_rods
end
end
# Put this in your config/initializers to use CerealPagination in all your models.
class ActiveRecord::Base
include CerealPagination
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment