Permalink
Browse files

Added #option and specs for overriding defaults with #to_html [#13]

  • Loading branch information...
1 parent d9aacef commit 46ca37cece605239eef473c73058b9e1dc67e7f8 @tj committed Oct 8, 2009
Showing with 27 additions and 9 deletions.
  1. +3 −2 lib/dm-pagination/defaults.rb
  2. +15 −7 lib/dm-pagination/pager.rb
  3. +9 −0 spec/unit/pager_spec.rb
@@ -1,7 +1,8 @@
+
module DataMapper
module Pagination
- @@defaults = {
+ @defaults = {
:per_page => 6,
:size => 7,
:pager_class => 'pager',
@@ -28,7 +29,7 @@ module Pagination
#
def self.defaults
- @@defaults
+ @defaults
end
end
@@ -59,8 +59,8 @@ def initialize options = {}
def to_html uri, options = {}
return unless total_pages > 1
- @uri = uri
- @size = options.fetch :size, Pagination.defaults[:size]
+ @uri, @options = uri, options
+ @size = option :size
raise ArgumentError, 'invalid :size; must be an odd number' if @size % 2 == 0
@size /= 2
[%(<ul class="#{Pagination.defaults[:pager_class]}">),
@@ -77,6 +77,14 @@ def to_html uri, options = {}
private
##
+ # Fetch _key_ from the options passed to #to_html, or
+ # its default value.
+
+ def option key
+ @options.fetch key, Pagination.defaults[key]
+ end
+
+ ##
# Link to _page_ with optional anchor tag _contents_.
def link_to page, contents = nil
@@ -89,7 +97,7 @@ def link_to page, contents = nil
def more position
return '' if position == :before && (current_page <= 1 || first <= 1)
return '' if position == :after && (current_page >= total_pages || last >= total_pages)
- li 'more', Pagination.defaults[:more_text]
+ li 'more', option(:more_text)
end
##
@@ -107,28 +115,28 @@ def intermediate_links
# Previous link.
def previous_link
- li 'previous jump', link_to(previous_page, Pagination.defaults[:previous_text]) if previous_page
+ li 'previous jump', link_to(previous_page, option(:previous_text)) if previous_page
end
##
# Next link.
def next_link
- li 'next jump', link_to(next_page, Pagination.defaults[:next_text]) if next_page
+ li 'next jump', link_to(next_page, option(:next_text)) if next_page
end
##
# Last link.
def last_link
- li 'last jump', link_to(total_pages, Pagination.defaults[:last_text]) if next_page
+ li 'last jump', link_to(total_pages, option(:last_text)) if next_page
end
##
# First link.
def first_link
- li 'first jump', link_to(1, Pagination.defaults[:first_text]) if previous_page
+ li 'first jump', link_to(1, option(:first_text)) if previous_page
end
##
@@ -94,6 +94,15 @@
end
end
+ [:first, :previous, :next, :last].each do |pos|
+ describe "with the #{pos.inspect} option set" do
+ it "should change the default contents for the '#{pos}' link" do
+ markup = Item.page(2, :per_page => 2).pager.to_html('/', :"#{pos}_text" => 'FOO')
+ markup.should include('>FOO<')
+ end
+ end
+ end
+
describe "when on the last page" do
it "should not render the 'Next' page link" do
markup = Item.page(4).pager.to_html('/')

0 comments on commit 46ca37c

Please sign in to comment.