Sha256: ef47d303e95607ebb1536be53ac1ce45dfa1ac55716be1fd0b9498c73d252850

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require 'helpers'

module PaginatorHelper
	def paginator(current_page, total_pages, options={})
		content = []

		state = current_page == 1 ? :disabled : :normal
		param_name = options.delete(:param) || :page
		content << page_item('&laquo;'.html_safe, current_page-1, state, param_name)

		total_pages.times do |i|
			page_num = i + 1
			state = page_num == current_page ? :active : :normal
			content << page_item(page_num, page_num, state, param_name)
		end

		state = current_page == total_pages ? :disabled : :normal
		content << page_item('&raquo;'.html_safe, current_page+1, state, param_name)

		paginator_params = options.dup
		paginator_params[:class] = parse_html_classes_to_arr paginator_params[:class]
		paginator_params[:class] << 'pagination'

		size = paginator_params.delete :size

		case size
			when :large
				paginator_params[:class] << 'pagination-lg'
			when :small
				paginator_params[:class] << 'pagination-sm'
		end

		content_tag :ul, content.join("\n").html_safe, paginator_params
	end

	private
	def page_item(text, page_num, state, param_name)
		options = {}

		case state
			when :disabled, :active
				content = content_tag :span, text
				options[:class] = state
			else
				content = link_to text, "#{request.path}?#{param_name}=#{page_num}#{additional_uri_params}"
		end

		content_tag :li, content, options
	end

	def additional_uri_params
		additionalParams = ''
		exceptedKeys = ['page', 'action', 'controller']

		params.each do |key, value|
			unless exceptedKeys.include? key.downcase
				additionalParams << "&#{key}=#{value}"
			end
		end

		additionalParams
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter-bootstrap-for-rails-1.3.4 app/helpers/paginator_helper.rb