Permalink
Browse files

move will_paginate files around so rails auto-loading doesnt get conf…

…used.

git-svn-id: svn://errtheblog.com/svn/plugins/will_paginate@213 1eaa51fe-a21a-0410-9c2e-ae7a00a434c4
  • Loading branch information...
1 parent 9f9dcb2 commit cce713cf624424c7ff49d5f2402d6d156f4a8e4f chris committed May 11, 2007
Showing with 159 additions and 156 deletions.
  1. +0 −1 init.rb
  2. +0 −60 lib/finder.rb
  3. +3 −95 lib/will_paginate.rb
  4. +60 −0 lib/will_paginate/finder.rb
  5. +38 −0 lib/will_paginate/paginated_collection.rb
  6. +58 −0 lib/will_paginate/view_helpers.rb
View
@@ -1,5 +1,4 @@
require 'will_paginate'
-require 'finder'
ActiveRecord::Base.send :include, WillPaginate::Finder
# Controllers will get some love soon
View
@@ -1,60 +0,0 @@
-module WillPaginate
- # A mixin for ActiveRecord::Base. Provides `per_page` class method
- # and makes `paginate` finders possible with some method_missing magic.
- #
- module Finder
- def self.included(base)
- base.extend ClassMethods
- class << base
- alias_method_chain :method_missing, :paginate
- define_method(:per_page) { 30 } unless respond_to? :per_page
- end
- end
-
- module ClassMethods
- def method_missing_with_paginate(method, *args, &block)
- # did somebody tried to paginate? if not, let them be
- unless method.to_s.index('paginate') == 0
- return method_missing_without_paginate(method, *args, &block)
- end
- options = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
-
- page = (options.delete(:page) || 1).to_i
- entries_per_page = options.delete(:per_page) || per_page
-
- total_entries = unless options[:total_entries]
- count_options = options.slice :conditions, :joins, :include, :order, :group, :select, :distinct
- count_options[:select] = options[:count] if options.key? :count
- count(count_options)
- else
- options.delete(:total_entries)
- end
-
- # oh, you're not so fun anymore!
- [:distinct, :count].each {|key| options.delete key } unless options.empty?
-
- finder = method.to_s.sub /^paginate/, 'find'
- # :all is implicit
- if finder == 'find'
- args.unshift(:all) if args.length < 2
- elsif finder !~ /^find_all/
- finder.sub! /^find/, 'find_all'
- end
-
- # do it!!
- args << options.merge(:offset => (page - 1) * entries_per_page, :limit => entries_per_page)
- entries = send finder, *args
- # wrap and return the package
- PaginatedCollection.new entries, page, entries_per_page, total_entries
- end
- end
- end
-
- # TODO: Controllers need love, too!
- #
- # module ControllerHelpers
- # def totally_awesome_helper
- # patience
- # end
- # end
-end
View
@@ -1,95 +1,3 @@
-module WillPaginate
- # PaginatedCollection is a proxy to the original array of
- # records. It is generated by AR::Base#paginate methods.
- #
- class PaginatedCollection
- attr_reader :current_page, :per_page, :entries
- attr_accessor :total_entries
-
- def initialize(entries, page, per_page, total)
- @entries = entries
- @current_page = page
- @per_page = per_page
- @total_entries = total
- @total_pages = (@total_entries / @per_page.to_f).ceil
- end
-
- def page_count
- @total_pages
- end
-
- include Enumerable
-
- def each &block
- @entries.each &block
- end
-
- def to_a
- @entries
- end
-
- protected
-
- # delegate missing stuff to the collection array
- def method_missing(method, *args, &block)
- @entries.send method, *args, &block
- end
- end
-
- module ViewHelpers
- # Renders Digg-style pagination. (We know you wanna!)
- # Returns nil if there is only one page in total (can't paginate that).
- #
- def will_paginate entries = @entries, options = {}
- total_pages = entries.page_count
-
- if total_pages > 1
- page = entries.current_page
- options = options.symbolize_keys.reverse_merge :class => 'pagination',
- :prev_label => '&laquo; Previous',
- :next_label => 'Next &raquo;',
- :inner_window => 4, # links around the current page
- :outer_window => 1 # links around beginning and end
-
- inner_window, outer_window = options.delete(:inner_window).to_i, options.delete(:outer_window).to_i
- min = page - inner_window
- max = page + inner_window
-
- # adjust lower or upper limit if other is out of bounds
- if max > total_pages then min -= max - total_pages
- elsif min < 1 then max += 1 - min
- end
-
- current = min..max
- beginning = 1..(1 + outer_window)
- tail = (total_pages - outer_window)..total_pages
- visible = [current, beginning, tail].map(&:to_a).sum
-
- # build the list of the links
- links = (1..total_pages).inject([]) do |list, n|
- if visible.include? n
- list << link_or_span(n, n == page, 'current')
- elsif n == beginning.last + 1 || n == tail.first - 1
- list << '...'
- end
- list
- end
-
- # next and previous buttons
- prev, succ = page - 1, page + 1
- links.unshift link_or_span(prev, prev.zero?, 'disabled', options.delete(:prev_label))
- links.push link_or_span(succ, succ > total_pages, 'disabled', options.delete(:next_label))
-
- content_tag :div, links.join, options
- end
- end
-
- protected
-
- def link_or_span(page, condition, span_class = nil, text = page.to_s)
- # page links preserve other (GET) parameters
- condition ? content_tag(:span, text, :class => span_class) :
- link_to(text, {:page => page}.reverse_merge(params))
- end
- end
-end
+require 'will_paginate/finder'
+require 'will_paginate/paginated_collection'
+require 'will_paginate/view_helpers'
@@ -0,0 +1,60 @@
+module WillPaginate
+ # A mixin for ActiveRecord::Base. Provides `per_page` class method
+ # and makes `paginate` finders possible with some method_missing magic.
+ #
+ module Finder
+ def self.included(base)
+ base.extend ClassMethods
+ class << base
+ alias_method_chain :method_missing, :paginate
+ define_method(:per_page) { 30 } unless respond_to? :per_page
+ end
+ end
+
+ module ClassMethods
+ def method_missing_with_paginate(method, *args, &block)
+ # did somebody tried to paginate? if not, let them be
+ unless method.to_s.index('paginate') == 0
+ return method_missing_without_paginate(method, *args, &block)
+ end
+ options = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
+
+ page = (options.delete(:page) || 1).to_i
+ entries_per_page = options.delete(:per_page) || per_page
+
+ total_entries = unless options[:total_entries]
+ count_options = options.slice :conditions, :joins, :include, :order, :group, :select, :distinct
+ count_options[:select] = options[:count] if options.key? :count
+ count(count_options)
+ else
+ options.delete(:total_entries)
+ end
+
+ # oh, you're not so fun anymore!
+ [:distinct, :count].each {|key| options.delete key } unless options.empty?
+
+ finder = method.to_s.sub /^paginate/, 'find'
+ # :all is implicit
+ if finder == 'find'
+ args.unshift(:all) if args.length < 2
+ elsif finder !~ /^find_all/
+ finder.sub! /^find/, 'find_all'
+ end
+
+ # do it!!
+ args << options.merge(:offset => (page - 1) * entries_per_page, :limit => entries_per_page)
+ entries = send finder, *args
+ # wrap and return the package
+ PaginatedCollection.new entries, page, entries_per_page, total_entries
+ end
+ end
+ end
+
+ # TODO: Controllers need love, too!
+ #
+ # module ControllerHelpers
+ # def totally_awesome_helper
+ # patience
+ # end
+ # end
+end
@@ -0,0 +1,38 @@
+module WillPaginate
+ # PaginatedCollection is a proxy to the original array of
+ # records. It is generated by AR::Base#paginate methods.
+ #
+ class PaginatedCollection
+ attr_reader :current_page, :per_page, :entries
+ attr_accessor :total_entries
+
+ def initialize(entries, page, per_page, total)
+ @entries = entries
+ @current_page = page
+ @per_page = per_page
+ @total_entries = total
+ @total_pages = (@total_entries / @per_page.to_f).ceil
+ end
+
+ def page_count
+ @total_pages
+ end
+
+ include Enumerable
+
+ def each &block
+ @entries.each &block
+ end
+
+ def to_a
+ @entries
+ end
+
+ protected
+
+ # delegate missing stuff to the collection array
+ def method_missing(method, *args, &block)
+ @entries.send method, *args, &block
+ end
+ end
+end
@@ -0,0 +1,58 @@
+module WillPaginate
+ module ViewHelpers
+ # Renders Digg-style pagination. (We know you wanna!)
+ # Returns nil if there is only one page in total (can't paginate that).
+ #
+ def will_paginate entries = @entries, options = {}
+ total_pages = entries.page_count
+
+ if total_pages > 1
+ page = entries.current_page
+ options = options.symbolize_keys.reverse_merge :class => 'pagination',
+ :prev_label => '&laquo; Previous',
+ :next_label => 'Next &raquo;',
+ :inner_window => 4, # links around the current page
+ :outer_window => 1 # links around beginning and end
+
+ inner_window, outer_window = options.delete(:inner_window).to_i, options.delete(:outer_window).to_i
+ min = page - inner_window
+ max = page + inner_window
+
+ # adjust lower or upper limit if other is out of bounds
+ if max > total_pages then min -= max - total_pages
+ elsif min < 1 then max += 1 - min
+ end
+
+ current = min..max
+ beginning = 1..(1 + outer_window)
+ tail = (total_pages - outer_window)..total_pages
+ visible = [current, beginning, tail].map(&:to_a).sum
+
+ # build the list of the links
+ links = (1..total_pages).inject([]) do |list, n|
+ if visible.include? n
+ list << link_or_span(n, n == page, 'current')
+ elsif n == beginning.last + 1 || n == tail.first - 1
+ list << '...'
+ end
+ list
+ end
+
+ # next and previous buttons
+ prev, succ = page - 1, page + 1
+ links.unshift link_or_span(prev, prev.zero?, 'disabled', options.delete(:prev_label))
+ links.push link_or_span(succ, succ > total_pages, 'disabled', options.delete(:next_label))
+
+ content_tag :div, links.join, options
+ end
+ end
+
+ protected
+
+ def link_or_span(page, condition, span_class = nil, text = page.to_s)
+ # page links preserve other (GET) parameters
+ condition ? content_tag(:span, text, :class => span_class) :
+ link_to(text, {:page => page}.reverse_merge(params))
+ end
+ end
+end

0 comments on commit cce713c

Please sign in to comment.