Skip to content

Commit

Permalink
bugfixes in index_page_for_model, sitecontroller pagination separated…
Browse files Browse the repository at this point in the history
… out for reuse in other public-facing controllers
  • Loading branch information
spanner committed Jun 8, 2010
1 parent d052474 commit 6509ade
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
6 changes: 3 additions & 3 deletions app/controllers/admin/resource_controller.rb
Expand Up @@ -182,9 +182,9 @@ def continue_url(options)

def index_page_for_model
parts = {:action => "index"}
if paginated? && model && index = model_class.all.index(model)
page = (index + 1 / pagination_parameters[:per_page]).to_i
parts[:page] = page if page && page > 0
if paginated? && model && i = model_class.all.index(model)
p = ((i + 1) / pagination_parameters[:per_page].to_i) + 1
parts[:p] = p if p && p > 1
end
parts
end
Expand Down
22 changes: 2 additions & 20 deletions app/controllers/site_controller.rb
@@ -1,6 +1,7 @@
class SiteController < ApplicationController
include Radiant::Pagination::Controller

skip_before_filter :verify_authenticity_token
before_filter :configure_pagination
no_login_required
cattr_writer :cache_timeout

Expand Down Expand Up @@ -72,24 +73,5 @@ def dev?
def live?
not dev?
end

def configure_pagination
# unconfigured parameters remain at will_paginate defaults
# will_paginate controller options are not overridden by tag attribetus
WillPaginate::ViewHelpers.pagination_options[:param_name] = Radiant::Config["pagination.param_name"].to_sym unless Radiant::Config["pagination.param_name"].blank?
WillPaginate::ViewHelpers.pagination_options[:per_page_param_name] = Radiant::Config["pagination.per_page_param_name"].blank? ? :per_page : Radiant::Config["pagination.per_page_param_name"].to_sym

# will_paginate view options can be overridden by tag attributes
[:class, :previous_label, :next_label, :inner_window, :outer_window, :separator, :container].each do |opt|
WillPaginate::ViewHelpers.pagination_options[opt] = Radiant::Config["pagination.#{opt}"] unless Radiant::Config["pagination.#{opt}"].blank?
end
end

def pagination_parameters
{
:page => params[WillPaginate::ViewHelpers.pagination_options[:param_name]] || 1,
:per_page => params[WillPaginate::ViewHelpers.pagination_options[:per_page_param_name]] || Radiant::Config['pagination.per_page'] || 20
}
end

end
34 changes: 34 additions & 0 deletions lib/radiant/pagination/controller.rb
@@ -0,0 +1,34 @@
module Radiant::Pagination::Controller
# for inclusion into public-facing controllers

def configure_pagination
# unconfigured parameters remain at will_paginate defaults
# will_paginate controller options are not overridden by tag attribetus
WillPaginate::ViewHelpers.pagination_options[:param_name] = Radiant::Config["pagination.param_name"].to_sym unless Radiant::Config["pagination.param_name"].blank?
WillPaginate::ViewHelpers.pagination_options[:per_page_param_name] = Radiant::Config["pagination.per_page_param_name"].blank? ? :per_page : Radiant::Config["pagination.per_page_param_name"].to_sym

# will_paginate view options can be overridden by tag attributes
[:class, :previous_label, :next_label, :inner_window, :outer_window, :separator, :container].each do |opt|
WillPaginate::ViewHelpers.pagination_options[opt] = Radiant::Config["pagination.#{opt}"] unless Radiant::Config["pagination.#{opt}"].blank?
end
end

def pagination_parameters
{
:page => params[WillPaginate::ViewHelpers.pagination_options[:param_name]] || 1,
:per_page => params[WillPaginate::ViewHelpers.pagination_options[:per_page_param_name]] || Radiant::Config['pagination.per_page'] || 20
}
end

def self.included(base)
base.class_eval {
before_filter :configure_pagination
}
end

end





0 comments on commit 6509ade

Please sign in to comment.