Class: Datewari::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/datewari/paginator.rb,
lib/datewari/paginator/base.rb,
lib/datewari/paginator/mysql.rb,
lib/datewari/paginator/postgresql.rb

Defined Under Namespace

Classes: Base, Mysql, Postgresql

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rel, column, order, options) ⇒ Paginator

Returns a new instance of Paginator.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/datewari/paginator.rb', line 25

def initialize(rel, column, order, options)
  @scope = options[:scope].to_sym

  @query = case rel.klass.connection.adapter_name
           when 'PostgreSQL', 'PostGIS'
             require_relative 'paginator/postgresql'
             Postgresql.new(rel, column, order, options)
           when 'Mysql2'
             require_relative 'paginator/mysql'
             Mysql.new(rel, column, order, options)
           else
             raise ArgumentError.new("Unsupported adapter: #{rel.klass.connection.adapter_name}")
           end

  @date = Util.parse_date(options[:date]) || pages.first || Date.today
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



23
24
25
# File 'lib/datewari/paginator.rb', line 23

def date
  @date
end

#scopeObject

Returns the value of attribute scope.



23
24
25
# File 'lib/datewari/paginator.rb', line 23

def scope
  @scope
end

Instance Method Details

#current_dateObject



72
73
74
# File 'lib/datewari/paginator.rb', line 72

def current_date
  pages[current_index]
end

#current_entriesObject



54
55
56
# File 'lib/datewari/paginator.rb', line 54

def current_entries
  @current_entries ||= paginate.count
end

#current_indexObject



62
63
64
65
66
67
68
69
70
# File 'lib/datewari/paginator.rb', line 62

def current_index
  @current_index ||= if pages.empty?
                       0
                     elsif (i = pages.index(date_range.first.to_date))
                       i
                     else
                       pages.size - 1
                     end
end

#date_rangeObject



58
59
60
# File 'lib/datewari/paginator.rb', line 58

def date_range
  @date_range ||= send("date_range_for_#{@scope}")
end

#next_dateObject



84
85
86
87
88
89
90
# File 'lib/datewari/paginator.rb', line 84

def next_date
  if current_index < pages.size - 1
    pages[current_index + 1]
  else
    nil
  end
end

#pagesObject



42
43
44
# File 'lib/datewari/paginator.rb', line 42

def pages
  @pages ||= @query.pages
end

#paginateObject



46
47
48
# File 'lib/datewari/paginator.rb', line 46

def paginate
  @query.paginate(*date_range)
end

#prev_dateObject



76
77
78
79
80
81
82
# File 'lib/datewari/paginator.rb', line 76

def prev_date
  if current_index > 0
    pages[current_index - 1]
  else
    nil
  end
end

#total_entriesObject



50
51
52
# File 'lib/datewari/paginator.rb', line 50

def total_entries
  @total_entries ||= @query.total_entries
end