Module: Boss::Pagination

Included in:
Post, Resource
Defined in:
lib/boss/pagination.rb

Class Method Summary collapse

Class Method Details

.get_class_name(name) ⇒ Object



4
5
6
# File 'lib/boss/pagination.rb', line 4

def get_class_name(name)
  name.match(/.*?\:?([^:]*$|$)/)[1].underscore.pluralize
end

.included(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/boss/pagination.rb', line 9

def self.included(base)
  class_name = Pagination.get_class_name(base.name)
  base.send :define_singleton_method, "#{class_name}_for_index", lambda { |options={}|
    options.keys.map(&:to_sym)
    options = { index_limit: Object.const_get(class_name.upcase.to_sym)["index_limit"]}.merge options

    if options[:starts_at] && options[:starts_at] != 0
      base.limit(options[:index_limit]).offset(options[:starts_at])
    elsif options[:starts_at] && options[:starts_at] == 0
      []
    else
      base.limit(options[:index_limit])
    end
  }
end