3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/paginate_me/paginate.rb', line 3
def paginate_me(item, options = {})
@options = options
@options[:params_var] ||= :page
@options[:per_page] ||= 10
@options[:current_page] = self.params[@options[:params_var]].to_i || 1
@options[:order] ||= "#{item.to_s}.created_at ASC"
current_page = @options[:current_page] <= 0 ? 1 : @options[:current_page]
model_name = item.to_s
model = model_name.singularize.camelize.constantize
@options[:base_url] ||= method("#{model_name}_path").call
instance_variable_set("@#{model_name}",
model
.includes(@options[:includes])
.where(@options[:where])
.order(@options[:order])
.limit(@options[:per_page])
.offset((current_page-1) * @options[:per_page]) )
@options[:page_total] = (model.includes(@options[:includes]).where(@options[:where]).count / @options[:per_page].to_f).ceil
if @options[:current_page] <= 0
@options[:current_page] = 1
elsif @options[:current_page] > @options[:page_total]
@options[:current_page] = @options[:page_total] - 1
end
end
|