Skip to content

Commit

Permalink
deprecate renderer, previous/next_label global settings with paginati…
Browse files Browse the repository at this point in the history
…on_options

Renderer shouldn't be set globally because of mounted apps
compatibility, and previous/next_label in Rails should be customized
with i18n rather than editing pagination_options.
  • Loading branch information
mislav committed Sep 17, 2011
1 parent 3c12251 commit d6e775c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 20 deletions.
55 changes: 55 additions & 0 deletions lib/will_paginate/deprecation.rb
@@ -0,0 +1,55 @@
module WillPaginate::Deprecation
class << self
def warn(message, stack = caller)
offending_line = origin_of_call(stack)
full_message = "DEPRECATION WARNING: #{message} (called from #{offending_line})"
logger = rails_logger || Kernel
logger.warn full_message
end

private

def rails_logger
defined?(Rails) && Rails.logger
end

def origin_of_call(stack)
lib_root = File.expand_path('../../..', __FILE__)
stack.find { |line| line.index(lib_root) != 0 } || stack.first
end
end

class Hash < ::Hash
def initialize(values = {})
super()
update values
@deprecated = {}
end

def []=(key, value)
check_deprecated(key, value)
super
end

def deprecate_key(*keys)
message = block_given? ? Proc.new : keys.pop
Array(keys).each { |key| @deprecated[key] = message }
end

def merge(another)
to_hash.update(another)
end

def to_hash
::Hash.new.update(self)
end

private

def check_deprecated(key, value)
if msg = @deprecated[key] and (!msg.respond_to?(:call) or (msg = msg.call(key, value)))
WillPaginate::Deprecation.warn(msg)
end
end
end
end
8 changes: 7 additions & 1 deletion lib/will_paginate/view_helpers.rb
@@ -1,6 +1,7 @@
# encoding: utf-8
require 'will_paginate/core_ext'
require 'will_paginate/i18n'
require 'will_paginate/deprecation'

module WillPaginate
# = Will Paginate view helpers
Expand All @@ -19,7 +20,7 @@ class << self
end

# default view options
self.pagination_options = {
self.pagination_options = Deprecation::Hash.new \
:class => 'pagination',
:previous_label => nil,
:next_label => nil,
Expand All @@ -30,7 +31,12 @@ class << self
:params => nil,
:page_links => true,
:container => true

label_deprecation = Proc.new { |key, value|
"set the 'will_paginate.#{key}' key in your i18n locale instead of editing pagination_options" if defined? Rails
}
pagination_options.deprecate_key(:previous_label, :next_label, &label_deprecation)
pagination_options.deprecate_key(:renderer) { |key| "pagination_options[#{key.inspect}] shouldn't be set globally" }

include WillPaginate::I18n

Expand Down
56 changes: 38 additions & 18 deletions spec/spec_helper.rb
Expand Up @@ -6,34 +6,26 @@
# no debugger available
end

module MyExtras
protected

def include_phrase(string)
PhraseMatcher.new(string)
end
RSpec.configure do |config|
config.include Module.new {
protected

def collection(params = {})
if params[:total_pages]
params[:per_page] = 1
params[:total_entries] = params[:total_pages]
def include_phrase(string)
PhraseMatcher.new(string)
end
WillPaginate::Collection.new(params[:page] || 1, params[:per_page] || 30, params[:total_entries])
end
end

RSpec.configure do |config|
# config.include My::Pony, My::Horse, :type => :farm
config.include MyExtras
# config.predicate_matchers[:swim] = :can_swim?
def have_deprecation(msg)
DeprecationMatcher.new(msg)
end
}

config.mock_with :mocha
end

class PhraseMatcher
def initialize(string)
@string = string
@pattern = /\b#{string}\b/
@pattern = /\b#{Regexp.escape string}\b/
end

def matches?(actual)
Expand All @@ -49,3 +41,31 @@ def negative_failure_message
"expected #{@actual.inspect} not to contain phrase #{@string.inspect}"
end
end

require 'stringio'

class DeprecationMatcher
def initialize(message)
@message = message
end

def matches?(block)
@actual = hijack_stderr(&block)
PhraseMatcher.new("DEPRECATION WARNING: #{@message}").matches?(@actual)
end

def failure_message
"expected deprecation warning #{@message.inspect}, got #{@actual.inspect}"
end

private

def hijack_stderr
err = $stderr
$stderr = StringIO.new
yield
$stderr.string.rstrip
ensure
$stderr = err
end
end
14 changes: 14 additions & 0 deletions spec/view_helpers/base_spec.rb
Expand Up @@ -33,6 +33,20 @@
will_paginate(collection).should be_nil
end
end

describe "pagination_options" do
let(:pagination_options) { WillPaginate::ViewHelpers.pagination_options }

it "deprecates setting :renderer" do
begin
lambda {
pagination_options[:renderer] = 'test'
}.should have_deprecation("pagination_options[:renderer] shouldn't be set")
ensure
pagination_options.delete :renderer
end
end
end

describe "page_entries_info" do
before :all do
Expand Down
10 changes: 9 additions & 1 deletion spec/view_helpers/link_renderer_base_spec.rb
Expand Up @@ -71,7 +71,15 @@ def showing_pages(*pages)
end

protected


def collection(params = {})
if params[:total_pages]
params[:per_page] = 1
params[:total_entries] = params[:total_pages]
end
WillPaginate::Collection.new(params[:page] || 1, params[:per_page] || 30, params[:total_entries])
end

def prepare(collection_options, options = {})
@renderer.prepare(collection(collection_options), options)
end
Expand Down

0 comments on commit d6e775c

Please sign in to comment.