Class: PaginationScope::Pagination

Inherits:
Struct
  • Object
show all
Defined in:
lib/pagination_scope.rb,
lib/pagination_scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject

Returns the value of attribute context

Returns:

  • (Object)

    the current value of context



57
58
59
# File 'lib/pagination_scope.rb', line 57

def context
  @context
end

#linkerObject

Returns the value of attribute linker

Returns:

  • (Object)

    the current value of linker



57
58
59
# File 'lib/pagination_scope.rb', line 57

def linker
  @linker
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



57
58
59
# File 'lib/pagination_scope.rb', line 57

def model
  @model
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



57
58
59
# File 'lib/pagination_scope.rb', line 57

def options
  @options
end

Instance Method Details

#currentObject



62
63
64
# File 'lib/pagination_scope.rb', line 62

def current
  page
end

#firstObject



74
75
76
# File 'lib/pagination_scope.rb', line 74

def first
  1
end

#first?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/pagination_scope.rb', line 82

def first?
  page == 1
end

#first_itemObject



66
67
68
# File 'lib/pagination_scope.rb', line 66

def first_item
  offset + 1
end

#inspectObject



176
177
178
179
# File 'lib/pagination_scope.rb', line 176

def inspect
  keys = %w( page count offset limit first last first_item last_item )
  "#<Pagination %s>" % keys.map{|key| "#{key}: #{send(key)}"}.join(', ')
end

#lastObject



78
79
80
# File 'lib/pagination_scope.rb', line 78

def last
  num_pages
end

#last?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/pagination_scope.rb', line 86

def last?
  page == num_pages
end

#last_itemObject



70
71
72
# File 'lib/pagination_scope.rb', line 70

def last_item
  [offset + limit, count].min
end

#leftObject



102
103
104
# File 'lib/pagination_scope.rb', line 102

def left
  options[:left] || 2
end

#limitObject



94
95
96
# File 'lib/pagination_scope.rb', line 94

def limit
  model.proxy_options[:limit]
end


152
153
154
155
156
157
158
# File 'lib/pagination_scope.rb', line 152

def link(i, *args)
  if linker
    linker.call(i, *args)
  else
    link_to i, url_for(:page => i)
  end
end

#next_labelObject



114
115
116
# File 'lib/pagination_scope.rb', line 114

def next_label
  options[:next] || 'Next &raquo;'
end


140
141
142
143
144
145
146
# File 'lib/pagination_scope.rb', line 140

def next_link
  if page < num_pages
    link_to next_label, url_for(:page => page + 1), :class => :older, :rel => "next"
  else
    span.call(next_label)
  end
end

#offsetObject



90
91
92
# File 'lib/pagination_scope.rb', line 90

def offset
  model.proxy_options[:offset]
end


148
149
150
# File 'lib/pagination_scope.rb', line 148

def page_link
  pages.map{|i| page_link_for(i)}
end


160
161
162
163
164
165
166
167
168
# File 'lib/pagination_scope.rb', line 160

def page_link_for(i)
  if i.nil?
    span.call(truncate, options[:style])
  elsif i == page
    span.call(i, "#{options[:style]} current")
  else
    span.call(link(i), options[:style])
  end
end

#pagesObject



122
123
124
# File 'lib/pagination_scope.rb', line 122

def pages
  @pages ||= model.pages(window, left, right)
end

#prev_labelObject



110
111
112
# File 'lib/pagination_scope.rb', line 110

def prev_label
  options[:prev] || '&laquo; Prev'
end


132
133
134
135
136
137
138
# File 'lib/pagination_scope.rb', line 132

def prev_link
  if page > 1
    link_to prev_label, url_for(:page => page - 1), :class => :prev, :rel => "prev"
  else
    span.call(prev_label)
  end
end

#rightObject



106
107
108
# File 'lib/pagination_scope.rb', line 106

def right
  options[:right] || 2
end

#spanObject



126
127
128
129
130
# File 'lib/pagination_scope.rb', line 126

def span
  proc do |*args|
    (:span, args[0].to_s, :class => (args[1]||"disabled"))
  end
end

#to_s(*args) ⇒ Object



170
171
172
173
174
# File 'lib/pagination_scope.rb', line 170

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

#truncateObject



118
119
120
# File 'lib/pagination_scope.rb', line 118

def truncate
  options[:truncate] || '...'
end

#windowObject



98
99
100
# File 'lib/pagination_scope.rb', line 98

def window
  options[:window] || 5
end