Skip to content

Commit

Permalink
Replace page numbers with ID based selection for trace indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Jul 23, 2023
1 parent bb3b0ac commit c909b29
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 71 deletions.
46 changes: 27 additions & 19 deletions app/controllers/traces_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,38 @@ def index
# 2 - all traces, not logged in = all public traces
# 3 - user's traces, logged in as same user = all user's traces
# 4 - user's traces, not logged in as that user = all user's public traces
@traces = if target_user.nil? # all traces
if current_user
Trace.visible_to(current_user) # 1
else
Trace.visible_to_all # 2
end
elsif current_user && current_user == target_user
current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
else
target_user.traces.visible_to_all # 4
end
traces = if target_user.nil? # all traces
if current_user
Trace.visible_to(current_user) # 1
else
Trace.visible_to_all # 2
end
elsif current_user && current_user == target_user
current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
else
target_user.traces.visible_to_all # 4
end

@traces = @traces.tagged(params[:tag]) if params[:tag]
traces = traces.tagged(params[:tag]) if params[:tag]

@params = params.permit(:display_name, :tag)
traces = traces.visible

@page = (params[:page] || 1).to_i
@page_size = 20
@params = params.permit(:display_name, :tag, :before, :after)

@traces = @traces.visible
@traces = @traces.order(:id => :desc)
@traces = @traces.offset((@page - 1) * @page_size)
@traces = @traces.limit(@page_size)
@traces = if params[:before]
traces.where("id < ?", params[:before]).order(:id => :desc)
elsif params[:after]
traces.where("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])

# final helper vars for view
@target_user = target_user
Expand Down
18 changes: 7 additions & 11 deletions app/views/traces/_trace_paging_nav.html.erb
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
<nav>
<ul class="pagination">
<% if page > 1 %>
<% if newer_traces %>
<li class="page-item">
<%= link_to t(".newer"), params.merge(:page => page - 1), :class => "page-link" %>
<%= link_to t(".newer"), params.merge(:before => nil, :after => traces.first.id), :class => "page-link" %>
</li>
<% else %>
<li class="page-item disabled">
<span class="page-link"><%= t(".newer") %></span>
</li>
<% end %>

<li class="page-item disabled">
<span class="page-link"><%= t(".showing_page", :page => page) %></span>
</li>

<% if traces.size < page_size %>
<li class="page-item disabled">
<span class="page-link"><%= t(".older") %></span>
<% if older_traces %>
<li class="page-item">
<%= link_to t(".older"), params.merge(:before => traces.last.id, :after => nil), :class => "page-link" %>
</li>
<% else %>
<li class="page-item">
<%= link_to t(".older"), params.merge(:page => page + 1), :class => "page-link" %>
<li class="page-item disabled">
<span class="page-link"><%= t(".older") %></span>
</li>
<% end %>
</ul>
Expand Down
20 changes: 10 additions & 10 deletions app/views/traces/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</li>
<% if params[:tag] %>
<li>
<%= link_to t(".remove_tag_filter", :tag => params[:tag]), { :controller => "traces", :action => "index", :display_name => nil, :tag => nil, :page => nil } %>
<%= link_to t(".remove_tag_filter", :tag => params[:tag]), { :controller => "traces", :action => "index", :tag => nil } %>
</li>
<% end %>
</ul>
Expand All @@ -17,33 +17,33 @@
<% if @target_user.blank? %>
<!-- public traces -->
<li class="nav-item">
<%= link_to t(".all_traces"), { :controller => "traces", :action => "index", :display_name => nil, :page => nil }, { :class => "nav-link active" } %>
<%= link_to t(".all_traces"), { :controller => "traces", :action => "index", :display_name => nil }, { :class => "nav-link active" } %>
</li>
<% if current_user %>
<li class="nav-item">
<%= link_to t(".my_traces"), { :action => "mine", :page => nil }, { :class => "nav-link" } %>
<%= link_to t(".my_traces"), { :action => "mine" }, { :class => "nav-link" } %>
</li>
<% end %>
<% elsif current_user && current_user == @target_user %>
<li class="nav-item">
<%= link_to t(".all_traces"), { :controller => "traces", :action => "index", :display_name => nil, :page => nil }, { :class => "nav-link" } %>
<%= link_to t(".all_traces"), { :controller => "traces", :action => "index", :display_name => nil }, { :class => "nav-link" } %>
</li>
<!-- my traces -->
<li class="nav-item">
<%= link_to t(".my_traces"), { :action => "mine", :page => nil }, { :class => "nav-link active" } %>
<%= link_to t(".my_traces"), { :action => "mine" }, { :class => "nav-link active" } %>
</li>
<% else %>
<!-- public_traces_from @target_user -->
<li class="nav-item">
<%= link_to t(".all_traces"), { :controller => "traces", :action => "index", :display_name => nil, :page => nil }, { :class => "nav-link" } %>
<%= link_to t(".all_traces"), { :controller => "traces", :action => "index", :display_name => nil }, { :class => "nav-link" } %>
</li>
<% if current_user && current_user != @target_user %>
<li class="nav-item">
<%= link_to t(".my_traces"), { :action => "mine", :page => nil }, { :class => "nav-link" } %>
<%= link_to t(".my_traces"), { :action => "mine" }, { :class => "nav-link" } %>
</li>
<% end %>
<li class="nav-item">
<%= link_to t(".public_traces_from", :user => @target_user&.display_name), { :action => "mine", :page => nil }, { :class => "nav-link active" } %>
<%= link_to t(".public_traces_from", :user => @target_user&.display_name), { :action => "mine" }, { :class => "nav-link active" } %>
</li>
<% end %>

Expand All @@ -66,15 +66,15 @@
<% end %>

<% if @traces.size > 0 %>
<%= render "trace_paging_nav", :page => @page, :page_size => @page_size, :traces => @traces, :params => @params %>
<%= render "trace_paging_nav", :older_traces => @older_traces, :newer_traces => @newer_traces, :traces => @traces, :params => @params %>

<table id="trace_list" class="table table-borderless table-striped">
<tbody>
<%= render @traces %>
</tbody>
</table>

<%= render "trace_paging_nav", :page => @page, :page_size => @page_size, :traces => @traces, :params => @params %>
<%= render "trace_paging_nav", :older_traces => @older_traces, :newer_traces => @newer_traces, :traces => @traces, :params => @params %>
<% else %>
<h2><%= t ".empty_title" %></h2>
<p><%= t ".empty_upload_html", :upload_link => link_to(t(".upload_new"), new_trace_path),
Expand Down
1 change: 0 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2434,7 +2434,6 @@ en:
visibility: "Visibility:"
confirm_delete: "Delete this trace?"
trace_paging_nav:
showing_page: "Page %{page}"
older: "Older Traces"
newer: "Newer Traces"
trace:
Expand Down
12 changes: 6 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,23 @@

# traces
resources :traces, :except => [:show]
get "/user/:display_name/traces/tag/:tag/page/:page" => "traces#index", :page => /[1-9][0-9]*/
get "/user/:display_name/traces/tag/:tag/page/:page", :page => /[1-9][0-9]*/, :to => redirect(:path => "/user/%{display_name}/traces/tag/%{tag}")
get "/user/:display_name/traces/tag/:tag" => "traces#index"
get "/user/:display_name/traces/page/:page" => "traces#index", :page => /[1-9][0-9]*/
get "/user/:display_name/traces/page/:page", :page => /[1-9][0-9]*/, :to => redirect(:path => "/user/%{display_name}/traces")
get "/user/:display_name/traces" => "traces#index"
get "/user/:display_name/traces/tag/:tag/rss" => "traces#georss", :defaults => { :format => :rss }
get "/user/:display_name/traces/rss" => "traces#georss", :defaults => { :format => :rss }
get "/user/:display_name/traces/:id" => "traces#show", :as => "show_trace"
get "/user/:display_name/traces/:id/picture" => "traces#picture", :as => "trace_picture"
get "/user/:display_name/traces/:id/icon" => "traces#icon", :as => "trace_icon"
get "/traces/tag/:tag/page/:page" => "traces#index", :page => /[1-9][0-9]*/
get "/traces/tag/:tag/page/:page", :page => /[1-9][0-9]*/, :to => redirect(:path => "/traces/tag/%{tag}")
get "/traces/tag/:tag" => "traces#index"
get "/traces/page/:page" => "traces#index", :page => /[1-9][0-9]*/
get "/traces/page/:page", :page => /[1-9][0-9]*/, :to => redirect(:path => "/traces")
get "/traces/tag/:tag/rss" => "traces#georss", :defaults => { :format => :rss }
get "/traces/rss" => "traces#georss", :defaults => { :format => :rss }
get "/traces/mine/tag/:tag/page/:page" => "traces#mine", :page => /[1-9][0-9]*/
get "/traces/mine/tag/:tag/page/:page", :page => /[1-9][0-9]*/, :to => redirect(:path => "/traces/mine/tag/%{tag}")
get "/traces/mine/tag/:tag" => "traces#mine"
get "/traces/mine/page/:page" => "traces#mine"
get "/traces/mine/page/:page", :page => /[1-9][0-9]*/, :to => redirect(:path => "/traces/mine")
get "/traces/mine" => "traces#mine"
get "/trace/create", :to => redirect(:path => "/traces/new")
get "/trace/:id/data" => "traces#data", :id => /\d+/, :as => "trace_data"
Expand Down
42 changes: 18 additions & 24 deletions test/controllers/traces_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,27 @@ def test_routes
{ :path => "/traces", :method => :get },
{ :controller => "traces", :action => "index" }
)
assert_routing(
{ :path => "/traces/page/1", :method => :get },
{ :controller => "traces", :action => "index", :page => "1" }
)
assert_routing(
{ :path => "/traces/tag/tagname", :method => :get },
{ :controller => "traces", :action => "index", :tag => "tagname" }
)
assert_routing(
{ :path => "/traces/tag/tagname/page/1", :method => :get },
{ :controller => "traces", :action => "index", :tag => "tagname", :page => "1" }
)
assert_routing(
{ :path => "/user/username/traces", :method => :get },
{ :controller => "traces", :action => "index", :display_name => "username" }
)
assert_routing(
{ :path => "/user/username/traces/page/1", :method => :get },
{ :controller => "traces", :action => "index", :display_name => "username", :page => "1" }
)
assert_routing(
{ :path => "/user/username/traces/tag/tagname", :method => :get },
{ :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
)
assert_routing(
{ :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
{ :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname", :page => "1" }
)

assert_routing(
{ :path => "/traces/mine", :method => :get },
{ :controller => "traces", :action => "mine" }
)
assert_routing(
{ :path => "/traces/mine/page/1", :method => :get },
{ :controller => "traces", :action => "mine", :page => "1" }
)
assert_routing(
{ :path => "/traces/mine/tag/tagname", :method => :get },
{ :controller => "traces", :action => "mine", :tag => "tagname" }
)
assert_routing(
{ :path => "/traces/mine/tag/tagname/page/1", :method => :get },
{ :controller => "traces", :action => "mine", :tag => "tagname", :page => "1" }
)

assert_routing(
{ :path => "/traces/rss", :method => :get },
Expand Down Expand Up @@ -112,6 +88,24 @@ def test_routes
{ :path => "/traces/1", :method => :delete },
{ :controller => "traces", :action => "destroy", :id => "1" }
)

get "/traces/page/1"
assert_redirected_to "/traces"

get "/traces/tag/tagname/page/1"
assert_redirected_to "/traces/tag/tagname"

get "/user/username/traces/page/1"
assert_redirected_to "/user/username/traces"

get "/user/username/traces/tag/tagname/page/1"
assert_redirected_to "/user/username/traces/tag/tagname"

get "/traces/mine/page/1"
assert_redirected_to "/traces/mine"

get "/traces/mine/tag/tagname/page/1"
assert_redirected_to "/traces/mine/tag/tagname"
end

# Check that the index of traces is displayed
Expand Down

0 comments on commit c909b29

Please sign in to comment.