Module: WillPaginate::ActiveRecord::RelationMethods

### Home Classes Methods

### In Files

- lib/will_paginate/active_record.rb

### Methods

- #clone
- #count
- #empty?
- #find_last
- #first
- #limit
- #offset
- #per_page
- #scoped
- #size
- #to_a
- #total_entries

### Included Modules

- WillPaginate::CollectionMethods

### Class/Module Index

Quicksearch

- WillPaginate
- WillPaginate::ActionView
- WillPaginate::ActionView::LinkRenderer
- WillPaginate::ActiveRecord
- WillPaginate::ActiveRecord::BaseMethods
- WillPaginate::ActiveRecord::Pagination
- WillPaginate::ActiveRecord::RelationMethods
- WillPaginate::Collection
- WillPaginate::CollectionMethods
- WillPaginate::DataMapper
- WillPaginate::DataMapper::CollectionMethods
- WillPaginate::DataMapper::Pagination
- WillPaginate::Deprecation
- WillPaginate::Deprecation::Hash
- WillPaginate::I18n
- WillPaginate::InvalidPage
- WillPaginate::Merb
- WillPaginate::Merb::LinkRenderer
- WillPaginate::PageNumber
- WillPaginate::PerPage
- WillPaginate::PerPage::Inheritance
- WillPaginate::Railtie
- WillPaginate::Railtie::ControllerRescuePatch
- WillPaginate::Railtie::ShowExceptionsPatch
- WillPaginate::SequelMethods
- WillPaginate::Sinatra
- WillPaginate::Sinatra::Helpers
- WillPaginate::Sinatra::LinkRenderer
- WillPaginate::ViewHelpers
- WillPaginate::ViewHelpers::LinkRenderer
- WillPaginate::ViewHelpers::LinkRendererBase
- Array
- Object

No matching classes.

# WillPaginate::ActiveRecord::RelationMethods

makes a Relation look like WillPaginate::Collection

### Attributes

current_page[RW]
total_entries[W]
wp_count_options[W]

### Public Instance Methods

clone() click to toggle source

    # File lib/will_paginate/active_record.rb, line 121
    def clone
      copy_will_paginate_data super
    end

count(*args) click to toggle source

    # 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?() click to toggle source

overloaded to be pagination-aware

    # 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_last() click to toggle source

fix for Rails 3.0

    # 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) click to toggle source

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

    # 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) click to toggle source

TODO: solve with less relation clones and code dups

    # 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) click to toggle source

    # 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) click to toggle source

    # 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) click to toggle source

workaround for Active Record 3.0

    # File lib/will_paginate/active_record.rb, line 126
    def scoped(options = nil)
      copy_will_paginate_data super
    end

size() click to toggle source

workaround for Active Record 3.0

    # 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_a() click to toggle source

    # 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

total_entries() click to toggle source

    # 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.