Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #412 paginated collection scope #428

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/active_admin/views/components/paginated_collection.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,16 +23,26 @@ module Views
class PaginatedCollection < ActiveAdmin::Component class PaginatedCollection < ActiveAdmin::Component
builder_method :paginated_collection builder_method :paginated_collection


attr_reader :collection


# Builds a new paginated collection component # Builds a new paginated collection component
# #
# @param [Array] collection A "paginated" collection from kaminari # @param [Array] collection A "paginated" collection from kaminari
# @param [Hash] options These options will be passed on to the page_entries_info # @param [Hash] options These options will be passed on to the page_entries_info
# method. # method.
# Useful keys: # Useful keys:
# :entry_name - The name to display for this resource collection # :entry_name - The name to display for this resource collection
# :param_name - Parameter name for page number in the links (:page by default)
# :download_links - Set to false to skip download format links
def build(collection, options = {}) def build(collection, options = {})
@collection = collection @collection = collection
@param_name = options.delete(:param_name)
@download_links = options.delete(:download_links)

unless collection.respond_to?(:num_pages)
raise(StandardError, "Collection is not a paginated scope. Set collection.page(params[:page]).per(10) before calling :paginated_collection.")
end

div(page_entries_info(options).html_safe, :class => "pagination_information") div(page_entries_info(options).html_safe, :class => "pagination_information")
@contents = div(:class => "paginated_collection_contents") @contents = div(:class => "paginated_collection_contents")
build_pagination_with_formats build_pagination_with_formats
Expand All @@ -52,13 +62,16 @@ def add_child(*args, &block)


def build_pagination_with_formats def build_pagination_with_formats
div :id => "index_footer" do div :id => "index_footer" do
build_download_format_links build_download_format_links unless @download_links == false
build_pagination build_pagination
end end
end end


def build_pagination def build_pagination
text_node paginate(collection) options = request.query_parameters.except(:commit, :format)
options[:param_name] = @param_name if @param_name

text_node paginate(collection, options.symbolize_keys)
end end


# TODO: Refactor to new HTML DSL # TODO: Refactor to new HTML DSL
Expand Down
59 changes: 59 additions & 0 deletions spec/unit/views/components/paginated_collection_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe ActiveAdmin::Views::PaginatedCollection do
describe "creating with the dsl" do
setup_arbre_context!

let(:collection) do
posts = [Post.new(:title => "First Post"), Post.new(:title => "Second Post"), Post.new(:title => "Third Post")]
Kaminari.paginate_array(posts).page(1).per(5)
end

before do
request.stub!(:query_parameters).and_return({:controller => 'admin/posts', :action => 'index', :page => '1'})
controller.params = {:controller => 'admin/posts', :action => 'index'}
end

context "when specifying collection" do
let(:pagination) do
paginated_collection(collection)
end

it "should set :collection as the passed in collection" do
pagination.find_by_tag('div').first.content.should == "Displaying <b>all 3</b> posts"
end

it "should raise error if collection has no pagination scope" do
lambda {
paginated_collection([Post.new, Post.new])
}.should raise_error(StandardError, "Collection is not a paginated scope. Set collection.page(params[:page]).per(10) before calling :paginated_collection.")
end
end

context "when specifying :param_name option" do
let(:collection) do
posts = 10.times.inject([]) {|m| m << Post.new }
Kaminari.paginate_array(posts).page(1).per(5)
end

let(:pagination) { paginated_collection(collection, :param_name => :post_page) }

it "should customize the page number parameter in pagination links" do
pagination.find_by_tag('div').last.content.should match(/\/admin\/posts\?post_page=2/)
end
end

context "when specifying :download_links => false option" do
let(:collection) do
posts = 10.times.inject([]) {|m| m << Post.new }
Kaminari.paginate_array(posts).page(1).per(5)
end

let(:pagination) { paginated_collection(collection, :download_links => false) }

it "should not render download links" do
pagination.find_by_tag('div').last.content.should_not match(/Download:/)
end
end
end
end