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.



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

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.



25
26
27
# File 'lib/datewari/paginator.rb', line 25

def date
  @date
end

#scopeObject

Returns the value of attribute scope.



25
26
27
# File 'lib/datewari/paginator.rb', line 25

def scope
  @scope
end

Instance Method Details

#current_dateObject



74
75
76
# File 'lib/datewari/paginator.rb', line 74

def current_date
  pages[current_index]
end

#current_entriesObject



56
57
58
# File 'lib/datewari/paginator.rb', line 56

def current_entries
  @current_entries ||= paginate.count
end

#current_indexObject



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

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



60
61
62
# File 'lib/datewari/paginator.rb', line 60

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

#next_dateObject



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

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

#pagesObject



44
45
46
# File 'lib/datewari/paginator.rb', line 44

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

#paginateObject



48
49
50
# File 'lib/datewari/paginator.rb', line 48

def paginate
  @query.paginate(*date_range)
end

#prev_dateObject



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

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

#total_entriesObject



52
53
54
# File 'lib/datewari/paginator.rb', line 52

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