Skip to content

Commit

Permalink
first version that works with Merb -> add a Merb inclusion hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Oct 12, 2008
1 parent 2039231 commit ba7096b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
8 changes: 8 additions & 0 deletions lib/will_paginate.rb
Expand Up @@ -33,3 +33,11 @@ def self.enable_named_scope(patch = true)
require 'will_paginate/view_helpers/action_view' if defined?(ActionController)
require 'will_paginate/finders/active_record' if defined?(ActiveRecord)
end

if defined?(Merb::Plugins)
require 'will_paginate/collection'
require 'will_paginate/view_helpers/base'
require 'will_paginate/view_helpers/link_renderer'
# this only includes will_paginate view stuff in Merb (not finder adapters)
Merb::AbstractController.send(:include, WillPaginate::ViewHelpers::Base)
end
12 changes: 12 additions & 0 deletions lib/will_paginate/view_helpers/action_view.rb
Expand Up @@ -68,3 +68,15 @@ def infer_collection_from_controller
if defined?(ActionController::Base) and ActionController::Base.respond_to? :rescue_responses
ActionController::Base.rescue_responses['WillPaginate::InvalidPage'] = :not_found
end

WillPaginate::ViewHelpers::LinkRenderer.class_eval do
protected

def default_url_params
{ :escape => false }
end

def generate_url(params)
@template.url_for(params)
end
end
30 changes: 20 additions & 10 deletions lib/will_paginate/view_helpers/link_renderer.rb
Expand Up @@ -82,29 +82,35 @@ def html_container(html)
# and <tt>:params</tt> option into account.
def url(page)
@base_url_params ||= begin
url_params = default_url_params
url_params = base_url_params
merge_optional_params(url_params)
url_params
end

url_params = @base_url_params.dup
add_current_page_param(url_params, page)

@template.url_for(url_params)
generate_url(url_params)
end

def default_url_params
url_params = { :escape => false }
if @template.request.get?
# page links should preserve GET parameters
symbolized_update(url_params, @template.params)
end
{ }
end

def base_url_params
url_params = default_url_params
# page links should preserve GET parameters
symbolized_update(url_params, @template.params) if get_request?
url_params
end

def merge_optional_params(url_params)
symbolized_update(url_params, @options[:params]) if @options[:params]
end

def add_current_page_param(url_params, page)
unless param_name.index(/[^\w-]/)
url_params[param_name] = page
url_params[param_name.to_sym] = page
else
page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest).
parse_query_parameters(param_name + '=' + page.to_s)
Expand All @@ -113,8 +119,12 @@ def add_current_page_param(url_params, page)
end
end

def merge_optional_params(url_params)
symbolized_update(url_params, @options[:params]) if @options[:params]
def get_request?
@template.request.get?
end

def generate_url(params)
@template.url(params)
end

private
Expand Down

0 comments on commit ba7096b

Please sign in to comment.