--- /dev/null
+module PaginationMethods
+ extend ActiveSupport::Concern
+
+ private
+
+ ##
+ # limit selected items to one page, get ids of first item before/after the page
+ def get_page_items(items, includes)
+ id_column = "#{items.table_name}.id"
+ page_items = if params[:before]
+ items.where("#{id_column} < ?", params[:before]).order(:id => :desc)
+ elsif params[:after]
+ items.where("#{id_column} > ?", params[:after]).order(:id => :asc)
+ else
+ items.order(:id => :desc)
+ end
+
+ page_items = page_items.limit(20)
+ page_items = page_items.includes(includes)
+ page_items = page_items.sort.reverse
+
+ newer_items_id = page_items.first.id if page_items.count.positive? && items.exists?(["#{id_column} > ?", page_items.first.id])
+ older_items_id = page_items.last.id if page_items.count.positive? && items.exists?(["#{id_column} < ?", page_items.last.id])
+
+ [page_items, newer_items_id, older_items_id]
+ end
+end
class DiaryEntriesController < ApplicationController
include UserMethods
+ include PaginationMethods
layout "site", :except => :rss
@zoom = 12
end
end
-
- def get_page_items(items, includes)
- id_column = "#{items.table_name}.id"
- page_items = if params[:before]
- items.where("#{id_column} < ?", params[:before]).order(:id => :desc)
- elsif params[:after]
- items.where("#{id_column} > ?", params[:after]).order(:id => :asc)
- else
- items.order(:id => :desc)
- end
-
- page_items = page_items.limit(20)
- page_items = page_items.includes(includes)
- page_items = page_items.sort.reverse
-
- newer_items_id = page_items.first.id if page_items.count.positive? && items.exists?(["#{id_column} > ?", page_items.first.id])
- older_items_id = page_items.last.id if page_items.count.positive? && items.exists?(["#{id_column} < ?", page_items.last.id])
-
- [page_items, newer_items_id, older_items_id]
- end
end
class TracesController < ApplicationController
include UserMethods
+ include PaginationMethods
layout "site", :except => :georss
@params = params.permit(:display_name, :tag, :before, :after)
- @traces = if params[:before]
- traces.where("gpx_files.id < ?", params[:before]).order(:id => :desc)
- elsif params[:after]
- traces.where("gpx_files.id > ?", params[:after]).order(:id => :asc)
- else
- traces.order(:id => :desc)
- end
-
- @traces = @traces.limit(20)
- @traces = @traces.includes(:user, :tags)
- @traces = @traces.sort.reverse
-
- @newer_traces = @traces.count.positive? && traces.exists?(["gpx_files.id > ?", @traces.first.id])
- @older_traces = @traces.count.positive? && traces.exists?(["gpx_files.id < ?", @traces.last.id])
+ @traces, @newer_traces_id, @older_traces_id = get_page_items(traces, [:user, :tags])
# final helper vars for view
@target_user = target_user
<%= render "shared/pagination",
:newer_key => "traces.trace_paging_nav.newer",
:older_key => "traces.trace_paging_nav.older",
- :newer_id => @newer_traces && @traces.first.id,
- :older_id => @older_traces && @traces.last.id %>
+ :newer_id => @newer_traces_id,
+ :older_id => @older_traces_id %>
<table id="trace_list" class="table table-borderless table-striped">
<tbody>
<%= render "shared/pagination",
:newer_key => "traces.trace_paging_nav.newer",
:older_key => "traces.trace_paging_nav.older",
- :newer_id => @newer_traces && @traces.first.id,
- :older_id => @older_traces && @traces.last.id %>
+ :newer_id => @newer_traces_id,
+ :older_id => @older_traces_id %>
<% else %>
<h2><%= t ".empty_title" %></h2>
<p><%= t ".empty_upload_html", :upload_link => link_to(t(".upload_new"), new_trace_path),