Skip to content

Commit

Permalink
Full configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ceritium committed Aug 19, 2015
1 parent 0664f5f commit a944ae9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
9 changes: 4 additions & 5 deletions lib/jbuilder/pagination/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
module Jbuilder::Pagination
class Configuration

attr_accessor :per_page_method
attr_accessor :page_method
attr_accessor :page_param
attr_accessor :per_page_param
attr_accessor :current_page_method, :total_pages_method, :per_page_method, :page_method, :page_param, :per_page_param

def initialize
@total_pages_method = :total_pages
@current_page_method = :current_page
@per_page_method = :per_page
@per_page_param = :per_page
@page_method = :page
@page_param = :page
@per_page_param = :per_page
end
end
end
19 changes: 12 additions & 7 deletions lib/jbuilder/pagination/pages.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Jbuilder

def pages!(collection, options={})
return unless collection

Expand All @@ -11,26 +12,30 @@ def pages!(collection, options={})

private

delegate :per_page_method, :page_param, :per_page_param, to: :configuration
delegate :current_page_method, :total_pages_method, :per_page_method, :page_param, :per_page_param, to: :configuration

def configuration
Pagination.configuration
end


def pages_from(collection)
return {} if collection.total_pages == 1
total_pages = collection.send(total_pages_method)
current_page = collection.send(configuration.current_page_method)

return {} if total_pages == 1

{}.tap do |pages|
unless collection.current_page == 1
unless current_page == 1
pages[:first] = 1
pages[:prev] = collection.current_page - 1
pages[:prev] = current_page - 1
end

unless collection.current_page == collection.total_pages
pages[:last] = collection.total_pages
pages[:next] = collection.current_page + 1
unless current_page == total_pages
pages[:last] = total_pages
pages[:next] = current_page + 1
end
end
end

end

0 comments on commit a944ae9

Please sign in to comment.