Module: PaginationScope

Defined in:
lib/pagination_scope.rb

Defined Under Namespace

Modules: Extention Classes: Pagination

Constant Summary collapse

VERSION =
'0.1.2'

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pagination_scope.rb', line 7

def included(base)
  base.class_eval do
    named_scope :paginate, (proc do |*args|
      options  = args.last.is_a?(Hash) ? args.pop : {}
      page     = [(args.shift || options.delete(:page) || 1).to_i - 1, 0].max
      per_page = args.shift || options.delete(:per_page) || 10
      options[:extend] = PaginationScope::Extention
      if per_page < 0
        per_page = -per_page
        options[:order] ||= "#{table_name}.id ASC"
        options[:order].gsub!(/\b(DESC|ASC)\b/i){
          case $1.upcase
          when 'DESC'; 'ASC'
          when 'ASC'; 'DESC'
          end
        }
      end
      options.merge :offset => per_page*(page), :limit => per_page
    end)
  end
end