Module: WillPaginate::ActiveRecord::RelationMethods

Includes:
CollectionMethods
Defined in:
lib/will_paginate/active_record.rb

Overview

makes a Relation look like WillPaginate::Collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CollectionMethods

#next_page, #out_of_bounds?, #previous_page, #total_pages

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



28
29
30
# File 'lib/will_paginate/active_record.rb', line 28

def current_page
  @current_page
end

#total_entriesObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/will_paginate/active_record.rb', line 73

def total_entries
  @total_entries ||= begin
    if loaded? and size < limit_value and (current_page == 1 or size > 0)
      offset_value + size
    else
      @total_entries_queried = true
      result = count
      result = result.size if result.respond_to?(:size) and !result.is_a?(Integer)
      result
    end
  end
end

#wp_count_options=(value) ⇒ Object (writeonly)

Sets the attribute wp_count_options

Parameters:

  • value

    the value to set the attribute wp_count_options to.



29
30
31
# File 'lib/will_paginate/active_record.rb', line 29

def wp_count_options=(value)
  @wp_count_options = value
end

Instance Method Details

#cloneObject



121
122
123
# File 'lib/will_paginate/active_record.rb', line 121

def clone
  copy_will_paginate_data super
end

#count(*args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/will_paginate/active_record.rb', line 86

def count(*args)
  if limit_value
    excluded = [:order, :limit, :offset, :reorder]
    excluded << :includes unless eager_loading?
    rel = self.except(*excluded)
    # TODO: hack. decide whether to keep
    rel = rel.apply_finder_options(@wp_count_options) if defined? @wp_count_options
    
    column_name = (select_for_count(rel) || :all)
    rel.count(column_name)
  else
    super(*args)
  end
end

#empty?Boolean

overloaded to be pagination-aware

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
# File 'lib/will_paginate/active_record.rb', line 111

def empty?
  if !loaded? and offset_value
    result = count
    result = result.size if result.respond_to?(:size) and !result.is_a?(Integer)
    result <= offset_value
  else
    super
  end
end

#find_lastObject

fix for Rails 3.0



59
60
61
62
63
64
65
# File 'lib/will_paginate/active_record.rb', line 59

def find_last
  if !loaded? and offset_value || limit_value
    @last ||= to_a.last
  else
    super
  end
end

#first(*args) ⇒ Object

dirty hack to enable ‘first` after `limit` behavior above



48
49
50
51
52
53
54
55
56
# File 'lib/will_paginate/active_record.rb', line 48

def first(*args)
  if current_page
    rel = clone
    rel.current_page = nil
    rel.first(*args)
  else
    super
  end
end

#limit(num) ⇒ Object

TODO: solve with less relation clones and code dups



38
39
40
41
42
43
44
45
# File 'lib/will_paginate/active_record.rb', line 38

def limit(num)
  rel = super
  if rel.current_page
    rel.offset rel.current_page.to_offset(rel.limit_value).to_i
  else
    rel
  end
end

#offset(value = nil) ⇒ Object



67
68
69
70
71
# File 'lib/will_paginate/active_record.rb', line 67

def offset(value = nil)
  if value.nil? then offset_value
  else super(value)
  end
end

#per_page(value = nil) ⇒ Object



31
32
33
34
35
# File 'lib/will_paginate/active_record.rb', line 31

def per_page(value = nil)
  if value.nil? then limit_value
  else limit(value)
  end
end

#scoped(options = nil) ⇒ Object

workaround for Active Record 3.0



126
127
128
# File 'lib/will_paginate/active_record.rb', line 126

def scoped(options = nil)
  copy_will_paginate_data super
end

#sizeObject

workaround for Active Record 3.0



102
103
104
105
106
107
108
# File 'lib/will_paginate/active_record.rb', line 102

def size
  if !loaded? and limit_value and group_values.empty?
    [super, limit_value].min
  else
    super
  end
end

#to_aObject



130
131
132
133
134
135
136
137
138
# File 'lib/will_paginate/active_record.rb', line 130

def to_a
  if current_page.nil? then super # workaround for Active Record 3.0
  else
    ::WillPaginate::Collection.create(current_page, limit_value) do |col|
      col.replace super
      col.total_entries ||= total_entries
    end
  end
end