Class: Datewari::Paginator
- Inherits:
-
Object
- Object
- Datewari::Paginator
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
#date ⇒ Object
Returns the value of attribute date.
23
24
25
|
# File 'lib/datewari/paginator.rb', line 23
def date
@date
end
|
#scope ⇒ Object
Returns the value of attribute scope.
23
24
25
|
# File 'lib/datewari/paginator.rb', line 23
def scope
@scope
end
|
Instance Method Details
#current_date ⇒ Object
72
73
74
|
# File 'lib/datewari/paginator.rb', line 72
def current_date
pages[current_index]
end
|
#current_entries ⇒ Object
54
55
56
|
# File 'lib/datewari/paginator.rb', line 54
def current_entries
@current_entries ||= paginate.count
end
|
#current_index ⇒ Object
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_range ⇒ Object
58
59
60
|
# File 'lib/datewari/paginator.rb', line 58
def date_range
@date_range ||= send("date_range_for_#{@scope}")
end
|
#next_date ⇒ Object
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
|
#pages ⇒ Object
42
43
44
|
# File 'lib/datewari/paginator.rb', line 42
def pages
@pages ||= @query.pages
end
|
#paginate ⇒ Object
46
47
48
|
# File 'lib/datewari/paginator.rb', line 46
def paginate
@query.paginate(*date_range)
end
|
#prev_date ⇒ Object
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_entries ⇒ Object
50
51
52
|
# File 'lib/datewari/paginator.rb', line 50
def total_entries
@total_entries ||= @query.total_entries
end
|