Class: DmPagination::PaginationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-pagination/pagination_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, pagination, *args, &block) ⇒ PaginationBuilder

Returns a new instance of PaginationBuilder.



3
4
5
6
7
8
9
10
# File 'lib/dm-pagination/pagination_builder.rb', line 3

def initialize(context, pagination, *args, &block)
  @context = context
  @pagination = pagination
  @block = block
  @options = args.last.kind_of?(Hash) ? args.pop : {}
  @options[:page] ||= :page
  @args = args.blank? ? [:prev, :pages, :next] : args
end

Instance Method Details



33
34
35
36
37
38
39
40
41
# File 'lib/dm-pagination/pagination_builder.rb', line 33

def link_to_next
  if @pagination.page < @pagination.num_pages
    @context.link_to next_label,
      url(@options[:page] => @pagination.page + 1),
      :class => :next, :rel => "next"
  else
    span.call(next_label, :next)
  end
end


43
44
45
46
47
48
49
50
51
# File 'lib/dm-pagination/pagination_builder.rb', line 43

def link_to_page(page)
  if page.nil?
    span.call truncate, @options[:style]
  elsif page == @pagination.page
    span.call page, [@options[:style], 'current'].compact.join(' ')
  else
    span.call link(page), @options[:style]
  end
end


19
20
21
# File 'lib/dm-pagination/pagination_builder.rb', line 19

def link_to_pages
  @pagination.pages.map{|page| link_to_page(page)}
end


23
24
25
26
27
28
29
30
31
# File 'lib/dm-pagination/pagination_builder.rb', line 23

def link_to_prev
  if @pagination.page > 1
    @context.link_to prev_label,
      url(@options[:page] => @pagination.page - 1),
      :class => :prev, :rel => "prev"
  else
    span.call(prev_label, :prev)
  end
end

#to_s(*args) ⇒ Object



12
13
14
15
16
17
# File 'lib/dm-pagination/pagination_builder.rb', line 12

def to_s(*args)
  args = @args if args.blank?
  items = args.map{|i| respond_to?("link_to_#{i}") ?
    send("link_to_#{i}") : []}.flatten
  @context.tag(:div, items.join("\n"), :class => "pagination")
end