makes a Relation look like WillPaginate::Collection
# File lib/will_paginate/active_record.rb, line 113 def clone copy_will_paginate_data super end
# File lib/will_paginate/active_record.rb, line 81 def count(*args) if limit_value excluded = [:order, :limit, :offset, :reorder] excluded << :includes unless eager_loading? rel = self.except(*excluded) column_name = (select_for_count(rel) || :all) rel.count(column_name) else super(*args) end end
overloaded to be pagination-aware
# File lib/will_paginate/active_record.rb, line 103 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
fix for Rails 3.0
# File lib/will_paginate/active_record.rb, line 54 def find_last(*args) if !loaded? && args.empty? && (offset_value || limit_value) @last ||= to_a.last else super end end
dirty hack to enable `first` after `limit` behavior above
# File lib/will_paginate/active_record.rb, line 43 def first(*args) if current_page rel = clone rel.current_page = nil rel.first(*args) else super end end
TODO: solve with less relation clones and code dups
# File lib/will_paginate/active_record.rb, line 33 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
# File lib/will_paginate/active_record.rb, line 62 def offset(value = nil) if value.nil? then offset_value else super(value) end end
# File lib/will_paginate/active_record.rb, line 26 def per_page(value = nil) if value.nil? then limit_value else limit(value) end end
workaround for Active Record 3.0
# File lib/will_paginate/active_record.rb, line 118 def scoped(options = nil) copy_will_paginate_data super end
workaround for Active Record 3.0
# File lib/will_paginate/active_record.rb, line 94 def size if !loaded? and limit_value and group_values.empty? [super, limit_value].min else super end end
# File lib/will_paginate/active_record.rb, line 122 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
# File lib/will_paginate/active_record.rb, line 68 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